Answered step by step
Verified Expert Solution
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 tictactoe against the computer.You may NOT USE THE C array or any STL container vector list, map, Your game program does the following: In the main function, declare a x twodimensional array of type char representing the board.Also, declare a second twod array of integers of size byor more with all positionsinitially assigned to record the winner and turns required to win reminder: do not use theC 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: systemclear; Windows: systemcls;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 : After the user has entered the row and column, you should check to make sure the cell is notalready occupied by a nonspace character. If the space is occupied, display an error messageand ask the user to reenter the row and column numbers. Record an X in the board array tomark the users move and increase turn count by
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;col rand;You may also use the current time as a seed to random position numbers to be generated bythe computer.srandtime; 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 nonspace 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 woncomputer 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 before the nd player gets achance to play. What happens if the number of games the user tries to play exceeds the size of your ndarray?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started