Question: please help modify existing code to match You need to complete the game of TicTacToe by adding in user input and a play method that

please help modify existing code to match "You need to complete the game of TicTacToe by adding in user input and a play method that loops until the game is complete. You may also add any instance variables that you think are useful to play the game such as a tracker for which players turn it is, and the Scanner
Make sure to test your new code, BUT you should not call your runTests method from part 1 in your final draft. Main.javas main method should only instantiate a TicTacToe variable and then call that objects play method. You should NOT write a loop in main. Put the primary game loop (the loop that repeats as long as the GameState is CONTINUE) in the play method in TicTacToe.java."
//Arley Suazo
// Enum for CellState
enum CellState {
X, O, EMPTY;
}
// Enum for GameState
enum GameState {
WIN, DRAW, CONTINUE;
}
// TicTacToe class handling the game logic
public class TicTacToe {
// Board to store the state of the cells
private CellState[][] board;
// Constructor
public TicTacToe(){
board = new CellState[3][3];
// Initialize the board with EMPTY values
for (int i =0; i <3; i++){
for (int j =0; j <3; j++){
board[i][j]= CellState.EMPTY;
}
}
// Assign X and O for testing
board[0][0]= CellState.X;
board[0][1]= CellState.O;
}
// Method to print the board
public void printBoard(){
for (int i =0; i <3; i++){
for (int j =0; j <3; j++){
System.out.print(getCellText(board[i][j])+"");
}
System.out.println();
}
}
// Helper method to get cell text
private String getCellText(CellState state){
switch (state){
case X:
return "X";
case O:
return "O";
default:
return "";
}
}
// Method to check if a move is valid
public boolean validMove(int row, int col){
// Convert 1-based indexing to 0-based indexing
row -=1;
col -=1;
// Check bounds and whether the cell is empty
if (row >=0 && row <3 && col >=0 && col <3 && board[row][col]== CellState.EMPTY){
return true;
}
return false;
}
// Method to check the current game status
public GameState gameStatus(){
// Check rows, columns, and diagonals for a win
if (checkWin(CellState.X)){
return GameState.WIN;
} else if (checkWin(CellState.O)){
return GameState.WIN;
}
// Check for a draw (if no empty cells are left)
if (isBoardFull()){
return GameState.DRAW;
}
// Otherwise, the game should continue
return GameState.CONTINUE;
}
// Helper method to check if the board is full
private boolean isBoardFull(){
for (int i =0; i <3; i++){
for (int j =0; j <3; j++){
if (board[i][j]== CellState.EMPTY){
return false;
}
}
}
return true;
}
// Helper method to check if a player has won
private boolean checkWin(CellState player){
// Check rows
for (int i =0; i <3; i++){
if (board[i][0]== player && board[i][1]== player && board[i][2]== player){
return true;
}
}
// Check columns
for (int i =0; i <3; i++){
if (board[0][i]== player && board[1][i]== player && board[2][i]== player){
return true;
}
}
// Check diagonals
if (board[0][0]== player && board[1][1]== player && board[2][2]== player){
return true;
}
if (board[0][2]== player && board[1][1]== player && board[2][0]== player){
return true;
}
return false;
}
// Method to run tests on the class
public void runTests(){
System.out.println("Running tests...");
// Test validMove
System.out.println("Test 1(out of bounds low): "+!validMove(0,1)); // Should be false
System.out.println("Test 2(out of bounds high): "+!validMove(4,1)); // Should be false
System.out.println("Test 3(cell not empty): "+!validMove(1,1)); // Should be false (X is there)
System.out.println("Test 4(valid move): "+ validMove(2,2)); // Should be true
// Test gameStatus
board[0][0]= board[1][1]= board[2][2]= CellState.X;
System.out.println("Test 5(X wins): "+(gameStatus()== GameState.WIN));
board[0][0]= board[0][1]= CellState.O;
System.out.println("Test 6(continue): "+(gameStatus()== GameState.CONTINUE));
// Reset the board and test draw
for (int i =0; i <3; i++){
for (int j =0; j <3; j++){
board[i][j]= CellState.X;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!