Advertisement
Guest User

Untitled

a guest
May 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import javax.swing.*;
  8.  
  9.  
  10.  
  11. @SuppressWarnings("serial")
  12. public class NaughtsAndCrosses extends JPanel implements ActionListener {
  13.  
  14. private static final int ROWS = 3;
  15. private static final int COLS = 3;
  16.  
  17. private int _move=1;
  18. GameStatus currentStatus= GameStatus.CROSS_WON.IN_PROGRESS;
  19.  
  20.  
  21. private enum GameStatus { NAUGHT_WON, CROSS_WON, IN_PROGRESS, DRAW };
  22.  
  23. // Values to be used for button text within a board.
  24. private static String NAUGHT = "0";
  25. private static String CROSS = "X";
  26. private static String BLANK = " ";
  27.  
  28. // A 2 dimensional array of JButton objects. Each button is intended to
  29. // display the text NAUGHT, CROSS or BLANK (see above).
  30. private JButton[][] _board;
  31. private JButton _newGame;
  32. private static JFrame _game;
  33.  
  34. {
  35. _newGame = new JButton("New Game");
  36. _newGame.setPreferredSize(new Dimension(600,100));
  37. }
  38.  
  39.  
  40. public NaughtsAndCrosses() {
  41. // Build the GUI.
  42. buildGUI();
  43.  
  44. // TO DO: set up event handlers.
  45. for(int row = 0; row < ROWS; row++) {
  46. for(int col = 0; col < COLS; col++) {
  47. _board[row][col].addActionListener(this);
  48. }
  49. }
  50.  
  51. _newGame.addActionListener(new ActionListener() {
  52. @Override
  53. public void actionPerformed(ActionEvent e) {
  54. /*String[] args= new String[0];
  55. main(args);*/
  56. _game.dispose();
  57. _game= new JFrame();
  58. _game.add(new NaughtsAndCrosses());
  59. _game.pack();
  60. _game.setVisible(true);
  61. }
  62. });
  63.  
  64. }
  65.  
  66. private void buildGUI() {
  67. // Initialise the board.
  68. _board = new JButton[ROWS][COLS];
  69.  
  70. for(int row = 0; row < ROWS; row++) {
  71. for(int col = 0; col < COLS; col++) {
  72. _board[row][col] = new JButton(BLANK);
  73. _board[row][col].setPreferredSize(new Dimension(200,200));
  74. }
  75. }
  76.  
  77. // TO DO: create other GUI components and lay them out as appropriate.
  78. // Note that _board only stores 9 JButton objects using a 3x3 array.
  79. // The buttons still need to be added to the GUI - use GridLayout to
  80. // add and layout the JButtons on a JPanel.
  81.  
  82. JPanel mainPanel= new JPanel(new BorderLayout());
  83. JPanel topGrid= new JPanel(new GridLayout(ROWS,COLS));
  84. JPanel bottomPanel= new JPanel();
  85.  
  86.  
  87. add(mainPanel);
  88.  
  89. mainPanel.add(topGrid, BorderLayout.CENTER);
  90. mainPanel.add(bottomPanel,BorderLayout.SOUTH);
  91.  
  92.  
  93.  
  94.  
  95. for(int row = 0; row < ROWS; row++) {
  96. for(int col = 0; col < COLS; col++) {
  97. topGrid.add(_board[row][col]);
  98. }
  99. }
  100.  
  101. bottomPanel.add(_newGame);
  102.  
  103.  
  104. }
  105.  
  106.  
  107. public static void main(String[] args) {
  108. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  109. @Override
  110. public void run() {
  111. // TO DO: Create and configure a JFrame to display the GUI.
  112. // Make class NaughtsAndCrosses extend JPanel so that a
  113. // NaughtsAndCrosses object can be added to the JFrame.
  114. _game= new JFrame();
  115. _game.add(new NaughtsAndCrosses());
  116. _game.pack();
  117. _game.setVisible(true);
  118.  
  119.  
  120. }
  121. });
  122. }
  123.  
  124. // Helper method to determine the status of a Naughts and Crosses game.
  125. // This method processes the 2 dimensional array of JButton objects, and
  126. // uses the text value (NAUGHT, CROSS or BLANK) of each JButton to
  127. // calculate the game's state of play.
  128. private GameStatus getGameStatus() {
  129. GameStatus status = GameStatus.DRAW;
  130.  
  131. List<String> lines = new ArrayList<String>();
  132.  
  133. // Top row
  134. lines.add(_board[0][0].getText() + _board[0][1].getText() + _board[0][2].getText());
  135.  
  136. // Middle row
  137. lines.add(_board[1][0].getText() + _board[1][1].getText() + _board[1][2].getText());
  138.  
  139. // Bottom row
  140. lines.add(_board[2][0].getText() + _board[2][1].getText() + _board[2][2].getText());
  141.  
  142. // Left col
  143. lines.add(_board[0][0].getText() + _board[1][0].getText() + _board[2][0].getText());
  144.  
  145. // Middle col
  146. lines.add(_board[0][1].getText() + _board[1][1].getText() + _board[2][1].getText());
  147.  
  148. // Right col
  149. lines.add(_board[0][2].getText() + _board[1][2].getText() + _board[2][2].getText());
  150.  
  151. // Diagonals
  152. lines.add(_board[0][0].getText() + _board[1][1].getText() + _board[2][2].getText());
  153. lines.add(_board[0][2].getText() + _board[1][1].getText() + _board[2][0].getText());
  154.  
  155. // Check to see if there's any cell without a NAUGHT or a CROSS.
  156. for(String line : lines) {
  157. System.out.println("Looking at line: |" + line + "|");
  158. if(line.contains(BLANK)) {
  159. status = GameStatus.IN_PROGRESS;
  160. System.out.println("In progress");
  161. break;
  162. }
  163. }
  164.  
  165. // Check to see if there's any line of NAUGHTs or CROSSes.
  166. if(lines.contains(CROSS + CROSS + CROSS)) {
  167. status = GameStatus.CROSS_WON;
  168. } else if (lines.contains(NAUGHT + NAUGHT + NAUGHT)) {
  169. status = GameStatus.NAUGHT_WON;
  170. }
  171.  
  172. return status;
  173. }
  174.  
  175. @Override
  176. public void actionPerformed(ActionEvent e) {
  177. if(currentStatus == GameStatus.IN_PROGRESS) {
  178. JButton button = (JButton) e.getSource();
  179. if(_move % 2 == 0) {
  180. button.setText(CROSS);
  181. } else {
  182. button.setText(NAUGHT);
  183. }
  184. _move++;
  185. } else {
  186. System.out.println(currentStatus);
  187. }
  188. currentStatus = getGameStatus();
  189. }
  190.  
  191.  
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement