Advertisement
Guest User

Untitled

a guest
May 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package tictactoe;
  7.  
  8. import java.awt.Dimension;
  9. import java.awt.FlowLayout;
  10. import java.awt.Font;
  11. import java.awt.GridLayout;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JTextField;
  18.  
  19. /**
  20. *
  21. * @author nccs
  22. */
  23. public class TicTacToe extends JFrame implements ActionListener{
  24.  
  25.  
  26. private JButton[] cells;
  27. private final String PLAYER1="X";
  28. private final String PLAYER2="O";
  29. private String currentPlayer = PLAYER1;
  30.  
  31.  
  32.  
  33. public TicTacToe() {
  34. setLayout(new GridLayout(3, 3));
  35.  
  36. cells = new JButton[9];
  37. for (int i = 0; i < cells.length; i++) {
  38.  
  39. cells[i] = new JButton();
  40. cells[i].setFont(new Font(Font.SANS_SERIF, Font.PLAIN,59));
  41. cells[i].setPreferredSize(new Dimension (100,100));
  42. cells[i].addActionListener(this);
  43.  
  44. add(cells[i]);
  45. }
  46. pack();
  47. }
  48. /**
  49. * @param args the command line arguments
  50. */
  51. public static void main(String[] args) {
  52. TicTacToe ttt = new TicTacToe();
  53. ttt.setVisible(true);
  54. ttt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55.  
  56.  
  57.  
  58.  
  59. }
  60.  
  61. // T
  62.  
  63. @Override
  64. public void actionPerformed(ActionEvent e) {
  65.  
  66. JButton currentCell = (JButton)e.getSource();
  67. currentCell.setText(currentPlayer);
  68.  
  69.  
  70. currentPlayer = (currentPlayer == PLAYER1)
  71.  
  72.  
  73.  
  74. ? PLAYER2
  75. : PLAYER1;
  76.  
  77.  
  78.  
  79.  
  80. }
  81. public boolean hasWon(String player){
  82. if(player.equals(cells[0].getText())
  83. && player.equals(cells[1].getText())
  84. && player.equals(cells[2].getText())
  85. return true;
  86.  
  87. }
  88.  
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement