作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 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/
我是一名优秀的程序员,十分优秀!