Do this ASAP
1. In NetBeans, create a new Java Application project named TicTacToeGame.
2. 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, where a cell can be empty (0) or contain a nought (–1) or cross (1) . The constants EMPTY, NOUGHT, and CROSS will simplify your code.
3. Add the following utility methods to the TicTacToeGame
class:
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!”);
}
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(“ “);
72
Graded Project
}
System.out.print(“|”);
}
System.out.println(“\n———-\n”);
}
}
4. Add the following method signatures to the
TicTacToeGame class:
5. Define the createBoard method.
6. Define the 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 void createBoard(int rows, int cols) {
//Initialize the gameboard
}
static boolean winOrTie() {
//Determine whether X or 0 won or there is a tie
}
Note: Review the sections “Initializing Multidimensional Arrays” on pages 144–145 and “Iterating Over Multidimensional Arrays” on pages 156–158 for how to initialize and iterate through a multidimensional array. A player wins if all the cells in a row or column are the same mark or diagonally through the center. The players tie if all cells have a cross or nought, but no player has three marks horizontally, verti- cally, or diagonally. Return NOUGHT if nought wins, CROSS if cross wins, 0 if there’s a tie, and another value (like –2, for example) if there are empty cells on the board.
7. In the main() method, perform the following actions:
a. 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.
b. While there’s no winner or tie, display the board and prompt for a row and column for the current player.
73
Graded Project
c. Use a try/catch block to handle the exception from the set method. You can use the System.err.println method rather than the System.out.println method to output the exception. This will display the message in red.
d. Display the final board and a message on which player won or if there’s a tie.
8. 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(“\n
—O’s turn—
”); else System.out.println(“\n
—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 ++;
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;
}
74
Graded Project
9. Compile and run the project to ensure it works as expected. Try a few games to verify all wins and ties are correctly detected.
The application should behave as follows in the Output window:
| | | |
| | | |
| | | |
—O’s turn—
Enter row and column:0 0
|O| | |
| | | |
| | | |
—X’s turn—
Enter row and column:0 1
|O|X| |
| | | |
| | | |
—O’s turn—
Enter row and column:1 1
|O|X| |
| |O| |
| | | |
—X’s turn—
Enter row and column:2 0
|O|X| |
| |O| |
|X| | |
75
Graded Project
—O’s turn—
Enter row and column: 2 2.
|O|X| |
| |O| |
|X| |O|
O wins!