Answered step by step
Verified Expert Solution
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 ;
static final int NOUGHT ;
static final int CROSS ;
Note: The variable gameboard is a twodimensional int array. Think of it as a table with rows and columns, in which cells can be empty or contain a nought or cross
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 setint val, int row, int col
throws IllegalArgumentException
if gameboardrowcol EMPTY
gameboardrowcol val;
else throw new IllegalArgumentException
Player already there!";
display the board
static void displayBoard
for int r ; r gameboard.length; r
System.out.print;
for int c ; c gameboardrlength; c
switchgameboardrc
case NOUGHT:
System.out.printO;
break;
case CROSS:
System.out.printX;
break;
default: Empty
System.out.print;
System.out.print;
System.out.println
;
Define a createBoard method to initialize the game board:
static void createBoardint 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 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, if theres a tie, and another value like 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 while 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 trycatch 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;
int turn ;
int playerVal;
int outcome;
java.util.Scanner scan new java.util.ScannerSystemin;
do
displayBoard;
playerVal turn NOUGHT : CROSS;
if playerVal NOUGHT
System.out.println
Os turn;
else System.out.println
Xs turn;
System.out.printEnter row and column:" ;
try
setplayerVal scan.nextInt scan.nextInt;
catch Exception ex
Systemerr.printlnex; turn;
turn ;
outcome winOrTie;
while outcome ;
displayBoard;
switch outcome
case NOUGHT:
System.out.printlnO wins!";
break;
case CROSS:
System.out.printlnX wins!";
break;
case :
System.out.printlnTie;
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 Fullscreen icon in the upper right corner. Click again to restore it
Os turn
Enter row and column:
O
Xs turn
Enter row and column:
OX
Os turn
Enter row and column:
OX
O
Xs turn
Enter row and column:
OX
O
X
Os turn
Enter row and column:
OX
O
XO
O wins!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started