Java 我的android tic-tac-toe游戏中的checkForWin()方法一直在错误放置积分
我的tic-tac-toe游戏中的checkForWin方法应该检查当前玩家并给予该玩家胜利的积分,但它一直给予错误的玩家积分,特别是当numOfPlayers条件成立时。玩家一赢得了比赛,但玩家二获得了胜利的积分,我已经检查确保文本设置为按钮后立即调用checkForWin方法,然后再交换当前玩家,但问题仍然存在。我需要帮助。这不是全部代码,只是我觉得相关的部分Java 我的android tic-tac-toe游戏中的checkForWin()方法一直在错误放置积分,java,android,tic-tac-toe,Java,Android,Tic Tac Toe,我的tic-tac-toe游戏中的checkForWin方法应该检查当前玩家并给予该玩家胜利的积分,但它一直给予错误的玩家积分,特别是当numOfPlayers条件成立时。玩家一赢得了比赛,但玩家二获得了胜利的积分,我已经检查确保文本设置为按钮后立即调用checkForWin方法,然后再交换当前玩家,但问题仍然存在。我需要帮助。这不是全部代码,只是我觉得相关的部分 @Override public void onClick(View v) { if (!((Butto
@Override
public void onClick(View v) {
if (!((Button) v).getText().toString().equals("")) {
return;
}
if (numOfPlayers){
do {
((Button) v).setText("X");
checkForWin();
player1Turn = !player1Turn;
}
while (player1Turn);
ComputerPlays();
}
else {
if (player1Turn) {
((Button) v).setText("X");
} else {
((Button) v).setText("O");
}
}
roundCount++;
if (checkForWin()) {
if (player1Turn) {
player1Wins();
} else {
player2Wins();
}
} else if (roundCount == 9) {
draw();
} else {
player1Turn = !player1Turn;
}
}
private void ComputerPlays()
{
Random rand;
rand = new Random();
if (roundCount!=9) {
int i;
int j;
do {
i = rand.nextInt( 2 );
j = rand.nextInt(2);
}while (!game_buttons[i][j].getText().toString().equals(""));
game_buttons[i][j].setText("O");
}
}
我知道,因为您在checkforwin之后交换了playerturn,您需要使用:
if (checkForWin()) {
if (!player1Turn) {
player1Wins();
} else {
player2Wins();
}
}
好的,谢谢@Albanniou,但是你的解决方案正好扭转了问题,并且不管谁赢,只给一个玩家积分。