Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.*;
- @SuppressWarnings("serial")
- public class NaughtsAndCrosses extends JPanel implements ActionListener {
- private static final int ROWS = 3;
- private static final int COLS = 3;
- private int _move=1;
- GameStatus currentStatus= GameStatus.CROSS_WON.IN_PROGRESS;
- private enum GameStatus { NAUGHT_WON, CROSS_WON, IN_PROGRESS, DRAW };
- // Values to be used for button text within a board.
- private static String NAUGHT = "0";
- private static String CROSS = "X";
- private static String BLANK = " ";
- // A 2 dimensional array of JButton objects. Each button is intended to
- // display the text NAUGHT, CROSS or BLANK (see above).
- private JButton[][] _board;
- private JButton _newGame;
- private static JFrame _game;
- {
- _newGame = new JButton("New Game");
- _newGame.setPreferredSize(new Dimension(600,100));
- }
- public NaughtsAndCrosses() {
- // Build the GUI.
- buildGUI();
- // TO DO: set up event handlers.
- for(int row = 0; row < ROWS; row++) {
- for(int col = 0; col < COLS; col++) {
- _board[row][col].addActionListener(this);
- }
- }
- _newGame.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- /*String[] args= new String[0];
- main(args);*/
- _game.dispose();
- _game= new JFrame();
- _game.add(new NaughtsAndCrosses());
- _game.pack();
- _game.setVisible(true);
- }
- });
- }
- private void buildGUI() {
- // Initialise the board.
- _board = new JButton[ROWS][COLS];
- for(int row = 0; row < ROWS; row++) {
- for(int col = 0; col < COLS; col++) {
- _board[row][col] = new JButton(BLANK);
- _board[row][col].setPreferredSize(new Dimension(200,200));
- }
- }
- // TO DO: create other GUI components and lay them out as appropriate.
- // Note that _board only stores 9 JButton objects using a 3x3 array.
- // The buttons still need to be added to the GUI - use GridLayout to
- // add and layout the JButtons on a JPanel.
- JPanel mainPanel= new JPanel(new BorderLayout());
- JPanel topGrid= new JPanel(new GridLayout(ROWS,COLS));
- JPanel bottomPanel= new JPanel();
- add(mainPanel);
- mainPanel.add(topGrid, BorderLayout.CENTER);
- mainPanel.add(bottomPanel,BorderLayout.SOUTH);
- for(int row = 0; row < ROWS; row++) {
- for(int col = 0; col < COLS; col++) {
- topGrid.add(_board[row][col]);
- }
- }
- bottomPanel.add(_newGame);
- }
- public static void main(String[] args) {
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- // TO DO: Create and configure a JFrame to display the GUI.
- // Make class NaughtsAndCrosses extend JPanel so that a
- // NaughtsAndCrosses object can be added to the JFrame.
- _game= new JFrame();
- _game.add(new NaughtsAndCrosses());
- _game.pack();
- _game.setVisible(true);
- }
- });
- }
- // Helper method to determine the status of a Naughts and Crosses game.
- // This method processes the 2 dimensional array of JButton objects, and
- // uses the text value (NAUGHT, CROSS or BLANK) of each JButton to
- // calculate the game's state of play.
- private GameStatus getGameStatus() {
- GameStatus status = GameStatus.DRAW;
- List<String> lines = new ArrayList<String>();
- // Top row
- lines.add(_board[0][0].getText() + _board[0][1].getText() + _board[0][2].getText());
- // Middle row
- lines.add(_board[1][0].getText() + _board[1][1].getText() + _board[1][2].getText());
- // Bottom row
- lines.add(_board[2][0].getText() + _board[2][1].getText() + _board[2][2].getText());
- // Left col
- lines.add(_board[0][0].getText() + _board[1][0].getText() + _board[2][0].getText());
- // Middle col
- lines.add(_board[0][1].getText() + _board[1][1].getText() + _board[2][1].getText());
- // Right col
- lines.add(_board[0][2].getText() + _board[1][2].getText() + _board[2][2].getText());
- // Diagonals
- lines.add(_board[0][0].getText() + _board[1][1].getText() + _board[2][2].getText());
- lines.add(_board[0][2].getText() + _board[1][1].getText() + _board[2][0].getText());
- // Check to see if there's any cell without a NAUGHT or a CROSS.
- for(String line : lines) {
- System.out.println("Looking at line: |" + line + "|");
- if(line.contains(BLANK)) {
- status = GameStatus.IN_PROGRESS;
- System.out.println("In progress");
- break;
- }
- }
- // Check to see if there's any line of NAUGHTs or CROSSes.
- if(lines.contains(CROSS + CROSS + CROSS)) {
- status = GameStatus.CROSS_WON;
- } else if (lines.contains(NAUGHT + NAUGHT + NAUGHT)) {
- status = GameStatus.NAUGHT_WON;
- }
- return status;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(currentStatus == GameStatus.IN_PROGRESS) {
- JButton button = (JButton) e.getSource();
- if(_move % 2 == 0) {
- button.setText(CROSS);
- } else {
- button.setText(NAUGHT);
- }
- _move++;
- } else {
- System.out.println(currentStatus);
- }
- currentStatus = getGameStatus();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement