Reputation: 88
I have my tic tac toe game coded here for my java assignment and everything works great except for one small problem that is when you enter the last move (the ninth turn) the very last 'X' does not show up. Not only is this just annoying as the winning piece is not shown, but it leads to some problems with the tie method not being addressed to properly, thus it showing nothing.
/*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
Upvotes: 0
Views: 178
Reputation: 15370
Below code is the culprit for not showing the current board.
do
{
ComputerSpotChoice = (int) (Math.random() * 9 ) + 1 ;
}
while (Board [(ComputerSpotChoice - 1) / 3] [(ComputerSpotChoice - 1) % 3] != '-') ;
When you enter ninth turn, below condition is always true as it can not find '-'. Goes on infinite loop.
while (Board [(ComputerSpotChoice - 1) / 3] [(ComputerSpotChoice - 1) % 3] != '-') ;
Upvotes: 0
Reputation: 992887
This statement:
if (GameOver) System.exit (0) ;
is probably the root of the problem. You detect that the game is over and exit your program at that point. It never loops back around to display the current state of the board.
To fix this, you could either:
System.exit
Upvotes: 3