Question
Write a C++ program that allows two players to play a game of tic-tac-toe. Do not include TicTacToe.h, class TicTacToe, and public private classes. Use
Write a C++ program that allows two players to play a game of tic-tac-toe. Do not include TicTacToe.h, class TicTacToe, and public private classes. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that:
- Displays the contents of the board array
- Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row number and then the column number.
- Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row number and then the column number.
- Validates the user's input:
- Do not let the user input a row outside the range [1,3]
- Do not let the user input a column outside the range [1,3]
- Do not let the user input into a cell that already has an 'X' or 'O'
- If the user enters an invalid input, just ask them for that input again.
- This is best done using nested do-while loops. Below is some pseudo-code that will help with this:
do { do { output "Row: " input row; }while(row is outside range); do { output "Column: "; input col; }while(col is outside range); }while(cell at row, col contains 'X' or 'O');
-
Determines whether a player has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should declare that and end.
-
These are the following end game outputs:
- Player 1 wins!
- Player 2 wins!
- Tie!
-
Player 1 wins when there are three Xs in a row on the game board. The Xs can appear in a row, in a column, or diagonally across the board. The same logic applies to player 2, but with Os. A tie occurs when all of the locations on the board are full, but there is no winner.
Below is a sample output where player 1 wins:
1 2 3 1 * * * 2 * * * 3 * * * Player 1, Row: 1 Player 1, Column: 1 1 2 3 1 X * * 2 * * * 3 * * * Player 2, Row: 1 Player 2, Column: 2 1 2 3 1 X O * 2 * * * 3 * * * Player 1, Row: 2 Player 1, Column: 2 1 2 3 1 X O * 2 * X * 3 * * * Player 2, Row: 3 Player 2, Column: 1 1 2 3 1 X O * 2 * X * 3 O * * Player 1, Row: 3 Player 1, Column: 3 1 2 3 1 X O * 2 * X * 3 O * X Player 1 wins!
Next is a sample output where player 2 wins after ignoring some invalid input:
1 2 3 1 * * * 2 * * * 3 * * * Player 1, Row: 1 Player 1, Column: 1 1 2 3 1 X * * 2 * * * 3 * * * Player 2, Row: 3 Player 2, Column: 1 1 2 3 1 X * * 2 * * * 3 O * * Player 1, Row: 1 Player 1, Column: 2 1 2 3 1 X X * 2 * * * 3 O * * Player 2, Row: 1 Player 2, Column: 3 1 2 3 1 X X O 2 * * * 3 O * * Player 1, Row: 1 Player 1, Column: 3 Player 1, Row: 9 Player 1, Row: 9 Player 1, Row: 2 Player 1, Column: 1 1 2 3 1 X X O 2 X * * 3 O * * Player 2, Row: 2 Player 2, Column: 2 1 2 3 1 X X O 2 X O * 3 O * * Player 2 wins!
1 2 3 1 * * * 2 * * * 3 * * * Player 1, Row: 1 Player 1, Column: 1 1 2 3 1 X * * 2 * * * 3 * * * Player 2, Row: 2 Player 2, Column: 1 1 2 3 1 X * * 2 O * * 3 * * * Player 1, Row: 1 Player 1, Column: 2 1 2 3 1 X X * 2 O * * 3 * * * Player 2, Row: 2 Player 2, Column: 2 1 2 3 1 X X * 2 O O * 3 * * * Player 1, Row: 2 Player 1, Column: 3 1 2 3 1 X X * 2 O O X 3 * * * Player 2, Row: 1 Player 2, Column: 3 1 2 3 1 X X O 2 O O X 3 * * * Player 1, Row: 3 Player 1, Column: 1 1 2 3 1 X X O 2 O O X 3 X * * Player 2, Row: 3 Player 2, Column: 2 1 2 3 1 X X O 2 O O X 3 X O * Player 1, Row: 3 Player 1, Column: 3 1 2 3 1 X X O 2 O O X 3 X O X Tie!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started