Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java tic-tac-toe implement a text-based Tic-Tac-Toe game for two human players. However, you will be expanding your program to include computer players at a later

Java tic-tac-toe

implement a text-based Tic-Tac-Toe game for two human players. However, you will be expanding your program to include computer players at a later date, so please realize that you are required to implement exactly the methods below even if you will only use the "human" options in this assignment. This program emphasizes two-dimensional arrays and methods.

Requirements

Board: You must use a 3 x 3 two-dimensional array of chars to store the game board information. Valid values for each square on the board are:

(space) -- meaning this square is empty (not yet chosen)

X -- meaning this square is taken by player 1 (always X)

O -- meaning this square is taken by player 2 (always O).

Player Options: For each game, your program should prompt the user to determine whether each player is human or computer, and you will just choose human for both. Player 1 always goes first and uses the X symbol.

Input and Output: The output of the board should be in text format, as shown below. Please use a fixed-width font (like Courier) so that things line up. Allow a human player to enter an integer to choose an empty square (see example below). Whenever an invalid square is chosen by a human player (already filled, or out of range), print an error message and have the human player try again. Sample output is shown below.

End of Game: When a player wins, print out a message declaring the winner as shown below. If the game ends with no winner, print out a message declaring it a draw.

Methods: You are required to define the following methods with these parameter profiles and return types. Assume the board is a 3x3 character array.

void main(): run a single game.

void takeTurn(board, int playerNum, boolean human): Call the humanTurn method below with the correct player number and the board based on whether the boolean variable human is true or false.

void humanTurn(board, int playerNum): Update the board by consulting the user, and announce the move.

void announceMove(int playerNum, boolean human, int move) Print a line announcing which player of which type took which move. See sample output below.

boolean horizontalWin(board, playerNum): Return true if this board has a horizontal win, false otherwise.

boolean verticalWin(board, playerNum): Return true if this board has a vertical win, false otherwise.

boolean diagonalWin(board, playerNum): Return true if this card has a diagonal win, false otherwise.

int Winner(board): Return players number (1 or 2) if they win, zero otherwise.

int availableSquares(board): Return the number of squares still available on the board.

boolean isFull(board): Return true if this board has no empty squares.

void printBoard(board): Print the current board and reminder board as shown below.

It should look like this:

Is player one human?(yes or no): yes

Is player two human?(yes or no): yes

Game Board: Positions:

| | 1 | 2 | 3

----------- -----------

| | 4 | 5 | 6

----------- -----------

| | 7 | 8 | 9

Player 1, please enter a number(1-9): 1

Player 1 chooses position 1.

X | | 1 | 2 | 3

----------- -----------

| | 4 | 5 | 6

----------- -----------

| | 7 | 8 | 9

Player 2, please enter a number(1-9): 6

Player 2 chooses position 6.

X | | 1 | 2 | 3

----------- -----------

| | O 4 | 5 | 6

----------- -----------

| | 7 | 8 | 9

Player 1, please enter a number(1-9): 3

Player 1 chooses position 3.

X | | X 1 | 2 | 3

----------- -----------

| | O 4 | 5 | 6

----------- -----------

| | 7 | 8 | 9

Player 2, please enter a number(1-9): 2

Player 2 chooses position 2.

X | O | X 1 | 2 | 3

----------- -----------

| | O 4 | 5 | 6

----------- -----------

| | 7 | 8 | 9

Player 1, please enter a number(1-9): 7

Player 1 chooses position 7.

X | O | X 1 | 2 | 3

----------- -----------

| | O 4 | 5 | 6

----------- -----------

X | | 7 | 8 | 9

Player 2, please enter a number(1-9): 4

Player 2 chooses position 4.

X | O | X 1 | 2 | 3

----------- -----------

O | | O 4 | 5 | 6

----------- -----------

X | | 7 | 8 | 9

Player 1, please enter a number(1-9): 5

Player 1 chooses position 5.

X | O | X 1 | 2 | 3

----------- -----------

O | X | O 4 | 5 | 6

----------- -----------

X | | 7 | 8 | 9

Game over! Player 1 has won the game!

BUILD SUCCESSFUL (total time: 1 minute 21 seconds)

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_2

Step: 3

blur-text-image_3

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

Upgrading Oracle Databases Oracle Database New Features

Authors: Charles Kim, Gary Gordhamer, Sean Scott

1st Edition

B0BL12WFP6, 979-8359657501

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago