Reputation:
I'm trying to make a program that uses a 2D Array to play Tic Tac Toe. I set up a while loop, and two other while loops within it to pass turns back and forth until victory conditions are met. Basically the while loop keeps going until one of the victory conditions is met (which happen immediately after one complete turn of play). The other two while loops within it represent turns. So if legal parameters are met, player one takes their turn, then player two takes their turn, and so on. If parameters aren't met, that's fine, it'll just go back to the top of the loop for that turn so they can try again. Here's my code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char TicTacToe [3][3] = {0}; // This initializes an array of "e's" for empty space.
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
TicTacToe [i][j] = 'e';
}
}
for (int i = 0; i < 3; i++) // This outputs the game board so the user(s) can get a visual.
{
for (int j = 0; j < 3; j++)
{
cout << TicTacToe [i][j] << " ";
}
cout << "\n";
}
bool flag = true;
bool turn1flag = true;
bool turn2flag = true;
int p1row, p1col, p2row, p2col = 0;
cout << "\n";
cout << "Welcome to Tic-Tac-Toe. The game board is shown above. " << endl;
cout << "The rows are numbered one through three from top to bottom. " << endl;
cout << "The columns are numbered one through three from left to right. " << endl;
cout << "Player one's spaces are indicated by an 'o.'" << endl;
cout << "Player two's spaces are indicated by an 'x.'" << endl;
cout << "Empty spaces are indicated by an 'e.'" << endl << endl;
while (flag == true) // This while loop starts the game
{
while (turn1flag == true)
{
cout << "It is player one's turn. Which row? " << endl;
cin >> p1row;
cout << "And which column? " << endl;
cin >> p1col;
if (TicTacToe[p1row-1][p1col-1] == 'e') // This checks if the selected spot is empty. If not, it's filled with the users symbol.
{
TicTacToe[p1row-1][p1col-1] = 'o';
for (int i = 0; i < 3; i++) // This outputs the game board so the user(s) can see their progress.
{
for (int j = 0; j < 3; j++)
{
cout << TicTacToe [i][j] << " ";
}
cout << "\n";
}
cout << "\n";
turn1flag = false;
}
else // If a position is taken or out of bounds, we're returned to the top of the turn loop.
{
cout << "That position is already taken or out of bounds. Try again. " << endl;
}
}
while (turn2flag == true)
{
cout << "It is player two's turn. Which row? " << endl;
cin >> p2row;
cout << "And which column? " << endl;
cin >> p2col;
if (TicTacToe[p2row-1][p2col-1] == 'e')
{
TicTacToe[p2row-1][p2col-1] = 'x';
for (int i = 0; i < 3; i++) // This outputs the game board so the user(s) can see their progress.
{
for (int j = 0; j < 3; j++)
{
cout << TicTacToe [i][j] << " ";
}
cout << "\n";
}
cout << "\n";
turn2flag = false;
}
else
{
cout << "That position is already taken or out of bounds. Try again. " << endl;
}
}
// All of the following are player one win conditions.
if ((TicTacToe [0][0] = 'o') && (TicTacToe [0][1] = 'o') && (TicTacToe [0][2] = 'o'))
{
cout << "Congratulations player one, you've won!" << endl;
flag = false;
}
... // the other 15 winning conditions for either player 1 or 2.
Upvotes: 3
Views: 1102
Reputation: 747
You use assignments in your if statement instead of comparisons. =
will return the value of the assignment, and as long as that value is something else than 0
it will be evaluated to true. Use ==
to compare two values instead.
Upvotes: 1
Reputation: 3400
You're never resetting the turn1flag
and turn2flag
back to true. As it's written, it looks like it will let each player have a single turn, then stop allowing turns. You'd need to either change lines like so:
turn1flag = false;
turn2flag = true;
and
turn2flag = false;
turn1flag = true;
Or just use a single flag and have the first while loop check if it's true, and the second check if it's false. Then the second loop can set it back to true after a move.
On top of that, you need to use ==
in the if statements, otherwise you're assigning values, which will automatically evaluate to true.
Upvotes: 0
Reputation: 263078
if ((TicTacToe [0][0] = 'o') && (TicTacToe [0][1] = 'o') && (TicTacToe [0][2] = 'o'))
Those are assignments. You want ==
instead of =
.
What happens here is that 'o'
is assigned and then used in a boolean context. Every visible character is treated as true
, so the condition is seen as true && true && true
, which is always true
.
Upvotes: 6