gpt4 book ai didi

java - TicTacToe Java 游戏没有结束

转载 作者:行者123 更新时间:2023-12-02 07:26:44 25 4
gpt4 key购买 nike

运行此代码时,游戏永远不会宣布获胜者或平局,它会不断要求玩家进行下一个 Action ...

示例输出:

Choose a position to play
1 | 2 | 3
---------
4 | 5 | 6
---------
7 | 8 | 9
Player X (Enter a position or -1 to resign): 1

Choose a position to play.
1 | 2 | 3
---------
4 | 5 | 6
---------
7 | 8 | 9
Player O (Enter a position or -1 to resign): 1

它不会改变角色的空格数,甚至不会记住它来宣布获胜者。

这是我代码的 runStandardGame() 部分,我认为问题出在哪里?

//***RUN STANDARD GAME******************************************************

static void runStandardGame() {

int position = QUIT;
char playerChar = 'X';
boolean isGameOver = false;

fillPosition();

System.out.println("Choose a position to play.\n");


displayGrid();

do {
System.out.print("Player " + playerChar +
" (Enter a position or " + QUIT + " to resign): ");
position = keyboard.nextInt();

System.out.println("Choose a position to play.\n");

displayGrid();


if(isWin()) {
System.out.print("\nPlayer " + playerChar + " WINS!!!\n");
isGameOver = true;
}
else if (isTie()) {
System.out.print("\nTIE.\n");
isGameOver = true;
}
else {
//switch players because one of the players has just played
//and not won;, so, it is the other player's turn
if (playerChar == 'X') {
playerChar = 'O';
}
else{
playerChar = 'X';
}
}
}while(!isGameOver && position != QUIT);

}//end of runStandardGame

或者甚至可能只是播放部分,因为这就是分配给数组的地方......

//***PLAY*******************************************************************    

static void play(int cell, char playerChar) {
//the player has entered a number 1 through 9 for the position they want
//to play, so, figure out which row and which column of the array this is
//For example, if the player wants to play 'X' in position 7, this means
//the 'X' must go into row 2, column 0 of the array
int row = 0;
int column = 0;

if (cell > 0 && cell <= (STANDARD_GRID_ROWS * STANDARD_GRID_COLUMNS)){
row = (cell - 1) / STANDARD_GRID_ROWS;
column = (cell - 1) % STANDARD_GRID_COLUMNS;
grid[row][column] = playerChar;
}
}

感谢任何帮助。谢谢。

最佳答案

我看到您正在获取用户输入

position = keyboard.nextInt();

但我没有看到你实际上通过调用来使用它

play(position, playerChar);

关于java - TicTacToe Java 游戏没有结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13484530/

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