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
|
void Input(char player);
bool hasWon = false;
bool isTie = false;
bool isPlaying = true;
void updateBoard9(char b[3][3], int dimension, char player, char currentMove, char currentMoveColumn);
void updateBoard16(char b[4][4], int dimension, char player, char currentMove);
void updateBoard25(char b[5][5], int dimension, char player, char currentMove);
char player = 'X';
char player2 = 'O';
char activePlayer = player;
char currentMove;
char currentMoveColumn;
int main()
{
int dimension;
int three = 3;
int four = 4;
int five = 5;
cout << "Please select a board size by inputting a full-number integer between 3 and 5" << endl;
cout << "Input 3 for a 3x3 board" << endl;
cout << "Input 4 for a 4x4 board" << endl;
cout << "Input 5 for a 5x5 board" << endl;
cin >> dimension;
if (dimension == three){
char b[3][3] = { { '-', '-', '-' }, { '-', '-', '-' }, { '-', '-', '-' } };
displayBoard9(b, 3);
while (isPlaying && !hasWon && !isTie)
{
Input(activePlayer);
char tile = b[currentMove][currentMoveColumn];
if (currentMove < '0' || currentMove > '2' || currentMoveColumn < '0' || currentMoveColumn > '2'){
cout << "Invalid Input." << endl;
}
else
{
if (tile == 'X' || tile == 'O'){
cout << "Invalid Move" << endl;
}
else
{
updateBoard9(b, 3, activePlayer, currentMove, currentMoveColumn);
checkWin3(b);
//checkTie();
}
}
if (hasWon){
system("pause");
cout << "A player has obtained victory." << endl;
}
else
displayBoard9(b, 3);
if (activePlayer == player) activePlayer = player2;
else activePlayer = player;
}
} if (dimension == four){
char b[04][04] = { { '-', '-', '-', '-' }, { '-', '-', '-', '-' }, { '-', '-', '-', '-' }, { '-', '-', '-', '-' } };
displayBoard16(b, 4);
while (isPlaying && !hasWon && !isTie)
{
Input(activePlayer);
char tile = b[0][currentMove - '1'];
if (currentMove < '1' || currentMove > '16')
cout << "Invalid Input." << endl;
else
{
if (tile == 'X' || tile == 'O')
{
cout << "Invalid Move" << endl;
}
else
{
updateBoard16(b, 4, activePlayer, currentMove);
checkWin4(b);
//checkTie();
}
}
if (hasWon){
system("pause");
cout << "A player has obtained victory." << endl;
}
else
displayBoard16(b, 4);
if (activePlayer == player) activePlayer = player2;
else activePlayer = player;
}
} if (dimension == five){
char b[05][05] = { { '-', '-', '-', '-', '-' }, { '-', '-', '-', '-', '-' }, { '-', '-', '-', '-', '-' }, { '-', '-', '-', '-', '-' }, { '-', '-', '-', '-', '-' } };
displayBoard25(b, 5);
while (isPlaying && !hasWon && !isTie)
{
Input(activePlayer);
char tile = b[0][currentMove - '1'];
if (currentMove < '1' || currentMove > '25')
cout << "Invalid Input." << endl;
else
{
if (tile == 'X' || tile == 'O')
{
cout << "Invalid Move" << endl;
}
else
{
updateBoard25(b, 5, activePlayer, currentMove);
checkWin5(b);
//checkTie();
}
}
if (hasWon){
system("pause");
cout << "A player has obtained victory." << endl;
}
else
displayBoard25(b, 5);
if (activePlayer == player) activePlayer = player2;
else activePlayer = player;
}
}
else {
cout << "That input is not valid" << endl;
return 0;
}
}
//
//End of int-main
void displayBoard9(char b[][3], int dimension)
{
cout << "Tic-Tac-Toe 3x3" << endl;;
for (int x = 0; x < dimension; x++)
{
for (int y = 0; y < dimension; y++)
{
cout << b[x][y];
cout << " ";
}
cout << endl;
}
cout << endl;
}
void displayBoard16(char b[][4], int dimension)
{
cout << "Tic-Tac-Toe 4x4" << endl;;
for (int x = 0; x < dimension; x++)
{
for (int y = 0; y < dimension; y++)
{
cout << b[x][y];
cout << " ";
}
cout << endl;
}
cout << endl;
}
void displayBoard25(char b[][5], int dimension)
{
cout << "Tic-Tac-Toe 5x5" << endl;
for (int x = 0; x < dimension; x++)
{
for (int y = 0; y < dimension; y++)
{
cout << b[x][y];
cout << " ";
}
cout << endl;
}
cout << endl;
}
void updateBoard9(char b[3][3], int dimension, char player, char currentMove, char currentMoveColumn)
{
if (currentMove > '-1' && currentMove <= '2' && currentMoveColumn > '-1' && currentMoveColumn<= '2')
b[currentMove][currentMoveColumn] = player;
}
void updateBoard16(char b[4][4], int dimension, char player, char currentMove)
{
if (currentMove > '0' && currentMove <= '16')
b[0][currentMove - '1'] = player;
}
void updateBoard25(char b[5][5], int dimension, char player, char currentMove)
{
if (currentMove > '0' && currentMove <= '25')
b[0][currentMove - '1'] = player;
}
void Input(char player)
{
cout << "Enter your move, starting with the row and then column value of the desired space " << player << ": ";
cin >> currentMove;
cin >> currentMoveColumn;
}
|