Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ program 8.10 Assignment: Tic Tac Toe Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed C++ program

8.10 Assignment: Tic Tac Toe Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (+). The program should run a loop that: Displays the contents of the board array Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row number and then the column number. Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row number and then the column number. Validates the user's input to ensure that the row and column entered is within the range [1...3] and that cell is not already taken Determines whether a player has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should declare that and end. Player 1 wins when there are three Xs in a row on the game board. The Xs can appear in a row, in a column, or diagonally across the board. The same logic applies to player 2, but with Os. A tie occurs when all of the locations on the board are ful, but there is no winner. Declare global constant ints ROWS and COLS, each equal to 3 In main, create a 2D array that is 3x3 to hold the game state. Your program must use the following functions. They will be unit tested: void fil1Board (charcOLS]) Fill the given array with all stars. Do not print anything here. void showBoard (char (COLS]): Output the game board, with labeled rows and columns. o Print a row with just the column numbers o Create a nested for loop. o For each row, print the row number, then run an inner loop to print the the three array values for that row void getChoice (charcOLS],bool) Input the player's choice and mark it on the game board. The first parameter is the game board. The second parameter is a bool for which player is currently selected o false indicates player 1 o true indicates player 2 o this will be toggled when you switch players Input the player's choice o Do not let the user input a row outside the range [1...3 o Do not let the user input a column outside the range [...3] o Do not let the user input into a cell that already has an 'X or 'O o If the user enters an invalid input, just ask them for that input again. If a row input is invalid, immediately ask for a new row input If a column input is invalid, immediately ask for a new column input -Once a valid row and column are entered, make sure that cell is not taken. If it is taken, ask for new row and column inputs o This is best done using nested do-while loops. See the starter code for a useful structure for this. bool gameOver (char [) [COLS]) .Determine whether a game ending condition has occurred. If so, print one of the following outputs o Player 1 wins! o Player 2 wins! o Tie! Return true if a game ending condition has occurred, false if otherwise. To use this design effectively, the code for main is given to you in the starter code: #include using namespace stai const int ROWS3: const int COLS = 3; //declare function prototypes int main () char board [ROWS] [COLS); bool playerToggle -false fillBoard (board) showBoard (board); while (!game0ver (board) ) getChoice (board, playerToggle) showBoard (board) playerToggle- !playerToggle return 0 Below is a sample output where player 1 wins: 1 2 3 Player 1, Row: 1 Player 1, Column: 1 1 2 3 Player 2, Row: 1 Player 2, Column: 2 1 2 3 Player 1, Row: 2 Player 1, Column: 2 1 2 3 2X * Player 2, Row: 3 Player 2, Column: 1 1 2 3 2X * 3 0 Player 1, Row: 3 Player 1, Column:3 1 2 3 2X * Player 1 wins! Next is a sample output where player 2 wins after ignoring some invalid input 1 2 3 Player 1, Row: 1 Player 1, Column: 1 1 2 3 Player 2, Row: 3 Player 2, Column: 1 1 2 3 3 0 Player 1, Row: 1 Player 1, Column: 2 1 2 3 3 0 Player 2, Row: 1 Player 2, Column:3 1 2 3 3 0 Player 1, Row: 1 Player 1, Column: 3 Player 1, Row: 9 Player 1, Row: 9 Player 1, Row: 2 Player 1, Column: 1 1 2 3 3 0 Player 2, Row: 2 Player 2, Column: 2 1 2 3 3 0 Player 2 wins! The next example produces a tie 1 2 3 Player 1, Row: 1 Player 1, Column: 1 1 2 3 Player 2, Row: 2 Player 2, Column: 1 1 2 3 2 0* Player 1, Row: 1 Player 1, Column: 2 1 2 3 2 0* Player 2, Row: 2 Player 2, Column: 2 1 2 3 Player 1, Row: 2 Player 1, Column: 3

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

Students also viewed these Databases questions