Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Eclipse, create a new Java project named TicTacToeGame. Define the package name to be tictactoegame. Add a single class also named TicTacToeGame. Add the

In Eclipse, create a new Java project named TicTacToeGame. Define the package name to be tictactoegame. Add a single class also named TicTacToeGame.
Add the following variable and constant declarations to the TicTacToeGame class:
static int[][] gameboard;
static final int EMPTY =0;
static final int NOUGHT =-1;
static final int CROSS =1;
Note: The variable gameboard is a two-dimensional int array. Think of it as a table with rows and columns, in which cells can be empty (0) or contain a nought (1) or cross (1).
The keywords static and final declare that the variable being defined is a constant. The value of a constant cant be changed. By convention, constant names are all uppercase. The constants EMPTY, NOUGHT, and CROSS will simplify your code.
Add the following utility methods to the TicTacToeGame class:
/* Set a square on the board must be empty */
static void set(int val, int row, int col)
throws IllegalArgumentException {
if (gameboard[row][col]== EMPTY)
gameboard[row][col]= val;
else throw new IllegalArgumentException
("Player already there!");
}
/* display the board */
static void displayBoard(){
for( int r =0; r < gameboard.length; r++){
System.out.print("|");
for (int c =0; c < gameboard[r].length; c++)
{
switch(gameboard[r][c]){
case NOUGHT:
System.out.print("O");
break;
case CROSS:
System.out.print("X");
break;
default: //Empty
System.out.print("");
}
System.out.print("|");
}
System.out.println("
-------");
}
}
Define a createBoard method to initialize the game board:
static void createBoard(int rows, int cols){
//TODO Initialize the gameboard
}
Define a winOrTie method. Check first for a win with rows and columns and then diagonally. Finally, check to see if there are any empty cells without a cross or naught.
static int winOrTie(){
//TODO Determine whether X or O won or there is a tie
}
Review the relevant sections of Chapter 5 in your textbook to see how to initialize and iterate through a multidimensional array.
A player wins if all cells in a row, column, or diagonally are the same mark. The players tie if all cells have a mark but no player has three horizontal, vertical, or diagonal marks. Return NOUGHT if nought wins, CROSS if cross wins, 0 if theres a tie, and another value (like 2, for example) if there are empty cells on the board.
In the main() method, perform the following actions:
Create the board and initialize a turn counter, player value, and game outcome. For nought, the value is 1, while 1 is the value for cross.
While theres no winner or tie, display the board and prompt for a row and column for the current player.
Use a try-catch block to handle the set method exception. Use the System.err.println() method rather than the System.out.println() method to output the exception. This will cause the message to be displayed in red.
Display the final board and a message stating which player won or if theres a tie.1.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions