Question: Java. How do I complete the following method for a tie game in Tic Tac Toe? Full Grid Class is also attached and Box Class

Java. How do I complete the following method for a tie game in Tic Tac Toe? Full Grid Class is also attached and Box Class is also attached which has the Player.Emply method.

public boolean isDraw(Player player) {

// TODO: Check whether the game has ended in a draw.

// Hint: Use a nested loop (see the constructor for an example). Check whether any of the Boxes in the board grid are Player.Empty. If they are, it is not a draw.

// Hint: Return false if it is not a draw, return true if there are not empty positions left

********************************************

/**

* The grid represents the game board.

*/

public class Grid {

// Define the amount of rows and columns

int ROWS = 3; // Rows

int COLUMNS = 3; // Columns

Box[][] board; // Represents the game board as a grid

int currentRow; // Row that was played last

int currentCol; // Column that was played last

/**

* Constructor

*/

public Grid() {

board = new Box[ROWS][COLUMNS]; // TODO: Initialise the board array using ROWS and COLUMNS

for (int row = 0; row < ROWS; ++row) {

for (int col = 0; col < COLUMNS; ++col) {

board[row][col] = new Box(row, col);

}

}

}

/**

* Checks if the game has ended in a draw

* One way to do this is to check that there are no empty positions left

*/

public boolean isDraw(Player player) {

// TODO: Check whether the game has ended in a draw.

// Hint: Use a nested loop (see the constructor for an example). Check whether any of the Boxes in the board grid are Player.Empty. If they are, it is not a draw.

// Hint: Return false if it is not a draw, return true if there are not empty positions left

}

/**

* Return true if the turn player has won after making their move at the coordinate given

*/

public boolean hasWon(Player player) {

// Row check

if(board[currentRow][0].content == player && board[currentRow][1].content == player && board[currentRow][2].content == player) {

return true;

}

// Column check

// TODO: Check if the currentCol is filled.

// Hint: Use the row code above as a starting point, remember that it goes board[row][column].

// Diagonal check (check both directions

if(board[0][0].content == player && board[1][1].content == player && board[2][2].content == player) {

return true;

}

// TODO: Check the diagonal in the other direction

// No one has won yet

return false;

}

/**

* Draws the tic-tac-toe board to the screen

*/

public void display() {

for (int row = 0; row < ROWS; ++row) {

for (int col = 0; col < COLUMNS; ++col) {

// Draw the contents of the box

board[row][col].display();

// Draw the vertical line

if (col < COLUMNS - 1) System.out.print("|");

}

System.out.println();

// Draw the horizontal line

if (row < ROWS - 1) {

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

}

}

}

}

************************************

Box Class

*/

public Box(int row, int col) {

this.row = row; // TODO: Initialise the variables row, col, and content

this.col = col;

this.content = Player.EMPTY;

}

/**

* Clear the box content to EMPTY

*/

public void clear() {

this.content = Player.EMPTY; // TODO: Set the value of content to EMPTY (Remember this is an enum)

}

/**

* Display the content of the box

*/

public void display() {

String output = ""; // TODO: Print the content of this box (" X " if it Player.X, " O " for Player.O and " " for Player.Empty)

if(content == Player.EMPTY) { // Hint: Can use an if-else or switch statement

output = "";

} else if(content == Player.X) {

output = "X";

} else if(content == Player.O) {

output = "O";

}

System.out.println(output);

}

}

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!