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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
#include <iostream>
#include <string>
using namespace std;
int getPlayerInput(string playerName);
bool isLegalMove(int location, char board[], int boardSize);
void placeMarkOnBoard(char playerMark, int location, char board[], int boardSize);
void clearBoard(char board[], int boardSize);
bool hasThreeInRow(char PlayerMark, char board[], int boardSize);
bool isBoardFull(char board[], int boardSize);
void displayBoard (char board[], int boardSize);
bool playAgain ();
int const BOARDSIZE = 10;
int main(void)
{
char board[BOARDSIZE] = {'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
string player1;
string player2;
int XPlayerScore = 0;
int OPlayerScore = 0;
int NumberOfTies = 0;
int PlayerOneMove;
int PlayerTwoMove;
cout << "Please enter your name, player one: ";
cin >> player1;
cout << "Please enter your name, player two: ";
cin >> player2;
do{
do
{
displayBoard(board, BOARDSIZE);
PlayerOneMove = getPlayerInput(player1);
isLegalMove(PlayerOneMove, board, BOARDSIZE);
placeMarkOnBoard('X', PlayerOneMove, board, BOARDSIZE);
if (isLegalMove(PlayerOneMove, board, BOARDSIZE) == false)
{
cout << "That is an invalid move."<< endl;
}
hasThreeInRow('X', board, BOARDSIZE);
if (hasThreeInRow('X', board, BOARDSIZE) == true)
{
cout << endl << "Congratulations " << player1 << " you are the winner!" << endl;
XPlayerScore = XPlayerScore + 1;
cout << player1 << " has won " << XPlayerScore << " time(s)." <<endl;
cout << player2 << " has won " << OPlayerScore << " time(s)." << endl;
cout << "There has been " << NumberOfTies << " tie(s)." << endl;
clearBoard(board, BOARDSIZE);
break;
}
displayBoard(board, BOARDSIZE);
PlayerTwoMove = getPlayerInput(player2);
isLegalMove(PlayerTwoMove, board, BOARDSIZE);
if (isLegalMove(PlayerTwoMove, board, BOARDSIZE) == false)
{
cout << "That is an invalid move.";
}
placeMarkOnBoard('O', PlayerTwoMove, board, BOARDSIZE);
hasThreeInRow('O', board, BOARDSIZE);
if (hasThreeInRow('O', board, BOARDSIZE) == true)
{
cout << endl << "Congratulations " << player2 << " you are the winner!" << endl;
OPlayerScore = OPlayerScore + 1;
cout << player1 << " has won " << XPlayerScore << " time(s)." << endl;
cout << player2 << " has won " << OPlayerScore << " time(s)." << endl;
cout << "There has been " << NumberOfTies << " tie(s)." << endl;
clearBoard (board, BOARDSIZE);
break;
}
if (isBoardFull(board, BOARDSIZE) == true)
{
cout << endl << "The game has ended in a tie." << endl;
NumberOfTies = NumberOfTies + 1;
cout << player1 << " has won " << XPlayerScore << " time(s)." << endl;
cout << player2 << " has won " << OPlayerScore << " time(s) " << endl;
cout << "There has been " << NumberOfTies << " tie(s)." << endl;
clearBoard (board, BOARDSIZE);
break;
}
}
while (hasThreeInRow('X', board, BOARDSIZE) == false && hasThreeInRow ('O', board, BOARDSIZE) == false
&& isBoardFull(board, BOARDSIZE) == false);
}
while (playAgain() == true);
return 0;
}
int getPlayerInput(string playerName)
{
int move;
cout << playerName << ", where would you like to move next?: ";
cin >> move;
return move;
}
bool isLegalMove(int location, char board[], int boardSize)
{
if (location > boardSize || location <= 0)
{
return false;
}
else if (board[location] == board[location])
{
return true;
}
else
{
return false;
}
}
void placeMarkOnBoard(char playerMark, int location, char board[], int boardSize)
{
if (isLegalMove( location, board, boardSize) == true)
{
board[location] = playerMark;
}
}
void clearBoard(char board[], int boardSize)
{
for (int i = 1; i < boardSize; i++)
{
board[i] = i + 48;
}
}
bool hasThreeInRow(char playerMark, char board[], int boardSize)
{
if (board[1] == playerMark && board[2] == playerMark && board [3] == playerMark)
return true;
else if (board[4] == playerMark && board[5] == playerMark && board[6] == playerMark)
return true;
else if (board[7] == playerMark && board[8] == playerMark && board[9] == playerMark)
return true;
else if (board[1] == playerMark && board[4] == playerMark && board[7] == playerMark)
return true;
else if (board[2] == playerMark && board[5] == playerMark && board[8] == playerMark)
return true;
else if (board[3] == playerMark && board[6] == playerMark && board[9] == playerMark)
return true;
else if (board[7] == playerMark && board[5] == playerMark && board[3] == playerMark)
return true;
else if (board[9] == playerMark && board[5] == playerMark && board[1] == playerMark)
return true;
else
return false;
}
bool isBoardFull(char board[], int boardSize)
{
if (board[1] != '1' && board[2] != '2' && board[3] != '3' && board[4] != '4' && board[5] != '5' &&
board[6] != '6' && board[7] != '7' && board[8] != '8' && board [9] != '9')
return true;
else
return false;
}
void displayBoard(char board[], int boardSize)
{
cout << endl << " " << board[1] << " | " << board[2] << " | " << board[3] << endl;
cout << "---+---+---" << endl;
cout << " " << board[4] << " | " << board[5] << " | " << board[6] << endl;
cout << "---+---+---" << endl;
cout << " " << board[7] << " | " << board[8] << " | " << board[9] << endl;
cout << endl;
}
bool playAgain()
{
char response;
cout << endl << "Would you like to play again? Please enter Y or N: ";
cin >> response;
if (response == 'Y' || response == 'y')
return true;
else
return false;
}
|