Question: 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 I'm 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[3][3];
private static char currentPlayer ='X';
private static boolean gameOver = false;
public static void main(String[] args){
initializeBoard();
displayBoard();
while (true){//REMOVE THE WHILE(TRUE)/BREAK
playTurn();
displayBoard();
checkGameOver();
togglePlayer();
if (gameOver==true)
break;
}
}
public static void initializeBoard(){
for (int i =0; i <3; i++){
for (int j =0; j <3; j++){
board[i][j]='';
}
}
}
private static void displayBoard(){
System.out.println("-------------");
for (int i =0; i <3; i++){
System.out.print("|");
for (int j =0; j <3; j++){
System.out.print(board[i][j]+"|");
}
System.out.println("
-------------");
}
}
public static void playTurn(){
int row, col;
Scanner scanner = new Scanner(System.in);
//CONVERT THIS DO/WHILE TO A WHILE() LOOP
//CONVERT THIS SO THE USER CAN ANSWER 1-3 INSTEAD OF 0-2(SEEMS MORE NORMAL FOR HUMANS)
do {
System.out.print("Player "+ currentPlayer +", enter row (0-2) and column (0-2): ");
row = scanner.nextInt();
col = scanner.nextInt();
} while (!isValidMove(row, col));
board[row][col]= currentPlayer;
}
//EXPLAIN THE LOGIC ON HOW THIS DETERMINES IF A MOVE IS VALID
public static boolean isValidMove(int row, int col){
if (row <0|| row >2|| col <0|| col >2|| board[row][col]!=''){
System.out.println("Invalid move. Try again.");
return false;
}
return true;
}
//EXPLAIN WHAT IS IF/ELSE IF IS DOING AND HOW IT IS DOING IT
public static void checkGameOver(){
if (checkWin()){
System.out.println("Player "+ currentPlayer +" wins!");
gameOver = true;
} else if (isBoardFull()){
System.out.println("It's 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 =0; i <3; i++){
if (board[i][0]== currentPlayer && board[i][1]== currentPlayer && board[i][2]== currentPlayer){
return true;
}
if (board[0][i]== currentPlayer && board[1][i]== currentPlayer && board[2][i]== currentPlayer){
return true;
}
}
if (board[0][0]== currentPlayer && board[1][1]== currentPlayer && board[2][2]== currentPlayer){
return true;
}
if (board[0][2]== currentPlayer && board[1][1]== currentPlayer && board[2][0]== 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 =0; i <3; i++){
for (int j =0; j <3; j++){
if (board[i][j]==''){
return false; // WHAT DOES THIS INDICATE?
}
}
}
return true; //WHAT DOES THIS INDICATE?
}
public static void togglePlayer(){
currentPlayer =(currentPlayer =='X')?'O' : 'X'; // CAREFULLY EXPLAIN THIS LINE OF CODE
}
}
SAMPLE OUTPUT:
-------------
||||
-------------
||||
-------------
||||
-------------
Player X, enter row (1-3) and column (1-3): 1
1
-------------
| X |||
-------------
||||
-------------
||||
-------------
Player O, enter row (1-3) and column (1-3): 1
2
-------------
| X | O ||
-------------
||||
-------------
||||
-------------
Player X, enter row (1-3) and column (1-3): 2
1
-------------
| X | O ||
-------------
| X |||
-------------
||||
-------------
Player O, enter row (1-3) and column (1-3): 3
1
-------------
| X | O ||
-------------
| X |||
-------------
| O |||
-------------
Player X, enter row (1-3) and column (1-3): 2
2
-------------
| X | O ||
-

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