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 main(String[] args)
class TicTacToe {
private CellValue[][] gameBoard; //3x3 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[3][3];
clearGameBoard();
currentPlayer = CellValue.X; // X starts the game
isEmpty = true;
}
// Method to clear the game board
public void clearGameBoard(){
for (int i =0; i <3; i++){
for (int j =0; j <3; j++){
gameBoard[i][j]= CellValue.Empty;
}
}
}
// Method to set cell value based on current player
public void setCellValue(int row, int col){
if (row <0|| row >=3|| col <0|| col >=3){
System.out.println("Invalid coordinates. Please enter values between 1 and 3.");
return;
}
if (gameBoard[row][col]!= CellValue.Empty){
System.out.println("Position already occupied. Please enter new coordinates.");
return;
}
gameBoard[row][col]= 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 isWin(CellValue player){
// Check rows
for (int i =0; i <3; i++){
if (gameBoard[i][0]== player && gameBoard[i][1]== player && gameBoard[i][2]== player)
return true;
}
// Check columns
for (int j =0; j <3; j++){
if (gameBoard[0][j]== player && gameBoard[1][j]== player && gameBoard[2][j]== player)
return true;
}
// Check diagonals
if ((gameBoard[0][0]== player && gameBoard[1][1]== player && gameBoard[2][2]== player)||
(gameBoard[0][2]== player && gameBoard[1][1]== player && gameBoard[2][0]== player)){
return true;
}
// Method to check if the game board is full
private boolean isBoardFull(){
for (int i =0; i <3; i++){
for (int j =0; j <3; j++){
if (gameBoard[i][j]== CellValue.Empty){
return false;
}
}
}
return true;
}
// Method to display the winner or tie
public void displayWinner(){
if (isWin(CellValue.X)){
System.out.println("Player X wins!");
} else if (isWin(CellValue.O)){
System.out.println("Player O wins!");
} else if (!isEmpty){
System.out.println("It's a tie!");
}
}
// Method to display the game board
public String toString(){
StringBuilder sb = new StringBuilder();
for (int i =0; i <3; i++){
for (int j =0; j <3; j++){
if (gameBoard[i][j]== CellValue.Empty){
sb.append("-");
} else {
sb.append(gameBoard[i][j]).append("");
}
}
sb.append("
");
}
return sb.toString();
}
// Method to start the game
public void startGame(){
Scanner scanner = new Scanner(System.in);
while (isEmpty && (!isWin(CellValue.X) && !isWin(CellValue.O))){
System.out.println("Current board:");
System.out.println(this);
System.out.println("Player "+ currentPlayer +", enter row (1-3) and column (1-3): ");
int row = scanner.nextInt()-1; // Adjusting for 0-based indexing
int col = scanner.nextInt()-1; // Adjusting for 0-based indexing
setCellValue(row, col);
isEmpty =!isBoardFull();
}
System.out.println("Final board:");
System.out.println(this);
displayWinner();
scanner.close();
}
}
public class Main {
public static void main(String[] 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 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!