与gui得分的tic tac toe

时间:2017-05-24 14:50:45

标签: java string conditional tic-tac-toe

我们的教授问我们。在日食中制作游戏代码我们选择了tic tac toe但我无法让它正常播放。当我运行程序时,我仍然可以从x更改为o而不是获胜者。

这是我的代码:

private void winningGame()
{
    String b1 = btn1.getText();
    String b2 = btn2.getText();
    String b3 = btn3.getText();
    String b4 = btn4.getText();
    String b5 = btn5.getText();
    String b6 = btn6.getText();
    String b7 = btn7.getText();
    String b8 = btn8.getText();
    String b9 = btn9.getText();

    //////Player X Winning Combination//////

    if (b1==("X")&& b2==("X") && b3==("X"))
    {
        JOptionPane.showMessageDialog(null, "Player X wins","Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
        xCount++;
        gameScore();
    }
    if (b4==("X")&& b5==("X") && b6==("X"))
    {
        JOptionPane.showMessageDialog(null, "Player X wins","Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
        xCount++;
        gameScore();
    }
    if (b7==("X")&& b8==("X") && b9==("X"))
    {
        JOptionPane.showMessageDialog(null, "Player X wins","Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
        xCount++;
        gameScore();
    }
    if (b1==("X")&& b4==("X") && b7==("X"))
    {
        JOptionPane.showMessageDialog(null, "Player X wins","Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
        xCount++;
        gameScore();
    }
    if (b2==("X")&& b5==("X") && b8==("X"))
    {
        JOptionPane.showMessageDialog(null, "Player X wins","Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
        xCount++;
        gameScore();
    }
    if (b3==("X")&& b6==("X") && b9==("X"))
    {
        JOptionPane.showMessageDialog(null, "Player X wins","Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
        xCount++;
        gameScore();
    }

    ////////Player O Winning Combination//////////

    if (b1==("O")&& b2==("O") && b3==("O"))
    {
        JOptionPane.showMessageDialog(null, "Player O wins","Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
        oCount++;
        gameScore();
    }
    if (b4==("O")&& b5==("O") && b6==("O"))
    {
        JOptionPane.showMessageDialog(null, "Player O wins","Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
        oCount++;
        gameScore();
    }
    if (b7==("O")&& b8==("O") && b9==("O"))
    {
        JOptionPane.showMessageDialog(null, "Player O wins","Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
        oCount++;
        gameScore();
    }
    if (b1==("O")&& b4==("O") && b7==("O"))
    {
        JOptionPane.showMessageDialog(null, "Player O wins","Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
        oCount++;
        gameScore();
    }
    if (b2==("O")&& b5==("O") && b8==("O"))
    {
        JOptionPane.showMessageDialog(null, "Player O wins","Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
        oCount++;
        gameScore();
    }
    if (b3==("O")&& b6==("O") && b9==("O"))
    {
        JOptionPane.showMessageDialog(null, "Player O wins","Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
        oCount++;
        gameScore();
    }   

}

////////Methods////////
private void gameScore()
{
txtCountX.setText(String.valueOf(xCount++));
txtCountO.setText(String.valueOf(oCount++));
}

private void choosePlayer()
{
if(start.equalsIgnoreCase("X"))
{
start = "0";
}
else
{
start = "X";
}

}

从重置到其他功能的一切正常。唯一的问题是我无法获得胜利者以及结果或分数。并且调试器说它在这些代码行中。更准确的赢得游戏()。

0 个答案:

没有答案