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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
#include <iostream>
#include <string>
using namespace std;
//array to create the rows and columns for which players may choose to put their symbols in from 1-9
char grid[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
//what the default player is
char player = 'X';
//Players names
string X = "Player1";
string O = "Player2";
//integer for when players tie
int b;
//Players can choose to play game or quit the game.
void Menu()
{
system("cls");
cout << "---------------------------------" << endl;
cout << " MAIN MENU" << endl;
cout << "---------------------------------" << endl;
cout << endl;
cout << "Hello! and welcome to tic-tac-toe!" << endl;
cout << endl;
cout << "To play the game, simply type 'y'." << endl;
cout << "Or if you wish to quit, type 'n'." << endl;
string choice1;
cout << "Enter here: ";
cin >> choice1;
if (choice1 == "Y" || choice1 == "y")
{
return;
}
else if (choice1 == "N" || choice1 == "n")
{
exit(0);
}
else
{
cout << "That is not the correct input, please try again." << endl;
system("PAUSE");
Menu();
}
}
//Players enter names
void Names()
{
system("cls");
cout << "Player 1 enter your name: ";
cin >> X;
cout << "Player 2 enter your name: ";
cin >> O;
}
//what the game will display
void Display()
{
//so that there is only one grid being displayed (clears previous display once an input is executed)
system("cls");
cout << "-------------------------------------------------------------------------------" << endl;
cout << "Welcome to Tic-Tac-Toe! Player 1: " << X << " Player 2: " << O << endl;
cout << "-------------------------------------------------------------------------------" << endl;
cout << X << " goes first." << endl;
//r = row c = column
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
cout << grid[r][c] << " ";
}
cout << endl;
}
}
//what the players will be inputting
void Input()
{
int a;
//players turn
cout << "It's " << player << "'s turn. " << "Type the number of the field: ";
cin >> a;
if (a == 1)
//if a field is already in use
{
if (grid[0][0] == '1')
grid[0][0] = player;
else
{
cout << "That field is already in use, please try again!" << endl;
Input();
}
}
else if (a == 2)
{
if (grid[0][1] == '2')
grid[0][1] = player;
else
{
cout << "That field is already in use, please try again!" << endl;
Input();
}
}
else if (a == 3)
{
if (grid[0][2] == '3')
grid[0][2] = player;
else
{
cout << "That field is already in use, please try again!" << endl;
Input();
}
}
else if (a == 4)
{
if (grid[1][0] == '4')
grid[1][0] = player;
else
{
cout << "That field is already in use, please try again!" << endl;
Input();
}
}
else if (a == 5)
{
if (grid[1][1] == '5')
grid[1][1] = player;
else
{
cout << "That field is already in use, please try again!" << endl;
Input();
}
}
else if (a == 6)
{
if (grid[1][2] == '6')
grid[1][2] = player;
else
{
cout << "That field is already in use, please try again!" << endl;
Input();
}
}
else if (a == 7)
{
if (grid[2][0] == '7')
grid[2][0] = player;
else
{
cout << "That field is already in use, please try again!" << endl;
Input();
}
}
else if (a == 8)
{
if (grid[2][1] == '8')
grid[2][1] = player;
else
{
cout << "That field is already in use, please try again!" << endl;
Input();
}
}
else if (a == 9)
{
if (grid[2][2] == '9')
grid[2][2] = player;
else
{
cout << "That field is already in use, please try again!" << endl;
Input();
}
}
}
//function to change player
void TogglePlayer()
{
if (player == 'X')
player = 'O';
else
player = 'X';
}
//function to check if a player has won
int Win()
{
//checking if 3 in row for Player 1 (X)
//rows
if (grid[0][0] == 'X' && grid[0][1] == 'X' && grid[0][2] == 'X')
return 'X';
if (grid[1][0] == 'X' && grid[1][1] == 'X' && grid[1][2] == 'X')
return 'X';
if (grid[2][0] == 'X' && grid[2][1] == 'X' && grid[2][2] == 'X')
return 'X';
//columns
if (grid[0][0] == 'X' && grid[1][0] == 'X' && grid[2][0] == 'X')
return 'X';
if (grid[0][1] == 'X' && grid[1][1] == 'X' && grid[2][1] == 'X')
return 'X';
if (grid[0][2] == 'X' && grid[1][2] == 'X' && grid[2][2] == 'X')
return 'X';
//diagonals
if (grid[0][0] == 'X' && grid[1][1] == 'X' && grid[2][2] == 'X')
return 'X';
if (grid[2][0] == 'X' && grid[1][1] == 'X' && grid[0][2] == 'X')
return 'X';
//checking if 3 in row for Player 2 (O)
//rows
if (grid[0][0] == 'O' && grid[0][1] == 'O' && grid[0][2] == 'O')
return 'O';
if (grid[1][0] == 'O' && grid[1][1] == 'O' && grid[1][2] == 'O')
return 'O';
if (grid[2][0] == 'O' && grid[2][1] == 'O' && grid[2][2] == 'O')
return 'O';
//columns
if (grid[0][0] == 'O' && grid[0][1] == 'O' && grid[2][0] == 'O')
return 'O';
if (grid[0][1] == 'O' && grid[1][1] == 'O' && grid[2][1] == 'O')
return 'O';
if (grid[0][2] == 'O' && grid[1][2] == 'O' && grid[2][2] == 'O')
return 'O';
//diagonals
if (grid[0][0] == 'O' && grid[1][1] == 'O' && grid[2][2] == 'O')
return 'O';
if (grid[2][0] == 'O' && grid[1][1] == 'O' && grid[0][2] == 'O')
return 'O';
//return default if there is in-correct input
return '/';
}
int main()
{
system("cls");
Menu();
Names();
string choice2;
b = 0;
Display();
//loop that will run until a player has won or tied
while (1)
{
b++;
Input();
Display();
//if player wins/tie function
//if X (Player 1) wins
if (Win() == 'X')
{
system("cls");
cout << X << " has Won!" << endl;
cout << "Would you like to Play again, y/n." << endl;
cin >> choice2;
if (choice2 == "y" || choice2 == "Y")
{
Display();
}
else if (choice2 == "n" || choice2 == "N")
{
return 0;
}
}
//if O (Player 2) wins
else if (Win() == 'O')
{
system("cls");
cout << O << " has Won!" << endl;
cout << "Would you like to Play again, y/n." << endl;
cin >> choice2;
if (choice2 == "y" || choice2 == "Y")
{
Display();
}
else if (choice2 == "N" || choice2 == "n")
{
return 0;
}
}
//if players tie
else if (Win() == '/' && b == 9)
{
system("cls");
cout << "It's a Tie!" << endl;
cout << "Would you like to Play again, y/n." << endl;
cin >> choice2;
if (choice2 == "y" || choice2 == "Y")
{
Display();
}
else if (choice2 == "N" || choice2 == "n")
{
return 0;
}
}
TogglePlayer();
}
}
|