Question: Tic-Tac-Toe Overview In this section you will be recreating the Tic-Tac-Toe game in C++ using console input and output. In the process you will: Make
Tic-Tac-Toe
Overview
In this section you will be recreating the Tic-Tac-Toe game in C++ using console input and output.
In the process you will:
Make use of control and repetition structures for input validation.
Make use of bitwise operators to handle the underlying bits in a variable.
Apply formatting rules to console output.
Replicate the gameplay of the Tic-Tac-Toe game.
Introduction to Tic-Tac-Toe
Tic-Tac-Toe is a kid's game consisting of a 3x3 grid and two players. The players, X and O, take turns marking a spot in the grid. The player who places three marks in a horizontal, vertical, or diagonal row wins the game. It is also possible for the game to end in a tie. See an example below.
If you wish to play the game click this link.
Your Task
Your task in this session is to write a program that allows the user to play Tic-Tac-Toe against the computer. You let the player decide whether he wants to start the match or let the computer start it. The player who starts the match is X. The other player is O.
Let's assume we have the match below. From the top grid it is clear that player O won the match. The grid on the bottom is the match's binary representation. We are using a one to represent X's marks and a zero to represent O's marks. We use the ?'s to represent values that have not been assigned.

We then assign a label to each row and column to make it easier to locate where the players' marks are.

We have:
Row 1: The row containing the binary representation 101.
Row 2: The row containing the binary representation 011.
Row 3: The row containing the binary representation 000.
You might have noticed that our Tic-Tac-Toe uses three bits per row to represent a match. This means that each row can be represented by an integer number between 0 and 7. The picture below shows all of these numbers and their binary representation.

Each of the rows above can be easily represented with a variable that holds an integer number. An appropriate data type for this task would be unsigned short or unsigned int. A variable of type unsigned short can hold up to 2 bytes of information. A variable of type unsigned int can hold up to 4 bytes of information. Because we only need three bits per row we can use three unsigned short variables.
Problem Solving Steps
Who goes first?: Your program must ask if the user wants to go first. If the user does not want to go first then the computer starts as X.
Tic-Tac-Toe Would you like to go first? (Y/N): Y
User's move: Your program must ask the user to place his token on the board. The board spaces are numbered so that the user can tell you where to place it in the board. Remember to validate the user's input to enforce that the value chosen is in the board.
You start as X. I am O. Tic-Tac-Toe Board: C1 C2 C3 +---+---+---+ R1 | 1 | 2 | 3 | +---+---+---+ R2 | 4 | 5 | 6 | +---+---+---+ R3 | 7 | 8 | 9 | +---+---+---+ Select the square number you want to place an X on: 1
Computer's move: The computer randomly selects a position in the board to place its token and performs the move. (Hint: use the rand() function to generate random numbers) It prints the new resulting board. It also prompts the user to keep playing if the game has not ended due to a tie or a win.
Ok. I'll proceed. My turn. Tic-Tac-Toe Board: C1 C2 C3 +---+---+---+ R1 | X | 1 | 2 | +---+---+---+ R2 | 3 | 4 | 5 | +---+---+---+ R3 | 6 | 7 | O | +---+---+---+ Select the square number you want to place an X on:
Game ends: If the game ends because a player wins, then the condition for winning will be displayed. Once the game ends, you need to ask the user if he wants to play again.
Ok. I'll proceed. My turn. Third Column Same Who won?: O Tic-Tac-Toe Board: C1 C2 C3 +---+---+---+ R1 | X | 1 | O | +---+---+---+ R2 | X | X | O | +---+---+---+ R3 | 2 | 3 | O | +---+---+---+ Would you like to play again? (Y/N): Y
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
