TicTacToe检查抽奖

时间:2016-04-01 17:50:57

标签: java user-interface tic-tac-toe

使用JButtons编写了一个带有GUI的TicTacToe游戏。以下是用于检查是否有赢家的代码。还包括我的代码来检查平局。但是,在点击所有按钮并且最后一次移动导致其中一个玩家获胜的事件中,由于点击了所有按钮,它仍被视为平局。如何修复它以便准确检查抽奖?

public void checkWin(char[] values)
{
    //Check if player 1 (xs) win
    if ((values[0] == 'x') && (values[1] == 'x') && (values[2] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    else if ((values[3] == 'x') && (values[4] == 'x') && (values[5] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    else if ((values[6] == 'x') && (values[7] == 'x') && (values[8] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    else if ((values[0] == 'x') && (values[4] == 'x') && (values[8] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    else if ((values[2] == 'x') && (values[4] == 'x') && (values[6] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    else if ((values[0] == 'x') && (values[3] == 'x') && (values[6] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    else if ((values[1] == 'x') && (values[4] == 'x') && (values[7] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    else if ((values[2] == 'x') && (values[5] == 'x') && (values[8] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    // check for draw
    else if (((values[0] == 'x') || (values[0] == 'o')) && ((values[1] == 'x') || (values[1] == 'o')) &&
    ((values[2] == 'x') || (values[2] == 'o')) && ((values[3] == 'x') || (values[3] == 'o')) && ((values[4] == 'x') || (values[4] == 'o'))
    && ((values[5] == 'x') || (values[5] == 'o')) && ((values[6] == 'x') || (values[6] == 'o')) && ((values[7] == 'x') || (values[7] == 'o'))
    && ((values[8] == 'x') || (values[8] == 'o')))
    {
        playerLabel.setText("DRAW!");
        disableButtons();
    }

    //Check is player 2 (os) wins
    if ((values[0] == 'o') && (values[1] == 'o') && (values[2] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    else if ((values[3] == 'o') && (values[4] == 'o') && (values[5] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    else if ((values[6] == 'o') && (values[7] == 'o') && (values[8] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    else if ((values[0] == 'o') && (values[4] == 'o') && (values[8] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    else if ((values[2] == 'o') && (values[4] == 'o') && (values[6] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    else if ((values[0] == 'o') && (values[3] == 'o') && (values[6] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    else if ((values[1] == 'o') && (values[4] == 'o') && (values[7] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    else if ((values[2] == 'o') && (values[5] == 'o') && (values[8] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    // check for draw
    else if (((values[0] == 'x') || (values[0] == 'o')) && ((values[1] == 'x') || (values[1] == 'o')) &&
    ((values[2] == 'x') || (values[2] == 'o')) && ((values[3] == 'x') || (values[3] == 'o')) && ((values[4] == 'x') || (values[4] == 'o'))
    && ((values[5] == 'x') || (values[5] == 'o')) && ((values[6] == 'x') || (values[6] == 'o')) && ((values[7] == 'x') || (values[7] == 'o'))
    && ((values[8] == 'x') || (values[8] == 'o')))
    {
        playerLabel.setText("DRAW!");
        disableButtons();
    }
}

3 个答案:

答案 0 :(得分:0)

只需检查所有获胜方案,然后在最后<Msg> <Can>0</Can> <MsgName>Frm0x111</MsgName> ... </Msg> 语句中,它应该是一个平局......

else

另一方面,您可以通过创建泛型方法并将适当的参数传递给它来真正缩短您的代码。这段代码非常麻烦。

答案 1 :(得分:0)

你可以使用一些函数和循环:

public void checkGameOver(char[] values) {
    if (checkWin(values, 'x')) {
        playerLabel.setText("Player 1 wins!");
    } else if (checkWin(values, 'o')) {
        playerLabel.setText("Player 2 wins!");
    } else if (checkDraw(values)) {
        playerLabel.setText("DRAW");
    } else {
        return;
    }

    disableButtons();
}

public boolean checkWins(char[] values, char player) {
    if (((values[0] == player) && (values[1] == player) && (values[2] == player)) ||
        ((values[3] == player) && (values[4] == player) && (values[5] == player)) ||
        ((values[6] == player) && (values[7] == player) && (values[8] == player)) ||
        ((values[0] == player) && (values[4] == player) && (values[8] == player)) ||
        ((values[2] == player) && (values[4] == player) && (values[6] == player)) ||
        ((values[0] == player) && (values[3] == player) && (values[6] == player)) || 
        ((values[1] == player) && (values[4] == player) && (values[7] == player)) ||
        ((values[2] == player) && (values[5] == player) && (values[8] == player)))
    {
        return true;
    }

    return false;
}

public boolean checkDraw(char[] values) {
    for (char c : values) { // check if each box holds either an 'x' or an 'o'
        if (c != 'o' && c != 'x') {
            return false;
        }
    }

    return true;
}

答案 2 :(得分:0)

您可以通过以下方式执行此操作:

1 - 您使用String文本变量来存储需要在播放器标签上设置的文本。

2 - 在每个if上,你根据需要设置的文本设置文本变量,例如在玩家1获胜的ifs上,你输入text =&#34; PLAYER 1 WINS&#34;。< / p>

3 - 您只在方法结束时设置一次播放器标签,而不是在每个&#34;中设置播放器标签,如果&#34;当你加上上面的代码时。

4-使用两个布尔变量,如boolean player1 = false,player2 = false。当player1获胜时,其设置为true,否则为false。这同样适用于player2。换句话说,如果玩家1获胜,则每个玩家都获胜,你将玩家1 =真。

5-最后,你只需要检查,如果player1和player2变量都为真,那么它的绘制。 (并且不要忘记设置变量text =&#34; DRAW&#34;)。