Question: Tic tac toe in Java. I have a Player Class, Grid Class, Box Class and GameMain Class. In the GameMain class i need to do

Tic tac toe in Java.

I have a Player Class, Grid Class, Box Class and GameMain Class.

In the GameMain class i need to do the following:

* Constructor

* Sets up the game. Creates the grid and sets the values of the variables before calling the play method.

*/

public GameMain() {

// Create the grid

// TODO: Create a new instance of the "Grid"class

// Reset the game variables to their defaults

// TODO: Assign the default values for currentPlayer (Player.X), gameOver (false), and winner (null

// Begin playing the game

// TODO: Call the "play()" method

Could somebody please show me to code to do this?

The Grid class is as follows:

**

* 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 char[3][3]; // 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() {

for (int i = 0; i < 3; i++) { // TODO: Check whether the game has ended in a draw.

for (int j = 0; j < 3; j++) { // Hint: Return false if it is not a draw, return true if there are not empty positions left

if (board[i][j] == '-') { // 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.

return false;

}

}

}

return true;

}

/**

* 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

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

return true;

}

// Diagonal check (check both directions

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

return true;

}

// Check the diagonal in the other direction

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

return true;

}

// 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("-----------");

}

}

}

}

Many thanks

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!