Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

you are finished before submitting. The code layout is important. If your code l

ID: 3753860 • Letter: Y

Question

you are finished before submitting. The code layout is important. If your code lacks comments before any method or normal 4 space indentation points will be taken off. In this assignment you are will create a two player Tic.tac.toe game that uses the console to show the state of the game after each move of a player. You should use only one class i.e., a single java file that is to be named Bxxxx.java with xxxx replaced by the last 4 digits of your CUNYFirst ID. All variables and methods should be static and belong to the class. The program must have the following methods in addition to a main(0 method. 1. A method to initialize a 3 x 3 2D-array which is your game board. 2. A method to determine the move the current player entered (i.e., row and column). Validate the move in this method. 3. A method to update the game immediately after a player makes a move. 4. A method to determine if the game has been won. 5. A method to determine if the game has ended a draw(tie). 6. A method to print the board. 7. A method to print content of each cell of the board. . Write one method at a time and test that it is working before moving . After each player's move, print the board to show the current state of . For each method write a pseudocode plan in your notebook before you on to the next one. the game. type any code,

Explanation / Answer

package Game;

import java.util.Scanner;

public class TicTacToeXO

{

// Constants to represent the cell contents

public static final int EMPTY = 0;

public static final int XSTATUS = 1;

public static final int OSTATUS = 2;

// Constants to represent the game states

public static final int PLAYING = 0;

public static final int DRAW = 1;

public static final int XWON = 2;

public static final int OWON = 3;

// Constants for number of rows and columns

public static final int ROWS = 3, COLS = 3;

// Declares a 3 x 3 matrix for game board

public static int[][] gameBoard = new int[ROWS][COLS];

// To store the current state of the game (PLAYING, DRAW, XWON, OWON))

public static int currentState;  

// To store current player (XSTATUS or OSTATUS)

public static int currentPlayer;

// To store current row and column

public static int currntRow, currentCol;

// Scanner class object declared  

public static Scanner keyboard = new Scanner(System.in);

// Creates board

public static void createBoard()

{

// Loops till number of rows

for (int r = 0; r < ROWS; ++r)

{

// Loops till number of columns

for (int c = 0; c < COLS; ++c)

gameBoard[r][c] = EMPTY;

}// End of for loop

// Sets the current status to start game

currentState = PLAYING;

// Sets the current player to 'X' (Cross will play first)

currentPlayer = XSTATUS;

}// End of method

// Method to display current board status

public static void displayBoard()

{

// Loops till number of rows

for (int r = 0; r < ROWS; ++r)

{

// Loops till number of columns

for (int c = 0; c < COLS; ++c)

{

// Calls the method to display current cell contents

displayCell(gameBoard[r][c]);

// Checks if current column index is not equals to number of columns -1

if (c != COLS - 1)

// Display vertical partition

System.out.print("|");

}// End of for loop

// Display new line

System.out.println();

// Checks if current row index is not equals to number of rows -1

if (r != ROWS - 1)

// Display horizontal partition line between rows

System.out.println("-----------");

}// End for loop

System.out.println();

}// End of method

// Displays current cell value

public static void displayCell(int content)

{

// Checks current cell contents

switch (content)

{

// If empty then display space

case EMPTY:  

System.out.print(" ");

break;

// If o - status then display O

case OSTATUS:

System.out.print(" O ");

break;

// If x - status then display X

case XSTATUS:  

System.out.print(" X ");

break;

}// End of switch case

}// End of method

// Method to play the game

public static void startPlay(int currentPlayer)

{

// To store input validation status initially false

boolean inputStatus = false;

// Loops until input is valid

do

{

// Checks if current player is equals to x status then player X move

if (currentPlayer == XSTATUS)

// Display the message for player X

System.out.print("Player 'X' turn Enter your move (row[1-3] column[1-3]): ");

// Otherwise player O move

else

// Display the message for player O

System.out.print("Player 'O' turn Enter your move (row[1-3] column[1-3]): ");

// Accepts the row and column index from the user

// Minus one because array index starts at 0 instead of 1

int rowPos = keyboard.nextInt() - 1;

int colPos = keyboard.nextInt() - 1;

// Validates the row and column index not outside the boundary of the board

// Also validates for empty cell

if (rowPos >= 0 && rowPos < ROWS && colPos >= 0 && colPos < COLS && gameBoard[rowPos][colPos] == EMPTY)

{

// Assigns the row and column inputed by the user

currntRow = rowPos;

currentCol = colPos;

// Updates the board with player move

gameBoard[currntRow][currentCol] = currentPlayer;

// Sets the input status to true for exit

inputStatus = true;

}// End of if condition

// Otherwise invalid data entered display error message

else

System.out.println("This move at (" + (rowPos + 1) + "," + (colPos + 1) + ") is not valid. Enter again...");

}while (!inputStatus);// End of do while loop

}// End of method

// Method to check the winning status or draw

public static void gameOverStatus(int currentPlayer, int currentRow, int currentCol)

{

// Calls the method to checks who is the winner?

if (checkWon(currentPlayer, currentRow, currentCol))

// Updates the current state by checking

// currentPlayer is equals to XXSTATUS then X won

// otherwise O won

currentState = (currentPlayer == XSTATUS) ? XWON : OWON;

// Otherwise call the method to check draw

else if (checkDraw())

// Update the current status to draw

currentState = DRAW;

  

// Otherwise, no change to currentState (continue playing).

}// End of method

// Method to return true if it is a draw (It is draw if no more empty cell and no one won)

public static boolean checkDraw()

{

// Loops till number of rows

for (int r = 0; r < ROWS; ++r)

{

// Loops till number of rows

for (int c = 0; c < COLS; ++c)

// Checks if current row and column index position is empty

if (gameBoard[r][c] == EMPTY)

// Then return false (game not over)

return false;

}// End of for loop

// Otherwise no empty cell return true for game draw

return true;

}// End of method

// Method to return true if the current player won the game

public static boolean checkWon(int currentPlayer, int currentRow, int currentCol)

{

// Checks for current player value is available in same for 3 row

return (gameBoard[currentRow][0] == currentPlayer

&& gameBoard[currentRow][1] == currentPlayer

&& gameBoard[currentRow][2] == currentPlayer

// Checks for current player value is available in same for 3 column

|| gameBoard[0][currentCol] == currentPlayer

&& gameBoard[1][currentCol] == currentPlayer

&& gameBoard[2][currentCol] == currentPlayer

// Checks for current player value is available in same for left diagonal

|| currentRow == currentCol

&& gameBoard[0][0] == currentPlayer

&& gameBoard[1][1] == currentPlayer

&& gameBoard[2][2] == currentPlayer

// Checks for current player value is available in same for right diagonal

|| currentRow + currentCol == 2

&& gameBoard[0][2] == currentPlayer

&& gameBoard[1][1] == currentPlayer

&& gameBoard[2][0] == currentPlayer);

}// End of method

// main method definition

public static void main(String[] args)

{

// Calls the method to create board and set the player status

createBoard();

// Calls the method to display board

displayBoard();

// Start playing the game for one time

do

{

// Calls the method to start the game with current player as X

startPlay(currentPlayer);

// Calls the method to check game over status

gameOverStatus(currentPlayer, currntRow, currentCol);

// Calls the method to display board

displayBoard();

// Checks if current player status is XWON value then display X own the game

if (currentState == XWON)

System.out.println("Congratulation game own by - 'X'! Bye Bye!");

// Otherwise checks if current player status is OWON value then display O own the game

else if (currentState == OWON)

System.out.println("Congratulation game own by - 'O'! Bye Bye!");

// Otherwise checks if current player status is DRAW value then game is draw

else if (currentState == DRAW)   

System.out.println("Opps ........ It's a DRAW! Bye Bye!");

// Switch player move

currentPlayer = (currentPlayer == XSTATUS) ? OSTATUS : XSTATUS;

} while (currentState == PLAYING); // End of do - while loop

}// End of main method

}// End of class

Sample Output 1:

| |

-----------

| |

-----------

| |

Player 'X' turn

Enter your move (row[1-3] column[1-3]): 1

2

| X |

-----------

| |

-----------

| |

Player 'O' turn

Enter your move (row[1-3] column[1-3]): 2

2

| X |

-----------

| O |

-----------

| |

Player 'X' turn

Enter your move (row[1-3] column[1-3]): 3

2

| X |

-----------

| O |

-----------

| X |

Player 'O' turn

Enter your move (row[1-3] column[1-3]): 2

1

| X |

-----------

O | O |

-----------

| X |

Player 'X' turn

Enter your move (row[1-3] column[1-3]): 2

3

| X |

-----------

O | O | X

-----------

| X |

Player 'O' turn

Enter your move (row[1-3] column[1-3]): 1

1

O | X |

-----------

O | O | X

-----------

| X |

Player 'X' turn

Enter your move (row[1-3] column[1-3]): 3

1

O | X |

-----------

O | O | X

-----------

X | X |

Player 'O' turn

Enter your move (row[1-3] column[1-3]): 3

3

O | X |

-----------

O | O | X

-----------

X | X | O

Congratulation game own by - 'O'! Bye Bye!

Sample Output 2:

| |

-----------

| |

-----------

| |

Player 'X' turn

Enter your move (row[1-3] column[1-3]): 2

2

| |

-----------

| X |

-----------

| |

Player 'O' turn

Enter your move (row[1-3] column[1-3]): 1

3

| | O

-----------

| X |

-----------

| |

Player 'X' turn

Enter your move (row[1-3] column[1-3]): 3

1

| | O

-----------

| X |

-----------

X | |

Player 'O' turn

Enter your move (row[1-3] column[1-3]): 11

1

This move at (11,1) is not valid.

Enter again...

Player 'O' turn

Enter your move (row[1-3] column[1-3]): 1

1

O | | O

-----------

| X |

-----------

X | |

Player 'X' turn

Enter your move (row[1-3] column[1-3]): 1

2

O | X | O

-----------

| X |

-----------

X | |

Player 'O' turn

Enter your move (row[1-3] column[1-3]): 3

2

O | X | O

-----------

| X |

-----------

X | O |

Player 'X' turn

Enter your move (row[1-3] column[1-3]): 3

3

O | X | O

-----------

| X |

-----------

X | O | X

Player 'O' turn

Enter your move (row[1-3] column[1-3]): 2

1

O | X | O

-----------

O | X |

-----------

X | O | X

Player 'X' turn

Enter your move (row[1-3] column[1-3]): 2

3

O | X | O

-----------

O | X | X

-----------

X | O | X

Opps ........

It's a DRAW! Bye Bye!3

Sample Output 3:

| |

-----------

| |

-----------

| |

Player 'X' turn

Enter your move (row[1-3] column[1-3]): 2

2

| |

-----------

| X |

-----------

| |

Player 'O' turn

Enter your move (row[1-3] column[1-3]): 1

1

O | |

-----------

| X |

-----------

| |

Player 'X' turn

Enter your move (row[1-3] column[1-3]): 1

3

O | | X

-----------

| X |

-----------

| |

Player 'O' turn

Enter your move (row[1-3] column[1-3]): 3

1

O | | X

-----------

| X |

-----------

O | |

Player 'X' turn

Enter your move (row[1-3] column[1-3]): 3

3

O | | X

-----------

| X |

-----------

O | | X

Player 'O' turn

Enter your move (row[1-3] column[1-3]): 3

2

O | | X

-----------

| X |

-----------

O | O | X

Player 'X' turn

Enter your move (row[1-3] column[1-3]): 2

3

O | | X

-----------

| X | X

-----------

O | O | X

Congratulation game own by - 'X'! Bye Bye!