Advertisement
Guest User

Untitled

a guest
May 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. package tictactoe;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.FlowLayout;
  5. import java.awt.Font;
  6. import java.awt.GridLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JOptionPane;
  12.  
  13. public class TicTacToe extends JFrame implements ActionListener {
  14.  
  15. private JButton[] cells;
  16. private final String PLAYER1 = "X";
  17. private final String PLAYER2 = "O";
  18. private String currentPlayer = PLAYER1;
  19.  
  20. private boolean isGameOver = false;
  21.  
  22. public TicTacToe() {
  23. setLayout(new GridLayout(3, 3));
  24. cells = new JButton[9];
  25. for (int i = 0; i < cells.length; i++) {
  26. cells[i] = new JButton();
  27. cells[i].setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 50));
  28. cells[i].setPreferredSize(new Dimension(100, 100));
  29. cells[i].addActionListener(this);
  30. add(cells[i]);
  31. }
  32. pack();
  33.  
  34. }
  35.  
  36. public static void main(String[] args) {
  37. TicTacToe ttt = new TicTacToe();
  38. ttt.setVisible(true);
  39. ttt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40.  
  41. }
  42.  
  43. @Override
  44. public void actionPerformed(ActionEvent e) {
  45. if (isGameOver) {
  46. return;
  47. }
  48. JButton currentCell = (JButton) e.getSource();
  49. if (currentCell.getText().length() > 0) {
  50. return;
  51. }
  52. currentCell.setText(currentPlayer);
  53. if (hasWon(currentPlayer)) {
  54. isGameOver = true;
  55. JOptionPane.showMessageDialog(this, currentPlayer + "has won");
  56. return;
  57. }
  58. currentPlayer = currentPlayer.equals(PLAYER1) ? PLAYER2 : PLAYER1;
  59.  
  60. }
  61.  
  62. public boolean hasWon(String player) {
  63. if (player.equals(cells[0].getText())
  64. && player.equals(cells[1].getText())
  65. && player.equals(cells[2].getText())) {
  66. return true;
  67. }
  68. if (player.equals(cells[3].getText())
  69. && player.equals(cells[4].getText())
  70. && player.equals(cells[5].getText())) {
  71. return true;
  72. }
  73. if (player.equals(cells[6].getText())
  74. && player.equals(cells[7].getText())
  75. && player.equals(cells[8].getText())) {
  76. return true;
  77. }
  78. if (player.equals(cells[0].getText())
  79. && player.equals(cells[3].getText())
  80. && player.equals(cells[6].getText())) {
  81. return true;
  82. }
  83. if (player.equals(cells[1].getText())
  84. && player.equals(cells[4].getText())
  85. && player.equals(cells[7].getText())) {
  86. return true;
  87. }
  88. if (player.equals(cells[3].getText())
  89. && player.equals(cells[5].getText())
  90. && player.equals(cells[8].getText())) {
  91. return true;
  92. }
  93. if (player.equals(cells[0].getText())
  94. && player.equals(cells[4].getText())
  95. && player.equals(cells[8].getText())) {
  96. return true;
  97. }
  98. if (player.equals(cells[2].getText())
  99. && player.equals(cells[4].getText())
  100. && player.equals(cells[6].getText())) {
  101. return true;
  102. }
  103. return false;
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement