gpt4 book ai didi

java - Tic Tac Toe 游戏循环错误

转载 作者:行者123 更新时间:2023-12-01 22:30:37 26 4
gpt4 key购买 nike

我在这里为我的java作业编写了井字棋游戏,一切都很好,除了一个小问题,即当您输入最后一步(第九回合)时,最后一个“X”不显示。这不仅很烦人,因为获胜的棋子没有显示,而且还导致了一些问题,即领带方法没有得到正确解决,因此什么也没有显示。

/*I have methods for drawing the board, determining a winner, and a loser. This is just the 'main' method containing the bulk of the program */

public static void main (String[] args)
{
//Variable declaration
Scanner kbReader = new Scanner(System.in);
char [] [] Board = new char [3] [3] ;
String MenuInput ;
int BoardOutput ;
int UserSpotChoice ;
int ComputerSpotChoice = 0;
int UserTurn = 1 ;
int Winner = 0 ;
Board [0] [0] = '-' ;
Board [0] [1] = '-' ;
Board [0] [2] = '-' ;
Board [1] [0] = '-' ;
Board [1] [1] = '-' ;
Board [1] [2] = '-' ;
Board [2] [0] = '-' ;
Board [2] [1] = '-' ;
Board [2] [2] = '-' ;

//Welcome
System.out.println ("Welcome to Alex Montague's Tic Tac Toe game!") ;
System.out.println ("") ;
System.out.println ("If you wish to play, type 'Play'") ;
System.out.println ("If you wish to read the instructions, type 'Instructions'") ;
System.out.println ("If you wish to exit, type 'Exit'") ;
MenuInput = kbReader.next () ;

do
{
if (MenuInput.equals ("Play") || MenuInput.equals ("play"))
{
while (!GameOver)
{
System.out.println ("\f") ;
System.out.println (" Tic Tac Toe") ;
BoardOutput = DrawBoard (Board) ;
System.out.println (" 1 2 3") ;
System.out.println (" 4 5 6") ;
System.out.println (" 7 8 9") ;
System.out.println ("Please enter the number you would like to move your spot to") ;
UserSpotChoice = kbReader.nextInt () ;

if (UserSpotChoice == 1) Board [0] [0] = 'X' ;
if (UserSpotChoice == 2) Board [0] [1] = 'X' ;
if (UserSpotChoice == 3) Board [0] [2] = 'X' ;
if (UserSpotChoice == 4) Board [1] [0] = 'X' ;
if (UserSpotChoice == 5) Board [1] [1] = 'X' ;
if (UserSpotChoice == 6) Board [1] [2] = 'X' ;
if (UserSpotChoice == 7) Board [2] [0] = 'X' ;
if (UserSpotChoice == 8) Board [2] [1] = 'X' ;
if (UserSpotChoice == 9) Board [2] [2] = 'X' ;

do
{
ComputerSpotChoice = (int) (Math.random() * 9 ) + 1 ;
}
while
(Board [(ComputerSpotChoice - 1) / 3] [(ComputerSpotChoice - 1) % 3] != '-') ;

if (ComputerSpotChoice == 1) Board [0] [0] = 'O' ;
if (ComputerSpotChoice == 2) Board [0] [1] = 'O' ;
if (ComputerSpotChoice == 3) Board [0] [2] = 'O' ;
if (ComputerSpotChoice == 4) Board [1] [0] = 'O' ;
if (ComputerSpotChoice == 5) Board [1] [1] = 'O' ;
if (ComputerSpotChoice == 6) Board [1] [2] = 'O' ;
if (ComputerSpotChoice == 7) Board [2] [0] = 'O' ;
if (ComputerSpotChoice == 8) Board [2] [1] = 'O' ;
if (ComputerSpotChoice == 9) Board [2] [2] = 'O' ;

Winner (Board) ;
Loser (Board) ;
Tie (Board) ;

} //While loop
if (GameOver) System.exit (0) ;
} //If play

else if (MenuInput.equals ("Instructions") || MenuInput.equals ("instructions"))
{
System.out.println ("\f") ;
System.out.println ("You will be playing the game of Tic Tac Toe against the computer.") ;
System.out.println ("The object of this game is to get three of your own x's or o's in a line.") ;
System.out.println ("You take turns placing the x's and o's and whoever gets three in a row first wins.") ;
System.out.println ("Good Luck!") ;
System.out.println ("") ;
System.out.println ("If you wish to play, type 'Play'") ;
System.out.println ("If you wish to exit, type 'Exit'") ;
MenuInput = kbReader.next () ;
}

else if (MenuInput.equals ("Exit") || MenuInput.equals ("exit"))
{
System.out.println ("Thank you for using Alex Montague's Tic Tac Toe game!") ;
System.exit (0) ;
}

else
{
System.out.println ("Sorry, that is not a valid choice.") ;
System.out.println ("If you wish to play, type 'Play'") ;
System.out.println ("If you wish to read the instructions, type 'Instructions'") ;
System.out.println ("If you wish to exit, type 'Exit'") ;
MenuInput = kbReader.next () ;
}

} //do while
while (!MenuInput.equals ("Instructions") || !MenuInput.equals ("instructions") || !MenuInput.equals ("Play") || !MenuInput.equals ("play") || !MenuInput.equals ("Exit") || !MenuInput.equals ("exit")) ;

} // main method

最佳答案

此声明:

        if (GameOver) System.exit (0) ;

可能是问题的根源。您检测到游戏结束并在此时退出程序。它永远不会循环显示板的当前状态。

要解决此问题,您可以:

  • 在调用System.exit之前显示当前棋盘
  • 决定游戏是否在程序中的不同点结束,也许是在打印当前棋盘之后

关于java - Tic Tac Toe 游戏循环错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27829669/

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