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 171 172 173 174 175 176 177
|
bool Board::isGameOver(int &tileCounter, int &playerWins1, int &PlayerLose1, int &playerWins2, int &playerLose2, int &tie)
{
bool gameOver1 = false; // If there are 3 X's.
bool gameOver2 = false; // If there are 3 O's.
bool tieGame = false;
bool noGameOver; // If there are no 3 X's or 3 O's.
if(board[0][0] == 'X' && board[0][1] == 'X' && board [0][2] == 'X')
{
gameOver1 = true;
}
else if(board[1][0] == 'X' && board[1][1] == 'X' && board [1][2] == 'X')
{
gameOver1 = true;
}
else if(board[2][0] == 'X' && board[2][1] == 'X' && board [2][2] == 'X')
{
gameOver1 = true;
}
else if(board[0][0] == 'X' && board[1][0] == 'X' && board [2][0] == 'X')
{
gameOver1 = true;
}
else if(board[0][1] == 'X' && board[1][1] == 'X' && board [2][1] == 'X')
{
gameOver1 = true;
}
else if(board[0][2] == 'X' && board[1][2] == 'X' && board [2][2] == 'X')
{
gameOver1 = true;
}
else if(board[0][0] == 'X' && board[1][1] == 'X' && board [1][2] == 'X')
{
gameOver1 = true;
}
else if(board[0][2] == 'X' && board[1][1] == 'X' && board [2][0] == 'X')
{
gameOver1 = true;
}
else if(board[0][0] == 'O' && board[0][1] == 'O' && board [0][2] == 'O')
{
gameOver2 = true;
}
else if(board[1][0] == 'O' && board[1][1] == 'O' && board [1][2] == 'O')
{
gameOver2 = true;
}
else if(board[2][0] == 'O' && board[2][1] == 'O' && board [2][2] == 'O')
{
gameOver2 = true;
}
else if(board[0][0] == 'O' && board[1][0] == 'O' && board [2][0] == 'O')
{
gameOver2 = true;
}
else if(board[0][1] == 'O' && board[1][1] == 'O' && board [2][1] == 'O')
{
gameOver2 = true;
}
else if(board[0][2] == 'O' && board[1][2] == 'O' && board [2][2] == 'O')
{
gameOver2 = true;
}
else if(board[0][0] == 'O' && board[1][1] == 'O' && board [1][2] == 'O')
{
gameOver2 = true;
}
else if(board[0][2] == 'O' && board[1][1] == 'O' && board [2][0] == 'O')
{
gameOver2 = true;
}
if(gameOver1 == true)
{
if(PlayerLetter1 == 'X')
{
playerWins1++;
playerLose2++;
cout << "Game over! Player 1 is the victor! " << endl;
return gameOver1;
}
else if(PlayerLetter2 == 'X')
{
playerWins2++;
PlayerLose1++;
cout << "Game over! Player 2 is the victor! " << endl;
return gameOver1;
}
}
else if(gameOver2 == true)
{
if(PlayerLetter1 == 'O')
{
playerWins1++;
playerLose2++;
cout << "Game over! Player 1 is the victor! " << endl;
return gameOver2;
}
else if(PlayerLetter2 == 'O')
{
playerWins2++;
PlayerLose1++;
cout << "Game over! Player 2 is the victor! " << endl;
return gameOver2;
}
}
else if(tileCounter == 9)
{
tieGame = true;
tie++;
cout << "Tie game! No one wins! " << endl;
return tieGame;
}
else if(gameOver1 && gameOver2 == false)
{
noGameOver = false;
}
return noGameOver;
}
void Board::refresh()
{
char newTile = ' '; // gets rid of all the X's and O's
for(unsigned int i = 0; i < 3; i++)
for(unsigned int j = 0; j < 3; j++)
board[i][j] = newTile;
}
void Board::playAgain(char &playerResponse1, char &playerResponse2)
{
cout << "\n\nWould you like to play again player1? [y/n] ";
cin >> playerResponse1;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
while(toupper(playerResponse1) != 'Y' && toupper(playerResponse1) != 'N')
{
cerr << "\nError, Invalid response. ";
cin >> playerResponse1;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "\n\nWould you like to play again player2? [y/n] ";
cin >> playerResponse2;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
while(toupper(playerResponse2) != 'Y' && toupper(playerResponse2) != 'N')
{
cerr << "\nError, Invalid response. ";
cin >> playerResponse2;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
if(toupper(playerResponse1) == 'Y' && toupper(playerResponse2) == 'Y')
{
cout << "\n\nCreating a new board to play on. " << endl;
refresh(); // refreshes the board.
}
else
cout << "\n\nThanks for playing! " << endl;
}
void Board::scoreBoard(int &playerWins1, int &PlayerLose1, int &playerWins2, int &playerLose2, int &tie)
{
cout << "\t\t\tScoreBoard " << endl << endl;
cout << "--------------------------------------------------------------" << endl;
cout << " \t\tWins\t\tLosses " << endl << endl;
cout << "player 1: " << "\t\t\t" << playerWins1 << "\t\t" << PlayerLose1 << endl;
cout << "player 2: " << "\t\t\t" << playerWins2 << "\t\t" << playerLose2 << endl << endl;
cout << "Number of tie games: " << tie << endl;
cout << "--------------------------------------------------------------" << endl;
}
|