我正在写这个井字游戏。我们都知道井字游戏是如何运作的,要么有人赢,要么没有人赢,然后棋盘就满了。
board.playable() 检查是否有人赢得了比赛或棋盘是否已满。这段代码运行良好,但我只是想知道是否有更简洁的方法来编写我的 while 循环。两次具有相同的条件似乎有点多余。但我需要在计算机移动之前运行 board.playable() 检查。
public void runGame()
{
while(board.playable()==true)
{
// Outputs a visual representation to console window
this.displayBoard();
// Asks player to enter a valid number
this.playerMove();
// Check if it still playable for the next
if(board.playable() == true)
{
computerMove()
}
}
this.displayBoard();
// Outputs the final status of the game and the winner if any
if(board.wonBoard()==true) {
System.out.println(board.whoWon() + " has won the game");
} else {
System.out.println("The board is full. Nobody has won the game");
}
}