如何为我的Tic Tac Toe GUI游戏制作正确的重启按钮?

时间:2015-12-07 20:04:20

标签: java swing

我目前有一个重启按钮,只有在第一个游戏结束后才能正确重启。在第二场比赛之后击中重启按钮会清除棋盘,但是当用户去绘制一个" O"或者" X",他们不被吸引到董事会。我真的想要一个功能齐全的重启按钮,但我不确定如何创建它。

// JFrame for klTicTacToe board.
public class GameClass extends JFrame {
// Indicate whose turn it is
private char whoseTurn = 'X';

// Cell grid 9 cells
private Cell[][] cells = new Cell[3][3];

// status label
JLabel introLabel = new JLabel("Welcome to Tic Tac Toe! X Goes First");
// restart button
JButton restart = new JButton("Restart");

// Game constructor
public GameClass() {

    // Panel to hold cells
    JPanel panel = new JPanel(new GridLayout(3, 3, 0, 0));
    for (int i = 0; i < 3; i++)
        for (int f = 0; f < 3; f++)
            panel.add(cells[i][f] = new Cell());

    panel.setBorder(new LineBorder(Color.BLACK, 5));
    introLabel.setBorder(new LineBorder(Color.BLACK, 2));
    introLabel.setFont(new Font("Times New Roman", Font.PLAIN, 20));
    introLabel.setForeground(Color.DARK_GRAY);

    restart.setBackground(Color.GREEN);
    // Restart button action listener
    restart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == restart) {

                panel.removeAll();
                // re establishes cell layout
                // Panel to hold cells
                JPanel panel = new JPanel(new GridLayout(3, 3, 0, 0));
                for (int i = 0; i < 3; i++)
                    for (int f = 0; f < 3; f++)
                        panel.add(cells[i][f] = new Cell());

                panel.setBorder(new LineBorder(Color.BLACK, 5));
                introLabel.setBorder(new LineBorder(Color.BLACK, 2));
                introLabel.setFont(new Font("Times New Roman", Font.PLAIN, 20));
                introLabel.setForeground(Color.DARK_GRAY);

                add(panel, BorderLayout.CENTER);
                add(introLabel, BorderLayout.NORTH);
                add(restart, BorderLayout.SOUTH);

                introLabel.setText("New Game! X goes First");

            }
        }
    });

    add(panel, BorderLayout.CENTER);
    add(introLabel, BorderLayout.NORTH);
    add(restart, BorderLayout.SOUTH);
}

// Determines if True, if game board is full. Otherwise, false.
public boolean isFull() {
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            if (cells[i][j].getToken() == ' ')
                return false;
    return true;
}

// Determines if a given token has won.

// Token to test for winning True, if the token has won. Otherwise, false.

public boolean isWon(char gameToken) {
    // check rows
    for (int i = 0; i < 3; i++)
        if ((cells[i][0].getToken() == gameToken) && (cells[i][1].getToken() == gameToken)
                && (cells[i][2].getToken() == gameToken)) {
            return true;
        }

    // check columns
    for (int j = 0; j < 3; j++)
        if ((cells[0][j].getToken() == gameToken) && (cells[1][j].getToken() == gameToken)
                && (cells[2][j].getToken() == gameToken)) {
            return true;
        }
    // check diagonal
    if ((cells[0][0].getToken() == gameToken) && (cells[1][1].getToken() == gameToken)
            && (cells[2][2].getToken() == gameToken)) {
        return true;
    }

    if ((cells[0][2].getToken() == gameToken) && (cells[1][1].getToken() == gameToken)
            && (cells[2][0].getToken() == gameToken)) {
        return true;
    }

    return false;
}

其余代码:

// Defines a cell in a TicTacToe game board.
public class Cell extends JPanel 
// token of this cell
private char token = ' ';

    // Cell constructor
    public Cell() {
        setBorder(new LineBorder(Color.black, 5));
        addMouseListener(new MyMouseListener());
    }

    // Gets the token of the cell.
    public char getToken() {
        return token;
    }

    // Sets the token of the cell.
    // Character to use as token value.
    public void setToken(char f) {
        token = f;
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        // Draws "X" and "O"
        if (token == 'X') {
            Graphics2D g2 = (Graphics2D) g;
            g.setColor(Color.BLUE);
            g2.setStroke(new BasicStroke(5));
            g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
            g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);

        }

        else if (token == 'O') {
            Graphics2D g2 = (Graphics2D) g;
            g.setColor(Color.RED);
            g2.setStroke(new BasicStroke(5));
            g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
        }
    }

    private class MyMouseListener extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {

            // if the cell is empty and the game is not over
            if (token == ' ' && whoseTurn != ' ')
                setToken(whoseTurn);

            // Check game status
            if (isWon(whoseTurn)) {
                introLabel.setText(whoseTurn + " has won! Game over! Press Restart to Play Again");
                whoseTurn = 'X';

            } else if (isFull()) {
                introLabel.setText("Tie game! Game over! Press Restart to Play Again");
                whoseTurn = ' ';

            } else {
                whoseTurn = (whoseTurn == 'X') ? 'O' : 'X';
                introLabel.setText(whoseTurn + "'s turn.");
            }
        }
    }
}

// Driver Class for Tic Tac Toe
public class TicTacToeGame {
    public void main(String[] args) {
        JFrame ticTacToe = new GameClass();
        ticTacToe.setTitle("TicTacToe Game");
        ticTacToe.setSize(700, 700);
        ticTacToe.setLocationRelativeTo(null);
        ticTacToe.setVisible(true);
        ticTacToe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

}

3 个答案:

答案 0 :(得分:1)

您正在运行时删除和添加组件。您拨打revalidate()的电话在哪里?试试以下内容:

        if (e.getSource() == restart) {

            panel.removeAll();
            // re establishes cell layout
            // Panel to hold cells
            JPanel panel = new JPanel(new GridLayout(3, 3, 0, 0));
            for (int i = 0; i < 3; i++)
                for (int f = 0; f < 3; f++)
                    panel.add(cells[i][f] = new Cell());

            panel.setBorder(new LineBorder(Color.BLACK, 5));
            introLabel.setBorder(new LineBorder(Color.BLACK, 2));
            introLabel.setFont(new Font("Times New Roman", Font.PLAIN, 20));
            introLabel.setForeground(Color.DARK_GRAY);

            add(panel, BorderLayout.CENTER);
            add(introLabel, BorderLayout.NORTH);
            add(restart, BorderLayout.SOUTH);

            introLabel.setText("New Game! X goes First");
            revalidate();  // <-- add this
        }

答案 1 :(得分:0)

尝试重新启动操作只需重置电路板上的9个单元格,然后重置标签 如果您有任何其他状态&#34;变量 - 它们也应该重新初始化 我不会打电话给panel.removeAll();

答案 2 :(得分:0)

好的,所以我才弄明白。通过在我的构造函数中抛出setUpBoard方法,我能够将构建内部抛出,然后在重启按钮内调用它。效果很好