Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This was attempted by someone but wasnt done properly. Please do not post that solution or any solution already on Chegg. 1 Summary of the

This was attempted by someone but wasnt done properly. Please do not post that solution or any solution already on Chegg. image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
1 Summary of the Game Tic-tac-toe is a two-player game where each player takes turns marking spaces in a 3x3 grid. In this case, Player A marks their space with 'A' and Player B marks their space with B'. A player wins when they manage to place three marks in a straight line horizontally vertically, or diagonally. It is possible that the game leads to a draw, where there are no places on the 3x3 grid left to mark and neither player has three marks in a straight line 2 The Game LOOp Game programming involves looping until a game over condition is met. In Tic-tac-toe, the game over condition occurs when one of the two players has won or there is a draw Dur ing the game loop, the program will: 1. Display the state of the game 2. Accept input from the user 3. Update the state of the game based on user input. 4. Check the gam e over condition. We will use integers to represent the state of the game. We will use functions to help with steps 1, 2, and 4. You can create additional functions if you would like to. 2.1 Representing the Game State The game board is a 3x3 grid, which wil be represented as a one-dimensional integer array with 9 elements. Each element of the array represents a position on the game board Each element is initialized to a number that identifies its position. Specifically, index 0 is initialized to 1, index 1 is initialized to 2, and so on. Player A and Player B are each represented by a unique integer. These integers should not be the same as the positions above (ie, should not be an integer between 1 and 9). 2.2 Display the Game Board You will write a function that displays the game board. The function will output the game board to the terminal (ie, via printf). The function will accept, as input, an array that represents the game board, a number that represents Player A, and a number that repre- sents Player B. Specifically, the function's signature is: void printBoardtint boardl. int playerA, int playerB) The printBoard function prints a new line, then the game board as a 3x3 grid, then another new line. An example of an empty game board where neither player has played yet is below. Spaces are annotated as Ispacel and new lines are annoted as [newlinel, for your convenience. [newline [space]1 [space]2 [space3[newline [space]4 [space]5[space [newline [space17[space]8 [space19[newline] [newline] If Player A has marked position 5 and Player B has yet to take their turn, then we would replace 5 with 'A for Player A: (nowlino tspace]1 [space]2 [apace] 3(newline] [space]4[spaceJA[space] 6[newline] (space [space] 8 [space] 9 [newline] [newline] 2.3 Accept User Input You will write a function that accepts user input. The function will read an integer from the keyboard (i.e, using scanf) and validate it. You can assume that the keyboard input will always be an integer that can be parsed by scanf. An integer is valid if 1. It is within the range of positions on the game board (i.e., between 1 and 9). 2. The position on the game board has not already been marked by one of the players. If the input is valid, then the function will return the aray index that has been selected by the ser. If the input is invalid, then the function will output an appropriate message and ask the user to try again. The function's signature is: int requestValidInput (int boardl. int playerA, int playerB) If the input is not within the range of positions on the game board, the function should ask the user to try again: Invalid input, please try again.[newline (wait for new input] Algorithm 1 Pseudocode for Tic-tac-toe Require: Game board is initialized. Require: current PlayerPlayerOne ganeOverNo while gammeuer Yes do print The current state of the Tic-tac-to Board:n printBoard print Whoever's turn it is. print Please enter a valid position to play.rn input requestVelidln) Update game board. Update current Player winner check ForWinner(.) if a winner exists then gameover Yes else if cheekForStalemate. then gameOverYes end if end while print Whoever won the game, or if it was a draw. printBoard.) 3 Example Game Output The following example shows how a game is played to completion. Spaces for the game board are shown explicitly as The current state of the Tic-tac-toe Board: 123 7,89 It is Player a's turn Please enter a valid position to play The current state of the Tic-tac-toe Board: It is Player B's turn. Please enter a valid position to play That position has already been played, please try again 10 Invalid input, please try again The current state of the Tic-tac-toe Board: A2B 7L89 It is Player A's turn Please enter a valid position to play The current state of the Tic-tac-toe Board: A 2B A56 7 89 It is Player B's turn Please enter a valid position to play The current state of the Tic-tac-toe Board: 789 It is Player A's turn Please enter a valid position to play Player A wins! LA,89

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions