我的tic tac toe游戏在这里编写了我的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
答案 0 :(得分:3)
本声明:
if (GameOver) System.exit (0) ;
可能是问题的根源。您检测到游戏已结束并在此时退出程序。它永远不会循环回来显示电路板的当前状态。
要解决此问题,您可以:
System.exit
答案 1 :(得分:0)
下面的代码是不显示当前主板的罪魁祸首。
do
{
ComputerSpotChoice = (int) (Math.random() * 9 ) + 1 ;
}
while (Board [(ComputerSpotChoice - 1) / 3] [(ComputerSpotChoice - 1) % 3] != '-') ;
当你进入第九回合时,下面的条件总是为真,因为它找不到' - '。继续无限循环。
while (Board [(ComputerSpotChoice - 1) / 3] [(ComputerSpotChoice - 1) % 3] != '-') ;