Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer the question below. Its in parts. 1 Summary of the Game Tic-tac-toe is a two-player game where each player takes turns marking spaces

Please answer the question below. Its in parts.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage 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. During 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 game 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 will 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 (i.e., 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 (i.e., 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 printBoard (int boardl, int playerA, int playerB) The printBoard function prints a new ne, 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 [spacel and new lines are annoted as [newlinel, for vour convenience [newline] [space] 1 [space] 2 [space] 3 [newline] [space]4 [space]5 [space] 6 [newline] [space] 7 [space]8 [space] 9[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: [newline] [space] 1 [space] 2 [space] 3 [newline] [space] 4 [space]A [space] 7 [space]8 [space] 9[newline] [newline] [space] 6 [newline] 2.3 Accept User Input You will write a function that accepts user input. The function will read an integer from the keyboard (.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 array index that has been selected by the user. 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 board[], 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] If the input is within the range of positions on the game board, but that position has already been marked, the function should ask the user to try again: That position has already been played, please try again.[newline] [wait for new input] The requestValidInput function should continue to loop until valid input is given 2.4 Update Game State You should update the state of the game based on the valid input you've received from the user. This involves updating the game board (i.e., the integer array). Once updated, you should also switch to the next player's turn. You do not need to do this step in a function - it can be done directly in the game loop 2.5 Check for Game Over There are two ways the game can finish. Either one of the players has won or the game has ended in a draw. We will separate these into two functions. The easiest way to solve this is through brute force (it is only a 3x3 grid). To check if a player has won, write a function that 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. The function will return zero if neither player has won. Otherwise, it will return the number that represents the player who won the game. The function's signature is int checkForWinner (int board[], int playerA, int playerB) You should also check if the game has ended in a draw. One option is to create a function, as is the theme of this lab. For example, the function could check to see if there are any positions on the board that are unmarked. If so, then it will return true. Otherwise, all positions have been marked and the function will return false. There are other ways to approach this, so we leave the checking for a tie up to you. 2.6 Getting Started Consider the pseudo code in Algorithm 1 to help you get started. When printing who- ever's turn it is, use (where appropriate) e It is Player A's turn.In . It is Player B's turn. In When printing the result of the game, use (where appropriate) . Player A wins! n Player B wins!An . It's a draw!n Algorithm 1 Pseudocode for Tic-tac-toe Require: Game board is initialized. Require: current Player

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

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions