Question: In C++ Objective: To write a program to allow a game of Tic Tac Toe to be played, and to determine when the game is

In C++

Objective: To write a program to allow a game of Tic Tac Toe to be played, and to determine when the game is over

Complete the TicTacToe program below. It will allow for a full game of Tic Tac Toe between two players, and it will tell you who won or if the game is over with no winner.

Details:

The TicTacToe board is stored in a 3x3 array of characters containing spaces at the start of the game (an empty board). Each time a player makes a move, an 'X' or 'O' is put in the proper location in the array.

Your "clear" function should initialize the board to store spaces

Your "display" function should display the board, with gridlines between each position (using the characters '-' and '|', which are not stored in the array)

Your "takeTurn" function should ask the user to enter amove, and check for errors in input. Specifically, you should check for the following 2 kinds of errors:

Row or Column is out of range (less than 0 or greater than 2)

Specified position is already occupied (a player can only move to an empty spot)

If either of the above errors are found, the same player should be asked to re-enter their move until they get it right.Your "winner" function should examine the board and return one of the following characters:

' ' (a space) meaning the game is not yet over

'X' meaning that player X has won

'O' meaning that player O has won

'?' meaning that the game is over because the board is full, but no one won.

I recommend you write some helper functions to help with all the various pieces of this program. Remember you should never copy and paste code if you can avoid it. Write a function to perform a common, generalizable task, and call that function every time you need it.

As always, don't use any global variables.

Following is the beginning of the TicTacToe program and an example of how your program should behave. You should copy this TicTacToe.cpp file exactly and use it as a starting point and to test your code once you've finished it.

 #include using namespace std; /////// clear //////// // Intialize the board for the beginning of a game. void clear(char b[][3]); /////// display //////// // Display the current status of the board on the // screen, using hyphens (-) for horizontal lines // and pipes (|) for vertical lines. void display(const char b[][3]); /////// takeTurn //////// // Allow the nextPlayer to take a turn. // Send output to screen saying whose turn // it is and specifying the format for input. // Read user's input and verify that it is a // valid move. If it's invalid, make them // re-enter it. When a valid move is entered, // put it on the board. void takeTurn(char b[][3], char& nextPlayer); /////// winner ///////// // Examines the board and returns one of the following: // ' ' (a space) meaning the game is not yet over // 'X' meaning that player X has won // 'O' meaning that player O has won // '?' meaning that the game is over because the board // is full, but no one won. char winner(char board[][3]); /////// main //////// // No changes needed in this function. // It declares the variables, initializes the game, // and plays until someone wins or the game becomes unwinnable. int main() { char board[3][3]; char nextPlayer; char winningPlayer; clear(board); nextPlayer = 'X'; display(board); do { takeTurn(board, nextPlayer); display(board); winningPlayer = winner(board); }while(winningPlayer == ' '); if (winningPlayer == '?') cout << "Nobody won. Please play again. "; else cout << "Congratulations, " << winningPlayer << " YOU WON! "; return 0; } /* Sample Output: $ a.out | | ----- | | ----- | | It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. -1 0 Invalid entry: row and column must both be between 0 and 2 (inclusive). Please try again. It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 3 Invalid entry: row and column must both be between 0 and 2 (inclusive). Please try again. It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 0 X| | ----- | | ----- | | It is now O's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 0 Invalid entry: Row 0 at Column 0 already contains: X Please try again. It is now O's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 1 X|O| ----- | | ----- | | It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 0 Invalid entry: Row 0 at Column 0 already contains: X Please try again. It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 1 Invalid entry: Row 0 at Column 1 already contains: O Please try again. It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 1 1 X|O| ----- |X| ----- | | It is now O's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 2 X|O|O ----- |X| ----- | | It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 2 2 X|O|O ----- |X| ----- | |X Congratulations, X YOU WON! $ a.out | | ----- | | ----- | | It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 2 | |X ----- | | ----- | | It is now O's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 0 O| |X ----- | | ----- | | It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 1 2 O| |X ----- | |X ----- | | It is now O's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 1 0 O| |X ----- O| |X ----- | | It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 2 2 O| |X ----- O| |X ----- | |X Congratulations, X YOU WON! $ a.out | | ----- | | ----- | | It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 0 X| | ----- | | ----- | | It is now O's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 1 1 X| | ----- |O| ----- | | It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 2 X| |X ----- |O| ----- | | It is now O's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 1 X|O|X ----- |O| ----- | | It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 2 1 X|O|X ----- |O| ----- |X| It is now O's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 1 0 X|O|X ----- O|O| ----- |X| It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 2 2 X|O|X ----- O|O| ----- |X|X It is now O's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 1 2 X|O|X ----- O|O|O ----- |X|X Congratulations, O YOU WON! $ a.out | | ----- | | ----- | | It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 2 2 | | ----- | | ----- | |X It is now O's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 2 1 | | ----- | | ----- |O|X It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 2 0 | | ----- | | ----- X|O|X It is now O's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 0 O| | ----- | | ----- X|O|X It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 1 O|X| ----- | | ----- X|O|X It is now O's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 0 2 O|X|O ----- | | ----- X|O|X It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 1 0 O|X|O ----- X| | ----- X|O|X It is now O's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 1 1 O|X|O ----- X|O| ----- X|O|X It is now X's turn. Please enter your move in the form row column. So 0 0 would be the top left, and 0 2 would be the top right. 1 2 O|X|O ----- X|O|X ----- X|O|X Nobody won. Please play again.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!