Advertisement
Guest User

Untitled

a guest
May 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. package tictactoe;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Font;
  5. import java.awt.GridLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JOptionPane;
  11.  
  12. public class TicTacToe extends JFrame implements ActionListener {
  13.  
  14. private JButton[] cells;
  15. private final String PLAYER1 = "X";//final le constant change garna didaina
  16. private final String PLAYER2 = "O";
  17. private String currentPlayer = PLAYER1;
  18. private boolean isGameOver = false;
  19.  
  20. public TicTacToe()//Constructor
  21. {
  22. setLayout(new GridLayout(3, 3));
  23. cells = new JButton[9];
  24. for (int i = 0; i < cells.length; i++) {
  25. cells[i] = new JButton();
  26. cells[i].setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 50));
  27. cells[i].setPreferredSize(new Dimension(100, 100));
  28. cells[i].addActionListener(this);
  29. add(cells[i]);
  30. pack();
  31. }
  32. }
  33.  
  34. public static void main(String[] args) {
  35.  
  36. {
  37. TicTacToe frame = new TicTacToe();
  38. frame.setVisible(true);
  39. frame.setDefaultCloseOperation(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();//e in typecasted to jbutton type
  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.  
  57. return;
  58. }
  59. currentPlayer = currentPlayer.equals(PLAYER1)
  60. ? PLAYER2
  61. : PLAYER1;
  62.  
  63. }
  64.  
  65. public boolean hasWon(String player) {
  66. if (player.equals(cells[0].getText()) && player.equals(cells[1].getText()) && player.equals(cells[2].getText())) {
  67. return true;
  68. }
  69. if (player.equals(cells[3].getText()) && player.equals(cells[4].getText()) && player.equals(cells[5].getText())) {
  70. return true;
  71. }
  72. if (player.equals(cells[6].getText()) && player.equals(cells[7].getText()) && player.equals(cells[8].getText())) {
  73. return true;
  74. }
  75. if (player.equals(cells[0].getText()) && player.equals(cells[3].getText()) && player.equals(cells[6].getText())) {
  76. return true;
  77. }
  78. if (player.equals(cells[1].getText()) && player.equals(cells[4].getText()) && player.equals(cells[7].getText())) {
  79. return true;
  80. }
  81. if (player.equals(cells[2].getText()) && player.equals(cells[5].getText()) && player.equals(cells[8].getText())) {
  82. return true;
  83. }
  84. if (player.equals(cells[0].getText()) && player.equals(cells[4].getText()) && player.equals(cells[8].getText())) {
  85. return true;
  86. }
  87. if (player.equals(cells[2].getText()) && player.equals(cells[4].getText()) && player.equals(cells[6].getText())) {
  88. return true;
  89. }
  90. return false;
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement