I'm done with my tic-tac-toe program, but now I want to ask a simple question to the winner of the match to make the victory absolute. Would I need a while loop? I also feel my code is too messy, how can I enhance/improve it? Thank you!
#include <iostream>
using namespace std;
bool checkWin (char);
void displayMap ();
char display [5][5] = { { ' ', '|', ' ', '|', ' ' }, { '-', '+', '-', '+', '-' }, { ' ', '|', ' ', '|', ' ' }, { '-', '+', '-', '+', '-' }, { ' ', '|', ' ', '|', ' ' } };
int main ()
{
system("color f4");
cout << "Welcome to Kevin Castillo's Tic-Tac-Toe C++ Game...Good Luck...haha!\n"
<<endl;
char map [3][3] = { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } },
player[2] = { ' ', ' ' },
again = 'y';
bool gameWon = false;
while (again != 'n')
{
int turns = 0;
cout << "Player 1 enter the character you are going to use (no spaces): ";
cin >> player[0];
while (player[0] == ' ' || player[0] == ' ')
{
cout << "You entered a space (no spaces), re-enter a new character: ";
cin >> player[0];
}
cout << endl << "Player 2 enter the character you are going to use (no spaces): ";
cin >> player[1];
while (player[1] == ' ' || player[1] == ' ')
{
cout << "You entered a space (no spaces), re-enter a new character: ";
cin >> player[1];
}
while (turns != 9 && !gameWon)
{
unsigned short x, y;
cout << endl << endl;
bool p1turn = true, p2turn = true;
while (p1turn && turns != 9)
{
p1turn = false;
displayMap();
cout << endl << endl << "Player 1: Enter 2 numbers (1-3) separated by a space to use the place: ";
cin >> x >> y;
while (x > 3 || x < 1 || y > 3 || y < 1)
{
cout << "You must enter numbers from 1 to 3, please re-enter them: ";
cin >> x >> y;
}
if (map[x - 1][y - 1] != ' ')
{
p1turn = true;
cout << "You entered a coordinate that was already taken." << endl << endl;
}
else
{
map[x - 1][y - 1] = player[0];
x = (x * 2) - 1;
y = (y * 2) - 1;
display[x - 1][y - 1] = player[0];
if (checkWin(player[0])) gameWon = true;
turns++;
}
}
while (p2turn && turns != 9 && !gameWon)
{
p2turn = false;
cout << endl;
displayMap();
cout << endl << endl << "Player 2: Enter 2 numbers (1-3) separated by a space to use the place: ";
cin >> x >> y;
while (x > 3 || x < 1 || y > 3 || y < 1)
{
cout << "You must enter numbers from 1 to 3, please re-enter them: ";
cin >> x >> y;
}
if (map[x - 1][y - 1] != ' ')
{
p2turn = true;
cout << "You entered a coordinate that was already taken." << endl << endl;
}
else
{
map[x - 1][y - 1] = player[1];
x = (x * 2) - 1;
y = (y * 2) - 1;
display[x - 1][y - 1] = player[1];
if (checkWin(player[1])) gameWon = true;
turns++;
}
}
}
cout << endl << endl;
displayMap();
if (checkWin(player[0]))
cout << endl << endl << "And the winner is: Player 1!!!" << endl << endl;
else if (checkWin(player[1]))
cout << endl << endl << "And the winner is: Player 2!!!" << endl << endl;
else cout << endl << endl << "And it looks like we have a draw folks. Cats Game." << endl << endl;
cout << "Play again? (y/n): ";
cin >> again;
if (again != 'n')
{
for (int x = 1; x <= 3; x++)
for (int y = 1; y <= 3; y++)
{
map[x - 1][y - 1] = ' ';
display[(x * 2) - 2][(y * 2) - 2] = ' ';
}
}
}
cout << endl << endl;
system("Pause");
return 0;
}
bool checkWin (char player)
{
bool result = false;
for (int x = 0; x < 5; x += 2)
{
bool thisResult = true;
for (int y = 0; y < 5; y += 2)
thisResult = thisResult && display[x][y] == player;
result = result || thisResult;
}
for (int x = 0; x < 5; x += 2)
{
bool thisResult = true;
for (int y = 0; y < 5; y += 2)
thisResult = thisResult && display[y][x] == player;
result = result || thisResult;
}
bool thisResult[2] = { true, true };
int y;
for (int x = 0; x < 5; x += 2)
{
y = 4 - x;
thisResult[0] = thisResult[0] && display[x][y] == player;
}
result = result || thisResult[0];
for (int x = 0; x < 5; x += 2)
{
y = x;
thisResult[0] = thisResult[0] && display[x][y] == player;
}
result = result || thisResult[0];
return result;
}
void displayMap ()
{
cout << " 1 2 3" << endl << " " << endl;
for (int x = 0; x < 5; x++)
{
if (x != 1 && x != 3) cout << (x + 2) / 2 << " ";
else cout << " ";
for (int y = 0; y < 5; y++)
cout << display[x][y];
cout << endl;
}
}