Question: How to fix the errors in this code / / Arley Suazo / / Enum for CellState enum CellState { X , O , EMPTY;
How to fix the errors in this code
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;
private CellState winner; To track the winner if any
private int rowChoice, colChoice;
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
winner CellState.X;
return GameState.WIN;
else if checkWinCellStateO
winner 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 ; 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;
System.out.printlnTest draw: gameStatus GameState.DRAW;
Main class to run the program
public class Main
public static void mainString args
Instantiate TicTacToe object
TicTacToe game new TicTacToe;
Print the initial board
game.printBoard;
Run the tests
game.runTests;
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
