Question: Please help me fix my java code for a tic tac toe game: import java.util.Scanner; / / Enum to represent cell values enum CellValue {
Please help me fix my java code for a tic tac toe game:
import java.util.Scanner;
Enum to represent cell values
enum CellValue
X O Empty
public static void mainString args
class TicTacToe
private CellValue gameBoard; x game board
private CellValue currentPlayer; current player X or O
private boolean isEmpty; flag to track if there are empty cells left
Constructor
public TicTacToe
gameBoard new CellValue;
clearGameBoard;
currentPlayer CellValue.X; X starts the game
isEmpty true;
Method to clear the game board
public void clearGameBoard
for int i ; i ; i
for int j ; j ; j
gameBoardij CellValue.Empty;
Method to set cell value based on current player
public void setCellValueint row, int col
if row row col col
System.out.printlnInvalid coordinates. Please enter values between and ;
return;
if gameBoardrowcol CellValue.Empty
System.out.printlnPosition already occupied. Please enter new coordinates.";
return;
gameBoardrowcol currentPlayer;
Switch player
currentPlayer currentPlayer CellValue.X CellValue.O : CellValue.X;
Method to check if the game is won by the specified player
public boolean isWinCellValue player
Check rows
for int i ; i ; i
if gameBoardi player && gameBoardi player && gameBoardi player
return true;
Check columns
for int j ; j ; j
if gameBoardj player && gameBoardj player && gameBoardj player
return true;
Check diagonals
if gameBoard player && gameBoard player && gameBoard player
gameBoard player && gameBoard player && gameBoard player
return true;
Method to check if the game board is full
private boolean isBoardFull
for int i ; i ; i
for int j ; j ; j
if gameBoardij CellValue.Empty
return false;
return true;
Method to display the winner or tie
public void displayWinner
if isWinCellValueX
System.out.printlnPlayer X wins!";
else if isWinCellValueO
System.out.printlnPlayer O wins!";
else if isEmpty
System.out.printlnIts a tie!";
Method to display the game board
public String toString
StringBuilder sb new StringBuilder;
for int i ; i ; i
for int j ; j ; j
if gameBoardij CellValue.Empty
sbappend;
else
sbappendgameBoardijappend;
sbappend
;
return sbtoString;
Method to start the game
public void startGame
Scanner scanner new ScannerSystemin;
while isEmpty && isWinCellValueX && isWinCellValueO
System.out.printlnCurrent board:";
System.out.printlnthis;
System.out.printlnPlayer currentPlayer enter row and column : ;
int row scanner.nextInt; Adjusting for based indexing
int col scanner.nextInt; Adjusting for based indexing
setCellValuerow col;
isEmpty isBoardFull;
System.out.printlnFinal board:";
System.out.printlnthis;
displayWinner;
scanner.close;
public class Main
public static void mainString args
TicTacToe game new TicTacToe;
game.startGame;
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
