Question
Consider the following C++ program. It declares a 2D array of characters called board and uses a variety of loops to implement a very simple
Consider the following C++ program. It declares a 2D array of characters called \"board\" and uses a variety of loops to implement a very simple \"tic-tac-toe\" game. The program starts by reading a sequence of x's and o's from the user and displays them on the tic-tac-toe board. Then the program checks to see if x or o has three in a row anywhere. If so, the program prints who the winner is.
#include
#include
using namespace std;
int main()
{
// Declare 2D array
const int SIZE = 3;
char board[SIZE][SIZE];
// Read x's and o's
cout
for (int r = 0; r
for (int c = 0; c
cin >> board[r][c];
// Print 2D array
cout
for (int r = 0; r
{
cout
for (int c = 0; c
cout
cout
}
// Check board contains only x's and o's
bool valid = true;
// TBA
if (!valid)
{
cout
exit(1);
}
// Check first diagonal to see who wins
char winner = ' ';
if ((board[0][0] == board[1][1]) &&
(board[1][1] == board[2][2]))
winner = board[0][0];
// Check second diagonal to see who wins
// TBA
// Check rows to see who wins
for (int r = 0; r
if ((board[r][0] == board[r][1]) &&
(board[r][1] == board[r][2]))
winner = board[r][0];
// Check columns to see who wins
// TBA
// Print winner
if (winner != ' ')
cout
else
cout
return 0 ;
}
Step 1:Copy this program into your C++ program editor, and compile it. Hopefully you will not get any error messages.
Step 2:Run your program and type in \"helpmeout\". You will see the 3 by 3 tic-tac-toe board displayed with the characters above going L-R and T-B in the array. The program says there is no winner, but it really should say that the board was invalid because we did not enter x's and o's.
Step 3:Modify your program and replace the TBA comment with the correct code to check all nine array locations to make sure the user entered in only x's and o's (lower case). If they type in any other character, you should set the variable \"valid\" to false. When you have this code finished, test it with \"helpmeout\" to verify correctness.
Step 4:Now run the program and type in \"xoo oxo xox\" to initialize the tic-tac-toe array. The program should tell you that x is the winner. If you check the code, you will see that there is a section of code that checks that array locations [0][0], [1][1], and [2][2] on the first diagonal all have the value. Because the board has an x in all three of these locations, the program declares x to be the winner.
Step 5:Modify your program to add some code to check the second diagonal direction on the tic-tac-toe board to see if there is a winner. When you think you have it right, run the program and type in \"oxo xoo oxx\". You should see that o is the winner this time.
Step 6:Run your program again and type in \"oxo xox xxx\". You should see that x is the winner. In this case, the program found three values on one row that were equal to x. Take a look at the for loop for checking rows, and you will see that we loop over rows 0,1,2 and then check all three values match each other.
Step 7:You have probably noticed that the code to check columns for the winner is missing. Modify your program to add the necessary code to check all three columns. Make up some test data to verify that this is working. Once you have this going, you will have a simple tic-tac-toe game that will let you check who wins/loses after the board is all filled in. There is clearly much more needed to implement an interactive game, but we will leave that for another time.
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