Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Instructions of the question (later there is a skeleton of the code which must be used): (thank you) In this lab, you will write an

Instructions of the question (later there is a skeleton of the code which must be used): (thank you)

In this lab, you will write an application that allows two users to play Tic-tac-toe against each other. By the end of this lab, you should be able to:

Accept and validate user input from the keyboard. Declare and define multiple functions. Declare, initialize, and modify a one-dimensional array. Pass around an array to different functions.

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 represents Player B. Specifically, 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]

2

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 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 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, thenitwillreturn 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 pseudocode in Algorithm 1 to help you get started. When printing who- evers 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!

3

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

3

gameOver No while gameOver = Y es 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 = requestV alidInput(...)

Update game board. Update currentP layer.

winner = checkF orW inner(...) if a winner exists then

gameOver Y es else if checkF orStalemate(...) then

gameOver Y es end if

end while print Whoever won the game, or if it was a draw.

printBoard(...) 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: 

A23 456 789

It is Player Bs turn. Please enter a valid position to play. 

4

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: 

A2B 456 789

It is Player As turn. Please enter a valid position to play. 4 The current state of the Tic-tac-toe Board: 

A2B A56 789

It is Player Bs turn. Please enter a valid position to play. 6 The current state of the Tic-tac-toe Board: 

A2B A5B 789

It is Player As turn. Please enter a valid position to play. 7 Player A wins! 

A2B A5B A89

The the skeleton of the code: (must be done like this)

#include

/**

* Call this function to display the game board on the terminal.

*

* @param board An array that represents the current state of the game.

* @param playerA A number that represents Player A.

* @param playerB A number that represents Player B.

*/

void printBoard(int board[], int playerA, int playerB) {

// TODO: Implement this function.

}

/**

* Call this function to accept valid input from the user via the keyboard.

*

* Valid input from the user is a number between 1 and 9, which represents

* a location on the game board. In addition, that location must not have

* already been claimed by Player A or Player B.

*

* This function will only return once valid input has been entered by the

* user.

*

* @param board An array that represents the current state of the game.

* @param playerA A number that represents Player A.

* @param playerB A number that represents Player B.

*

* @return An index to the array that is valid.

*/

int requestValidInput(int board[], int playerA, int playerB) {

// TODO: Implement this function.

return 0; // TODO: Fix me.

}

/**

* Call this function to check who, if anyone, has won the game.

*

* @param board An array that represents the current state of the game.

* @param playerA A number that represents Player A.

* @param playerB A number that represents Player B.

*

* @return 0 if neither Player A nor Player B has won the game. Otherwise,

* the function will return the player who has won.

*/

int checkForWinner(int state[], int playerA, int playerB) {

// TODO: Implement this function.

return 0; // TODO: Fix me.

}

int main(void) {

// TODO: See Algorithm 1 in the lab handout to get you started here.

return 0;

}

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

Students also viewed these Databases questions

Question

=+What would you prioritise?

Answered: 1 week ago

Question

2. What is the difference between an embellishment and a lie?

Answered: 1 week ago