Question: Java Tic Tac Toe. I am having a problem getting the correct code in my GameMain class. The class has the main method at the
Java Tic Tac Toe. I am having a problem getting the correct code in my GameMain class. The class has the main method at the bottom of the code. The main method requires me to do the following: Please note I have also attached the whole of the GameMain class. There is also a Grid Class, Player Enum and Box Class which all seem to have been set correctly.
* The main() method
*/
public static void main(String[] args)
Scanner scanner = new Scanner(System.in)
// TODO: Add a loop to restart the game once it has finished
// TODO: Then update the loop to ask the player if they want to play again, exit if they do not
new GameMain();
}
****************************************************************************
import java.util.Scanner; // Scanner required for player input
/**
* The main class for the game Tic-Tac-Toe.
* Controls the flow of the game, allowing each player to enter an option until the game ends.
*/
public class GameMain {
private static final String play = null;
private static Scanner scanner = new Scanner(System.in); // Scanner for input
private static Grid grid; // The game board
private static boolean gameOver; // Whether game is playing or over
private static Player winner; // Winner of the game
private static Player currentPlayer; // Current player (enum)
/**
* Constructor
* Sets up the game. Creates the grid and sets the values of the variables before calling the play method.
*/
public GameMain() {
currentPlayer = Player.X;
boolean gameOver = false;
winner = null;
Grid grid = new Grid();
play();
// 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 S// TODO: Call the "play()" method
}
/**
* Controls the game play, rotates between player turns until a winner is decided.
*/
public static void play() {
do {
playerMove(currentPlayer); // Have the player perform their move
grid.display(); // Display the current game board
checkForWinner(currentPlayer); // Checks if the game has been won
// Display results if game is over
if(gameOver) {
if(winner == Player.X) {
System.out.println("Player X wins!");
}
if(winner == Player.O) {
System.out.println("Player O wins!");
}
if(winner == Player.X || winner == Player.O) {
System.out.println("It is a draw!");
}
}
// Switch turn to the next player
if(currentPlayer == Player.X) {
currentPlayer = Player.O;
} else {
currentPlayer = Player.X;
}
} while (!gameOver); // repeat until game-over
}
/**
* Handles the player making their move, checks if the move is valid before making it.
*/
public static void playerMove(Player turnPlayer) {
boolean validInput = false;
do {
// Display instructions to the player
if (turnPlayer == Player.X) {
System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");
} else {
if (turnPlayer == Player.O) {
System.out.println("Player 'O', enter your move (row[1-3] column[1-3]: ");
}
}
// Obtains input from the player for both row and column
int row = scanner.nextInt();
int col = scanner.nextInt();
// Decrease the value entered by 1 to compensate for the array index starting at 0
row--;
col--;
// Verify the values the player entered are valid (position is valid and empty)
if (row >= 0 && row < Grid.ROWS && col >= 0 && col < Grid.COLUMNS && grid.board[row][col].content == Player.EMPTY) {
grid.board[row][col].content = turnPlayer;
grid.currentRow = row;
grid.currentCol = col;
validInput = true;
} else {
System.out.println(" Error message - the move was not valid. Please re-enter a valid move");// TODO: Display an error message that the move was not valid.
}
} while (!validInput); // Repeat until input is valid
}
/**
* Checks if the game has ended
*/
public static void checkForWinner(Player turnPlayer) {
if (grid.hasWon(turnPlayer)) {
gameOver = true;
turnPlayer = winner;
} else if (grid.isDraw()) {
gameOver = true;
winner = Player.EMPTY;
// TODO: Set gameOver and winner appropriately
}
}
/**
* The main() method
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// TODO: Add a loop to restart the game once it has finished
// TODO: Then update the loop to ask the player if they want to play again, exit if they do not
new GameMain();
}
}
*********************************************
Grid Class
/**
* The grid represents the game board.
*/
public class Grid {
// Define the amount of rows and columns
static int ROWS = 3; // Rows
static int COLUMNS = 3; // Columns
static 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 COLUMN
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 row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLUMNS; ++col) {
if (board[row][col].content == Player.EMPTY) {
return false;
}
}
}
return true;
// 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
if(board[0][currentCol].content == player && board[1][currentCol].content == player && board[2][currentCol].content == player) {
return true;
// 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;
}
if(board[0][2].content == player && board[1][1].content == player && board[2][0].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 static 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
/**
* The Box class models each individual box of the Grid
*/
public class Box {
Player content; // The move this box holds (Empty, X, or O)
int row, col; // Row and column of this box (Not currently used but possibly useful in future updates)
/**
* Constructor
*/
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);
}
}
************************
Player Enum
** * Enumeration for the players move */
public enum Player { EMPTY, X, O;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
