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;
gameOver false;
currentPlayer X;
initializeBoard;
private void initializeBoard
for int i ; i ; i
for int j ; j ; j
boardij;
public void playerMove
Scanner scanner new ScannerSystemin;
System.out.printlnPlayer currentPlayer s turn. Enter the row and column to place your mark:";
int row scanner.nextInt;
int col scanner.nextInt;
if isValidMoverow col
boardrowcol currentPlayer;
currentPlayer currentPlayer XO : X;
else
System.out.printlnInvalid move. Try again.";
playerMove;
private boolean isValidMoveint row, int col
return row && row && col && col && boardrowcol;
public void computerMove
Random rand new Random;
int row, col;
do
row rand.nextInt;
col rand.nextInt;
while boardrowcol;
boardrowcol currentPlayer;
currentPlayer currentPlayer XO : X;
public void displayResult
displayBoard;
if checkForWinnercurrentPlayer
System.out.printlnPlayer currentPlayer wins!";
gameOver true;
else if isBoardFull
System.out.printlnIts a tie!";
gameOver true;
private void displayBoard
for int i ; i ; i
for int j ; j ; j
System.out.printboardij;
System.out.println;
private boolean checkForWinnerchar player
Check rows, columns, and diagonals for a win
for int i ; i ; i
if boardi player && boardi player && boardi player
return true; Check rows
if boardi player && boardi player && boardi player
return true; Check columns
if board player && board player && board playerboard player && board player && board player
return true; Check diagonals
return false;
private boolean isBoardFull
for int i ; i ; i
for int j ; j ; j
if boardij
return false; Board is not full
return true; Board is full
public boolean isGameOver
return gameOver;
public void initGame
System.out.printlnTicTacToe Game Started!";
private void initBoard
for int i ; i ; i
for int j ; j ; j
boardij;
public class TicTacToeGame
public static void mainString 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 gameStateisGameOver
break;
Computer makes a move
gameState.computerMove;
Display the game result
gameState.displayResult;
this is the whole code.
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
