Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please develop the following code using C programming and u sing the functions, variables and code format that is specified and given below. Thanks very

Please develop the following code using C programming and using the functions, variables and code format that is specified and given below. Thanks very much in advance!

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 identies its position. Specically, 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 represents Player B. Specically, the functions signature is: void printBoard(int board[ ], 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 [space] and new lines are annoted as [newline], for your 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]6[newline] [space]7[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 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 functions 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 youve received from the user. This involves updating the game board (i.e., the integer array). Once updated, you should also switch to the next players 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 nish. 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 represents 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 functions 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 whoevers turn it is, use (where appropriate): It is Player As turn. It is Player Bs turn.

When printing the result of the game, use (where appropriate): Player A wins! Player B wins! Its a draw!

Algorithm 1 Pseudocode for Tic-tac-toe Require: Game board is initialized. Require: currentPlayer PlayerOne

gameOver No while gameOver 6 (not equal symbol) Yes do print The current state of the Tic-tac-to Board: printBoard(...)

print Whoevers turn it is. print Please enter a valid position to play. input = requestValidInput(...) Update game board. Update currentPlayer. winner = checkForWinner(...) if a winner exists then gameOver Yes else if checkForStalemate(...) then gameOver Yes 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 456 789 It is Player As turn. Please enter a valid position to play. 1 The current state of the Tic-tac-toe Board: A 2 3 4 5 6 7 8 9 It is Player Bs turn. Please enter a valid position to play. 1 That position has already been played, please try again. 10 Invalid input, please try again. 3 The current state of the Tic-tac-toe Board: A 2 B 4 5 6 7 8 9 It is Player As turn. Please enter a valid position to play. 4 The current state of the Tic-tac-toe Board: A 2 B A 5 6 7 8 9 It is Player Bs turn. Please enter a valid position to play. 6 The current state of the Tic-tac-toe Board: A 2 B A 5 B 7 8 9 It is Player As turn. Please enter a valid position to play. 7 Player A wins! A 2 B A 5 B A 8 9

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

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions