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 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;
Initialize the board with EMPTY values
for int i ; i ; i
for int j ; j ; j
boardij CellState.EMPTY;
Assign X and O for testing
board CellState.X;
board CellState.O;
Method to print the board
public void printBoard
for int i ; i ; i
for int j ; j ; j
System.out.printgetCellTextboardij;
System.out.println;
Helper method to get cell text
private String getCellTextCellState state
switch state
case X:
return X;
case O:
return O;
default:
return ;
Method to check if a move is valid
public boolean validMoveint row, int col
Convert based indexing to based indexing
row ;
col ;
Check bounds and whether the cell is empty
if row && row && col && col && boardrowcol 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 checkWinCellStateX
return GameState.WIN;
else if checkWinCellStateO
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 ; i ; i
for int j ; j ; j
if boardij CellState.EMPTY
return false;
return true;
Helper method to check if a player has won
private boolean checkWinCellState player
Check rows
for int i ; i ; i
if boardi player && boardi player && boardi player
return true;
Check columns
for int i ; i ; i
if boardi player && boardi player && boardi player
return true;
Check diagonals
if board player && board player && board player
return true;
if board player && board player && board player
return true;
return false;
Method to run tests on the class
public void runTests
System.out.printlnRunning tests...";
Test validMove
System.out.printlnTest out of bounds low: validMove; Should be false
System.out.printlnTest out of bounds high: validMove; Should be false
System.out.printlnTest cell not empty: validMove; Should be false X is there
System.out.printlnTest valid move: validMove; Should be true
Test gameStatus
board board board CellState.X;
System.out.printlnTest X wins: gameStatus GameState.WIN;
board board CellState.O;
System.out.printlnTest continue: gameStatus GameState.CONTINUE;
Reset the board and test draw
for int i ; i ; i
for int j ; j ; j
boardij CellState.X;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
