如何判断哪个玩家获胜

时间:2018-04-27 14:12:36

标签: java jpanel

我需要让游戏说哪个玩家赢了。现在它说游戏已经结束但是,我不确定如何让它说哪个玩家赢了?我假设我需要比较放入每个按钮的字符串,但我不知道该怎么做。我知道播放播放器1获胜的文字不正确。只有暂时才能让它正常工作。

    // Array for the buttons.
    JButton buttons[] = new JButton[9];

    // if it is the first, third, fifth etc... then it is an x. if it is second,
    // fourth sixth etc... it is an o
    int alternate = 0;

    // This sets up the buttons and adds them to the pane. It sets the font, size,
    // and stes the default button as nothing.
    public void buttonSetup() {
        for (int j = 0; j <= 8; j++) {
            buttons[j] = new JButton();
            buttons[j].setText("");
            buttons[j].setFont(new Font("Arial", Font.PLAIN, 150));
            buttons[j].addActionListener(new buttonListener());

            add(buttons[j]);

        }
    }

    // Sets the grid size to be used and adds the buttons to it.
    public TicTacToe() {
        setLayout(new GridLayout(3, 3));
        buttonSetup();
    }

    // This starts the following methods after a button is clicked and reacts to the
    // buttons being pressed.
    private class buttonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            // This takes the input for which button is pressed and sets the display to
            // either X or O.
            JButton pressed = (JButton) e.getSource();
            if (alternate % 2 == 0)
                pressed.setText("X");
            else
                pressed.setText("O");

            // This calls winCheck to see if somebody won, if they did it displays the text.
            if (winCheck() == true) {
                JOptionPane.showConfirmDialog(null, "The End Player 1 wins");

                newGame();
            }
            alternate++;
        }

        // This is the method that checks to see which symbols are on which button to
        // see who won.
        public boolean winCheck() {

            // This looks at the inputs in a horizontal line to see if they won.
            if (sideCheck(0, 1) && sideCheck(1, 2))
                return true;
            else if (sideCheck(3, 4) && sideCheck(4, 5))
                return true;
            else if (sideCheck(6, 7) && sideCheck(7, 8))
                return true;

            // This looks at the inputs in a vertical line to see if they won.
            else if (sideCheck(0, 3) && sideCheck(3, 6))
                return true;
            else if (sideCheck(1, 4) && sideCheck(4, 7))
                return true;
            else if (sideCheck(2, 5) && sideCheck(5, 8))
                return true;

            // This looks at the inputs in a diagonal line to see if they won.
            else if (sideCheck(0, 4) && sideCheck(4, 8))
                return true;
            else if (sideCheck(2, 4) && sideCheck(4, 6))
                return true;
            else
                return false;
        }

        // This method sets the game board to a new one by seting all the buttons to
        // nothing.
        public void newGame() {
            for (int i = 0; i <= 8; i++) {
                buttons[i].setText("");
            }
        }

        // This is the method that allows winCheck to see if the symbols are the same.
        public boolean sideCheck(int x, int y) {

            if (buttons[x].getText().equals(buttons[y].getText()) && !buttons[x].getText().equals(""))

                return true;
            else
                return false;
        }
    }

}

I need to make it so the buttons are only clickable one time. I am unsure how to make it say which player won? Thanks

1 个答案:

答案 0 :(得分:0)

目前sideCheckwinCheck仅返回布尔值。因此,返回的唯一信息是,是否有人获胜,而不是谁获胜。我建议你改变这些方法,以便他们返回更多信息。例如,winCheck可以返回一个Int,其中0表示没有人赢,1表示玩家1赢,2表示玩家2赢。这只是一种方式 - 您可以按照自己喜欢的方式表示信息。