Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Instructions/Design In a game of tic-tac-toe, 2 players take turns marking an available cell in a 3x3 grid with their respective tokens (either X or

Instructions/Design In a game of tic-tac-toe, 2 players take turns marking an available cell in a 3x3 grid with their respective tokens (either X or O). When one player has placed 3 tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Create a program for playing tic-tac-toe. The program prompts 2 players to enter an X token and O token alternately. Whenever a token is entered, the program redisplays the board on the console and determines the status of the game (win, draw or continue). So the program is not actually playing the game--it is just showing the game state.

YOU MUST USE THE FOLLOWING TWO CLASSES IN YOUR SOLUTION:

TicTacToe Class: This class is the driver program. It contains one method: main() which uses the GameBoard class to play the game of Tic-Tac-Toe. It also contains a flag 'keepPlaying' that is set when the game is over (win or draw).

Here is the UML:

TicTacToe

+ main(String): void

Here is the algorithm:

displayBoard while (keepPlaying) promptAndRecordMove X displayBoard if (isWon X) X player won else if (isDraw) No winner if (keepPlaying) //no need to continue if X won/draw displayBoard promptAndRecordMove O if (isWon O) O player won else if (isDraw) No winner.

GameBoard Class:

This class holds the reference to the 2-D array representing the board and contains all the methods needed to display the board, have one user make a move, check if there is a winner, and check if there is a draw.

The constructor should initialize each cell to a character of your choice. Use a nested loop to do this.

Here is the UML:

GameBoard - board: char[ ][ ] - input: Scanner + GameBoard(): GameBoard + displayBoard(): void + makeAMove(char): void + isDraw(): boolean + isWon(char): boolean

The ONLY classes you may use are: GameBoard and the driver: TicTacToe

Input:

One user acts as BOTH player X and player O and is prompted to enter the row and then the column they want. Example (user input in bold): Enter a row for player X: 1 Enter a column for player X: 2

Hint: Do one method at a time and make it work before moving on to the next method.

Start with displaying the board using a nested for-loop.

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

Step: 3

blur-text-image

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

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago