Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In the code I ' m providing, I have a working version of TicTacToe from ChatGPT. Runs fine, but it's not the best programming structure.
In the code Im providing, I have a working version of TicTacToe from ChatGPT. Runs fine, but it's not the best programming structure. You will see several comments in all capitals in the code. You need to leave these. Then do what is indicated by the comment. In some, you will be changing code and in others, you will answer in your own comment underneath mine.
import java.util.Scanner;
public class TicTacToe
REMOVE THESE STATIC FIELDS. MAKE THEM LOCAL VARIABLES TO MAIN
private static char board new char;
private static char currentPlayer X;
private static boolean gameOver false;
public static void mainString args
initializeBoard;
displayBoard;
while trueREMOVE THE WHILETRUEBREAK
playTurn;
displayBoard;
checkGameOver;
togglePlayer;
if gameOvertrue
break;
public static void initializeBoard
for int i ; i ; i
for int j ; j ; j
boardij;
private static void displayBoard
System.out.println;
for int i ; i ; i
System.out.print;
for int j ; j ; j
System.out.printboardij;
System.out.println
;
public static void playTurn
int row, col;
Scanner scanner new ScannerSystemin;
CONVERT THIS DOWHILE TO A WHILE LOOP
CONVERT THIS SO THE USER CAN ANSWER INSTEAD OF SEEMS MORE NORMAL FOR HUMANS
do
System.out.printPlayer currentPlayer enter row and column : ;
row scanner.nextInt;
col scanner.nextInt;
while isValidMoverow col;
boardrowcol currentPlayer;
EXPLAIN THE LOGIC ON HOW THIS DETERMINES IF A MOVE IS VALID
public static boolean isValidMoveint row, int col
if row row col col boardrowcol
System.out.printlnInvalid move. Try again.";
return false;
return true;
EXPLAIN WHAT IS IFELSE IF IS DOING AND HOW IT IS DOING IT
public static void checkGameOver
if checkWin
System.out.printlnPlayer currentPlayer wins!";
gameOver true;
else if isBoardFull
System.out.printlnIts a draw!";
gameOver true;
THIS CHECKS FOR A WIN. ADD COMMENTS TO INDICATE WHICH TYPE OF WINE EACH
FOR INSTANT.."THIS IS A HORIZONAL WIN" OR "THIS IS VERTICAL WIN"
public static boolean checkWin
for int i ; i ; i
if boardi currentPlayer && boardi currentPlayer && boardi currentPlayer
return true;
if boardi currentPlayer && boardi currentPlayer && boardi currentPlayer
return true;
if board currentPlayer && board currentPlayer && board currentPlayer
return true;
if board currentPlayer && board currentPlayer && board currentPlayer
return true;
return false;
ADD A COMMENT TO INDICATE HOW THIS DETERMINES IF THE BOARD IS FULL
public static boolean isBoardFull
for int i ; i ; i
for int j ; j ; j
if boardij
return false; WHAT DOES THIS INDICATE?
return true; WHAT DOES THIS INDICATE?
public static void togglePlayer
currentPlayer currentPlayer XO : X; CAREFULLY EXPLAIN THIS LINE OF CODE
SAMPLE OUTPUT:
Player X enter row and column :
X
Player O enter row and column :
X O
Player X enter row and column :
X O
X
Player O enter row and column :
X O
X
O
Player X enter row and column :
X O
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started