Penn Foster Exam 40259500 Tic-Tac-Toe game Java Program

$ 12

Penn Foster Exam 40259500 Tic-Tac-Toe game Java Program

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 ninegrid square. An X mark is known as a cross, while an O is called a nought. 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 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(“ “);
}
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, vertically, 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.
static void createBoard(int rows, int cols) {
//Initialize the gameboard
}
static boolean winOrTie() {
//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:
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;
}

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 src\tictactoegame folder and the TicTacToeGame.class file from the build\tictactoegame folder to your desktop or any other temporary location.

38 in stock

SKU: PENN40259500 Category:

Description

Penn Foster Exam 40259500 Tic-Tac-Toe game Java Program

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 ninegrid square. An X mark is known as a cross, while an O is called a nought. 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 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(“ “);
}
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, vertically, 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.
static void createBoard(int rows, int cols) {
//Initialize the gameboard
}
static boolean winOrTie() {
//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:
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;
}

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 src\tictactoegame folder and the TicTacToeGame.class file from the build\tictactoegame folder to your desktop or any other temporary location.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.