1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#include <iostream>
using namespace std;
bool checkWin (char);
void displayMap ();
char display [5][5] = { { ' ', '|', ' ', '|', ' ' }, { '-', '+', '-', '+', '-' }, { ' ', '|', ' ', '|', ' ' }, { '-', '+', '-', '+', '-' }, { ' ', '|', ' ', '|', ' ' } };
int main ()
{
system("color f4");
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;
}
}
| |