Reputation: 57
The code is a Tic-Tac-Toe game (in progress). When I run the code it asks X's position number then it stops. It has to do with the while loop because when I run it without it it works fine. I need the loop however to set X as false so it changes to O's turn.
import java.util.Scanner;
class TicTacToe
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int XInput, OInput;
boolean IfGameOver;
String []board = {"1","2","3","4","5","6","7","8","9"};
IfGameOver = false;
boolean Xturn, Oturn;
Xturn = true;
Oturn = false;
System.out.print("Welcome to Tic-Tac-Toe!\n\n");
do
{
{
System.out.println(" " + board[0] + board[1] + board[2]);
System.out.println(" " + board[3] + board[4] + board[5]);
System.out.println(" " + board[6] + board[7] + board[8]);
}
if (Xturn)
System.out.println("It is X's turn, position number?");
if (Oturn)
System.out.println("It is O's turn, position number?");
do
{
try
{
XInput = scan.nextInt();
board[XInput-1] = "X";
Oturn= true;
Xturn = false;
}
catch (NumberFormatException e)
{
System.out.println("Invalid input. Please enter a position number");
XInput = scan.nextInt();
}
}while(Xturn = true);
do
{
try
{
OInput = scan.nextInt();
board[OInput-1] = "X";
Oturn = false;
Xturn = true;
}
catch (NumberFormatException e)
{
System.out.println("Invalid input. Please enter a position number");
OInput = scan.nextInt();
}
}while (Oturn = true);
} while (!IfGameOver);
}
}
Upvotes: 1
Views: 119
Reputation: 1352
You have multiple issues. As stated previously, your not using an equality check in your while statement, but the other issues override this one. You are setting Oturn to true after X's turn but prior to your do-while loop for O's turn. Therefore, you are going into the Oturn do loop immediately following X input. This should clean it up...
import java.util.Scanner;
class TicTacToe
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int Input;
boolean IfGameOver;
String []board = {"1","2","3","4","5","6","7","8","9"};
IfGameOver = false;
boolean Xturn = true;
System.out.print("Welcome to Tic-Tac-Toe!\n\n");
do
{
{
System.out.println(" " + board[0] + board[1] + board[2]);
System.out.println(" " + board[3] + board[4] + board[5]);
System.out.println(" " + board[6] + board[7] + board[8]);
}
if (Xturn)
System.out.println("It is X's turn, position number?");
else
System.out.println("It is O's turn, position number?");
Input = -1;
do
{
try
{
Input = scan.nextInt();
board[Input-1] = (Xturn?"X":"O");
Xturn = !Xturn;
}
catch (Exception e)
{
System.out.println("Invalid input. Please enter a valid position number");
}
}while(Input==-1);
} while (!IfGameOver);
}
}
Upvotes: 2
Reputation: 2140
Change
}while(Xturn = true);
...
}while (Oturn = true);
to
}while(Xturn);
...
}while (Oturn);
Xturn = true
is an assignment and won't check to see if Xturn
is true. In order to do that, you must use Xturn == true
or simple Xturn
. The same goes for Oturn
.
Also, as stated by TAsk, you need the value of IfGameOver
to become true inside the loop in order for the code to exit the outer do while
loop.
Upvotes: 0
Reputation: 992757
The problem is likely on this line:
}while(Xturn = true);
This is not a comparison. The =
operator is assignment. This assigns the value true
to Xturn
, and then continues the loop because the result of an assignment is always the value that is assigned.
For comparison, you would use ==
. But since Xturn
is a boolean to start with, you can write simply:
}while(Xturn);
Upvotes: 2