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
|
bool checkForGameEnd (string board) {
//check if all board positions are full, in which case game is over
//count the number of filled positions
int positionsFilled = 0;
for (int pos = 0; pos < boardSize; pos++) {
if ((board[pos] == 'X' || (board[pos] == 'O'))) {
positionsFilled++;
}
}
if (board[0] == board[1] && board[1] == board[2]) {
if (board[0] == 'X') {
cout << endl << "GAME OVER, PLAYER X WINS!" << endl;
return true;
}
else if (board[0] == 'O') {
cout << endl << "GAME OVER, PLAYER O WINS!" << endl;
return true;
}
}
if (board[3] == board[4] && board[4] == board[5]) {
if (board[3] == 'X') {
cout << endl << "GAME OVER, PLAYER X WINS!" << endl;
return true;
}
else if (board[3] == 'O') {
cout << endl << "GAME OVER, PLAYER O WINS!" << endl;
return true;
}
}
if (board[6] == board[7] && board[7] == board[8]) {
if (board[6] == 'X') {
cout << endl << "GAME OVER, PLAYER X WINS!" << endl;
return true;
}
else if (board[6] == 'O') {
cout << endl <<"GAME OVER, PLAYER O WINS!" << endl;
return true;
}
}
if (board[0] == board[3] && board[3] == board[6]) {
if (board[0] == 'X') {
cout << endl <<"GAME OVER, PLAYER X WINS!" << endl;
return true;
}
else if (board[0] == 'O') {
cout << endl << "GAME OVER, PLAYER O WINS!" << endl;
return true;
}
}
if (board[1] == board[4] && board[4] == board[7]) {
if (board[1] == 'X') {
cout << endl <<"GAME OVER, PLAYER X WINS!" << endl;
return true;
}
else if (board[1] == 'O') {
cout << endl << "GAME OVER, PLAYER O WINS!" << endl;
return true;
}
}
if (board[2] == board[5] && board[5] == board[8]) {
if (board[2] == 'X') {
cout << endl << "GAME OVER, PLAYER X WINS!" << endl;
return true;
}
else if (board[2] == 'O') {
cout << endl << "GAME OVER, PLAYER O WINS!" << endl;
return true;
}
}
if (board[0] == board[4] && board[4] == board[8]) {
if (board[0] == 'X') {
cout << endl << "GAME OVER, PLAYER X WINS!" << endl;
return true;
}
else if (board[0] == 'O') {
cout << endl << "GAME OVER, PLAYER O WINS!" << endl;
return true;
}
}
if (board[2] == board[4] && board[4] == board[6]) {
if (board[2] == 'X') {
cout << endl << "GAME OVER, PLAYER X WINS!" << endl;
return true;
}
else if (board[2] == 'O') {
cout << endl << "GAME OVER, PLAYER O WINS!" << endl;
return true;
}
}
if (positionsFilled == boardSize) {
cout << endl << "GAME OVER, TIE GAME" << endl;
return true;
}
//otherwise the game is not over
return false;
}
bool playGameAgain(){
char choice;
cout << "Would you like to play again (Y or N) ?";
cin >> choice;
if ((choice == 'Y') || (choice == 'y')) {
return true;
cout << endl << endl;
}
else if ((choice == 'N') || (choice == 'n')) {
return false;
}
}
|