Question: code help. so this is a project. pls check the image for instructions. my code is below pls let me know what i did wrong:

code help. so this is a project. pls check the image for instructions. my code is below pls let me know what i did wrong:
code:
import java.util.Scanner;
import java.util.Random;
/**
* Write a description of class g here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class GameState
{
private char[][] board;
private boolean gameOver;
private char currentPlayer;
public GameState()
{
board = new char[3][3];
gameOver = false;
currentPlayer ='X';
initializeBoard();
}
private void initializeBoard()
{
for (int i =0; i 3; i++)
{
for (int j =0; j 3; j++)
{
board[i][j]='-';
}
}
}
public void playerMove()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Player "+ currentPlayer +"'s turn. Enter the row (0-2) and column (0-2) to place your mark:");
int row = scanner.nextInt();
int col = scanner.nextInt();
if (isValidMove(row, col))
{
board[row][col]= currentPlayer;
currentPlayer =(currentPlayer =='X')?'O' : 'X';
}
else
{
System.out.println("Invalid move. Try again.");
playerMove();
}
}
private boolean isValidMove(int row, int col)
{
return row >=0 && row 3 && col >=0 && col 3 && board[row][col]=='-';
}
public void computerMove()
{
Random rand = new Random();
int row, col;
do
{
row = rand.nextInt(3);
col = rand.nextInt(3);
}
while (board[row][col]!='');
board[row][col]= currentPlayer;
currentPlayer =(currentPlayer =='X')?'O' : 'X';
}
public void displayResult()
{
displayBoard();
if (checkForWinner(currentPlayer))
{
System.out.println("Player "+ currentPlayer +" wins!");
gameOver = true;
} else if (isBoardFull()){
System.out.println("It's a tie!");
gameOver = true;
}
}
private void displayBoard()
{
for (int i =0; i 3; i++)
{
for (int j =0; j 3; j++)
{
System.out.print(board[i][j]+"");
}
System.out.println();
}
}
private boolean checkForWinner(char player)
{
// Check rows, columns, and diagonals for a win
for (int i =0; i 3; i++){
if (board[i][0]== player && board[i][1]== player && board[i][2]== player)
{
return true; // Check rows
}
if (board[0][i]== player && board[1][i]== player && board[2][i]== player)
{
return true; // Check columns
}
}
if ((board[0][0]== player && board[1][1]== player && board[2][2]== player)||(board[0][2]== player && board[1][1]== player && board[2][0]== player))
{
return true; // Check diagonals
}
return false;
}
private boolean isBoardFull()
{
for (int i =0; i 3; i++)
{
for (int j =0; j 3; j++)
{
if (board[i][j]=='-')
{
return false; // Board is not full
}
}
}
return true; // Board is full
}
public boolean isGameOver()
{
return gameOver;
}
public void initGame()
{
System.out.println("Tic-Tac-Toe Game Started!");
}
private void initBoard()
{
for (int i =0; i 3; i++){
for (int j =0; j 3; j++){
board[i][j]='-';
}
}
}
}
public class TicTacToeGame
{
public static void main(String[] args)
{
// Initialize the game
GameState gameState = new GameState();
gameState.initGame();
// Start the game loop
while (!gameState.isGameOver())
{
// Allow players to make moves
gameState.playerMove();
if (gameState.isGameOver())
{
break;
}
// Computer makes a move
gameState.computerMove();
}
// Display the game result
gameState.displayResult();
}
}
this is the whole code.
code help. so this is a project. pls check the

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 Finance Questions!