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.
When completed, the contents of the main() method should resemble the following:
createBoard(3,3);
int turn =0;
int playerVal;
int outcome;
java.util.Scanner scan = new java.util.Scanner(System.in);
do {
displayBoard();
playerVal =(turn %2==0)? NOUGHT : CROSS;
if (playerVal == NOUGHT)
System.out.println ("
O's turn");
else System.out.println("
X's turn");
System.out.print("Enter row and column:" );
try {
set(playerVal, scan.nextInt(), scan.nextInt());
} catch (Exception ex)
{System.err.println(ex); turn--;}
turn 1.++;
outcome = winOrTie();
} while ( outcome ==-2);
displayBoard();
switch (outcome){
case NOUGHT:
System.out.println("O wins!");
break;
case CROSS:
System.out.println("X wins!");
break;
case 0:
System.out.println("Tie.");
break;
}
Compile and run the project to ensure it works as expected. Try a few games to verify all wins and ties are correctly detected.
When running, the program should produce output similar to the following in the console window. Note: if you need to expand the window, click on the Full-screen icon in the upper right corner. Click again to restore it.
||||
-------
||||
-------
||||
-------
--O's turn--
Enter row and column:00
|O|||
-------
||||
-------
||||
-------
--X's turn--
Enter row and column:01
|O|X||
-------
||||
-------
||||
-------
--O's turn--
Enter row and column:11
|O|X||
-------
||O||
-------
||||
-------
--X's turn--
Enter row and column:20
|O|X||
-------
||O||
-------
|X|||
-------
--O's turn--
Enter row and column: 22
|O|X||
-------
||O||
-------
|X||O|
-------
O wins!

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

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

What lifestyle traits does your key public have?

Answered: 1 week ago