gpt4 book ai didi

java - 我的 Android tic-tac-toe 游戏中的 checkForWin() 方法总是放错积分

转载 作者:行者123 更新时间:2023-12-02 11:21:14 25 4
gpt4 key购买 nike

我的 tic-tac-toe 游戏中的 checkForWin() 方法应该检查当前玩家并给予该玩家获胜的荣誉,但它总是给予错误的玩家荣誉,特别是当 numOfPlayers 条件成立时。玩家一赢得了游戏,但玩家二获得了胜利的荣誉,我已经检查以确保在文本设置为按钮后立即调用 checkForWin() 方法,然后当前玩家被交换,但问题仍然存在。我需要帮助。这不是全部代码,只是我认为相关的部分。

  @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 之后交换玩家回合,所以你需要使用:

if (checkForWin()) {
if (!player1Turn) {
player1Wins();
} else {
player2Wins();
}
}

关于java - 我的 Android tic-tac-toe 游戏中的 checkForWin() 方法总是放错积分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49906635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com