【发布时间】:2018-08-15 15:34:48
【问题描述】:
我正在开发一款简单的 tic-tac-toe 4x4 游戏。我有 input() 函数,用户在其中输入他的数据, printBoard() 打印板本身,analyzeBoard() 用于选择获胜者的所有计算和 done() 仅返回状态。我的问题是,即使我找到了获胜者,我也无法停止 input() 的工作,并且它一遍又一遍地保持印刷板。第一次,我认为问题出在analyzeBoard() 中,但后来我发现它实际上返回了我需要的所有内容,而问题出在input() 中。当我尝试从输入中返回时,它要么说缺少返回语句要么只是不会停止,即使它可以看到获胜者。
public char input ()
{
while (status == false || numberOfMovesLeft > 0)
{
System.out.println("User "+ whoseTurn + " enter your move: "); //prompt user to make a move
char userInput = reader.next().charAt(0); // local variable to hold user input
// validation of user input
while (userInput != 'a' && userInput != 'b' && userInput != 'c'
&& userInput != 'd' && userInput != 'e' && userInput != 'f'
&& userInput != 'g' && userInput != '1' && userInput != '2'
&& userInput != '3' && userInput != '5' && userInput != '6'
&& userInput != '6' && userInput != '7' && userInput != '8'
&& userInput != '9')
{
System.out.println("Try again, user "+ whoseTurn + " enter your move: ");
userInput = reader.next().charAt(0);
}
// places user input into a board cell
for (int row = 0; row < board.length; row++)
{
for (int col = 0; col < board[row].length; col++)
{
if (board[row][col] == userInput)
{
board[row][col] = whoseTurn;
}
}
}
// check for tie result
numberOfMovesLeft--;
if (numberOfMovesLeft==0)
{
System.out.println("Tie!");
winner = 'T';
System.out.println(winner);
}
analyzeBoard();
printBoard();
done();
whoseTurn = (whoseTurn == 'X') ? '0' : 'X';
}
return winner;
}
public void analyzeBoard()
{
// row winner algoritm
for (int row = 0; row <=3; row++)
{
for (int col = 0; col < 2; col++)
{
if (board[row][col] == board[row][col + 1]
&&
board[row][col] == board[row][col + 2])
{
System.out.println("winner");
winner = whoseTurn;
System.out.println("winner "+ winner);
System.out.println("whoseTurn "+ whoseTurn);
status = true;
System.out.println("status "+status);
done();
}
else
{
//System.out.println("status "+status);
}
}
}
//column winner algoritm
for (int row = 0; row <2; row++)
{
for (int col = 0; col <= 3; col++)
{
if (board[row][col] == board[row+1][col] &&
board[row][col] == board[row+2][col])
{
System.out.println("winner");
winner = whoseTurn;
System.out.println("winner "+ winner);
System.out.println("whoseTurn "+ whoseTurn);
status = true;
//System.out.println("status "+status);
}
}
}
//diagonal winner algoritm
for (int row = 0; row <2; row++)
{
for (int col = 0; col <2; col++)
{
if (board[row][col] == board[row+1][col+1] &&
board[row][col] == board[row+2][col+2])
{
System.out.println("winner");
winner = whoseTurn;
System.out.println("winner "+ winner);
System.out.println("whoseTurn "+ whoseTurn);
status = true;
//System.out.println("status "+status);
}
}
}
//diagonal winner algoritm
for (int row = 0; row <2; row++)
{
for (int col = 2; col <=3; col++)
{
if (board[row][col] == board[row+1][col-1])
{
System.out.println("winner");
winner = whoseTurn;
System.out.println("winner "+ winner);
System.out.println("whoseTurn "+ whoseTurn);
status = true;
//System.out.println("status "+status);
}
}
}
}
【问题讨论】:
-
Matex 是正确的,但正如我所见,您的代码中可能存在更多问题。如果您能解释得更多,我将能够为您提供帮助
-
只有在有赢家且没有更多动作的情况下才会退出。这就是你在 while 条件下的意思吗?
标签: java function while-loop