Buy Best Penn Foster 402595
Your Project #402595
In this project, you’ll create a text-based Tic-Tac-Toe game in
which each player places either an X or O mark on a nine grid
square. An X mark is known as a cross, while an O is
called a naught. The winner is the first player to place their
mark on three contiguous squares vertically, horizontally, or
diagonally across. You can read about it in more detail on
Wikipedia (http://en.wikipedia.org/wiki/Tic-tac-toe).
The output of this project will be referenced in the subsequent
graded projects for this course
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 naught (–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 value, int row, int col) throws
IllegalArgumentException {
if (gameboard[row][col] == EMPTY) gameboard[row][col] = val;
else throw new IllegalArgumentException(“Player already there!”);
}
static void display board() {
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(“
———-
”);
}
}
4. Add the following method signatures to the
TicTacToeGame class:
5. Define the create board method.
6. Define the wineries 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.
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
naught, but no player has three marks horizontally, vertically,
or diagonally. Return NOUGHT if naught 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 naught, the value is
–1, while 1 is the value for the cross.
b. While there’s no winner or tie, display the board and
prompt for a row and column for the current player.
static void createBoard(int rows, int cols) {
//Initialize the gameboard
}
static boolean wineries() {
//Determine whether X or 0 won
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:
create boards(3,3);
int turn = 0;
int player Val;
int outcome;
java. util.Scanner scan = new java. util.Scanner(System.in);
do {
display board();
player Val = (turn % 2 == 0)? NOUGHT: CROSS;
if (player Val == 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 ++;
outcome = wineries();
} while ( outcome == -2 );
display board();
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;
}
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| | |
—O’s turn—
Enter row and column: 2 2.
|O|X| |
______
| |O| |
______
|X| |O|
O wins!
SUBMISSION GUIDELINES
To submit your project, you must provide the following
two files:
■ TicTacToeGame.java
■ TicTacToeGame.class
To find these files within NetBeans, go to the TicTacToeGame
project folder. To determine this folder, right-click on
TicTacToeGame project in the Projects panel. Copy the
value for the Project Folder textbox using the keyboard
shortcut CTRL+C. In Windows Explorer, paste the project
folder path and hit the ENTER key. Copy both the
TicTacToeGame.java file from the srctictactoegame folder
and the TicTacToeGame.class file from the buildtictactoegame
folder to your desktop or any other temporary
location.
Want Assistance with the Penn Foster?
When you visit IWantOnlineclasshelp.com, it is simple to hire an outstanding range of services. You must complete the online form at www.IWantOnlineclasshelp.com to request my online exam-related services, and you must immediately submit all of your records, including your Order ID, name, contact information, and email address, among other details. can be handled by experts in sales to provide you with effective quotations. After you accept the extremely reasonable quote, you will need to pay for the services. After that, the management team will take better care of matching tutor-subject pairs. We strive to meet all of your objectives even before the due date with outstanding A or B grades in a safe manner regarding pay someone to take my exam question. You can have some fun with your loved ones, talk about the important things in your life, and leave the rest to our professional team. For custom, help email us at [email protected] or WhatsApp us at +1(657)366-7486