Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. /**
  2. @author Cosmin Baciu
  3. @date - 12/22/2015
  4. @description - Tic Tac Toe with GUI
  5. */
  6.  
  7. import java.awt.GridLayout;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import javax.swing.*;
  11.  
  12. public class TicTacToe extends JPanel
  13. {
  14. JButton buttons[] = new JButton[9];
  15. int alternate = 0;//if this number is a even, then put a X. If it's odd, then put an O
  16.  
  17. public TicTacToe()
  18. {
  19. setLayout(new GridLayout(3,3));
  20. initializebuttons();
  21. }
  22.  
  23. public void initializebuttons()
  24. {
  25. for(int i = 0; i <= 8; i++)
  26. {
  27. buttons[i] = new JButton();
  28. buttons[i].setText("");
  29. buttons[i].addActionListener(new buttonListener());
  30.  
  31. add(buttons[i]); //adds this button to JPanel (note: no need for JPanel.add(...)
  32. //because this whole class is a JPanel already
  33. }
  34. }
  35. public void resetButtons()
  36. {
  37. for(int i = 0; i <= 8; i++)
  38. {
  39. buttons[i].setText("");
  40. }
  41. }
  42.  
  43. // when a button is clicked, it generates an ActionEvent. Thus, each button needs an ActionListener. When it is clicked, it goes to this listener class that I have created and goes to the actionPerformed method. There (and in this class), we decide what we want to do.
  44. private class buttonListener implements ActionListener
  45. {
  46.  
  47. public void actionPerformed(ActionEvent e)
  48. {
  49.  
  50. JButton buttonClicked = (JButton)e.getSource(); //get the particular button that was clicked
  51. if(alternate%2 == 0)
  52. buttonClicked.setText("X");
  53. else
  54. buttonClicked.setText("O");
  55.  
  56. if(checkForWin() == true)
  57. {
  58. JOptionPane.showConfirmDialog(null, "Game Over.");
  59. resetButtons();
  60. }
  61.  
  62. alternate++;
  63.  
  64. }
  65.  
  66. public boolean checkForWin()
  67. {
  68. /** Reference: the button array is arranged like this as the board
  69. * 0 | 1 | 2
  70. * 3 | 4 | 5
  71. * 6 | 7 | 8
  72. */
  73. //horizontal win check
  74. if( checkAdjacent(0,1) && checkAdjacent(1,2) ) //no need to put " == true" because the default check is for true
  75. return true;
  76. else if( checkAdjacent(3,4) && checkAdjacent(4,5) )
  77. return true;
  78. else if ( checkAdjacent(6,7) && checkAdjacent(7,8))
  79. return true;
  80.  
  81. //vertical win check
  82. else if ( checkAdjacent(0,3) && checkAdjacent(3,6))
  83. return true;
  84. else if ( checkAdjacent(1,4) && checkAdjacent(4,7))
  85. return true;
  86. else if ( checkAdjacent(2,5) && checkAdjacent(5,8))
  87. return true;
  88.  
  89. //diagonal win check
  90. else if ( checkAdjacent(0,4) && checkAdjacent(4,8))
  91. return true;
  92. else if ( checkAdjacent(2,4) && checkAdjacent(4,6))
  93. return true;
  94. else
  95. return false;
  96.  
  97.  
  98. }
  99.  
  100. public boolean checkAdjacent(int a, int b)
  101. {
  102. if ( buttons[a].getText().equals(buttons[b].getText()) && !buttons[a].getText().equals("") )
  103. return true;
  104. else
  105. return false;
  106. }
  107.  
  108. }
  109.  
  110. public static void main(String[] args)
  111. {
  112. JFrame window = new JFrame("Tic-Tac-Toe");
  113. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  114. window.getContentPane().add(new TicTacToe());
  115. window.setBounds(300,200,300,300);
  116. window.setVisible(true);
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement