Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C + + program that allows a player to play the game of tic - tac - toe against the computer.You may NOT

Write a C++ program that allows a player to play the game of tic-tac-toe against the computer.You may NOT USE THE C++11 array or any STL container (vector, list, map, ...).Your game program does the following: In the main() function, declare a 3x3 two-dimensional array of type char representing the board.Also, declare a second two-d array of integers of size 2-by-10(or more) with all positionsinitially assigned 0 to record the winner and turns required to win (reminder: do not use theC++11 array or any STL container). The main function should have a loop that runs as manytimes as a player wants to play. In the loop first call the initialize function and then there should be another loop that plays thegame by calling the following functions (this is a list of the functions you need to call notnecessarily how you need to call them) they are described below:
o drawBoardo playerTurno computerTurno checkWinnerWhen the user chooses not to play anymore, you should look for the minimum number of turnsin the second array and then display a message: That the overall winner is You or Computer with N turns. The overall winner is basedon who won any game in the fewest turns, ties go to the computer. Display the number of user wins Display the number of computer wins. Initialize: This function assigns a space (blank) to each board position. drawBoard: This function first clears the screen by calling the system function with the clearcommand: Linux: system("clear"); Windows: system(cls);The current board will then be displayed using the characters |,-, O, X, and space. Use O torepresent the computers move and X the players. You need not use loops to draw the boardbut you get more elegant code when you draw it using a nested loop.Here is an example of what the board may look like:|----|----|----|| O | X | O ||----|----|----||| X |||----|----|----||| X |||----|----|----| playerTurn This function prompts the user to enter the row and column chosen. The messagedisplayed will look like the following:Enter you move's cell row and column : 10After the user has entered the row and column, you should check to make sure the cell is notalready occupied by a non-space character. If the space is occupied, display an error messageand ask the user to re-enter the row and column numbers. Record an X in the board array tomark the users move and increase turn count by 1.
computerTurn This function generates two random integers, the first for the row and the secondfor the column. If you use the rand function then this can be done as follows:row = rand()%3;col = rand()%3;You may also use the current time as a seed to random position numbers to be generated bythe computer.srand(time(0)); // Assuming you will use the rand() functionAfter the row and column are generated, you should check to make sure the cell is notalready occupied by a non-space character. If the space is occupied, then generate anotherpair of numbers for the row and column. Record an O in the board array to mark thecomputers move. checkWinner This function that goes through the board to see if there are any three alignedXs or Os. Alignment can be vertical, horizontal, or diagonal. Additional requirement: If a winner is found record in the second array record who won(computer, player, cat) in the first entry and how many moves it took (current play count) inthe second entry.If the user won, display a message that saysYou won in N turns.Otherwise, the message should taunt the user to the effect:You have been defeated by a computer in N turns.Use Cases to ConsiderDoes your program handle the following cases: Does your program handle the condition of a tie (Cat winning)? What happens in your program if the first player wins on turn 9 before the 2nd player gets achance to play. What happens if the number of games the user tries to play exceeds the size of your 2ndarray?

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

Oracle Autonomous Database In Enterprise Architecture

Authors: Bal Mukund Sharma, Krishnakumar KM, Rashmi Panda

1st Edition

1801072248, 978-1801072243

More Books

Students also viewed these Databases questions