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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
const short DIMENSION = 3;
//initialize tic-tac-toe board
void initialize_board(char position[][DIMENSION]);
//convert digit (1-9) of type short to a digit (1-9) of the type char
//returns the char type of the digit
char convert_digit(short digit);
//clears screen
void clr();
//displays board
void disp(char position[][DIMENSION]);
//display upper border
void upper_lower();
//display section of boards with squares
void square_section(char position[][DIMENSION], short row);
//displays midsection border
void midsection();
//places an "x" or "o" on the board
void move(char position[][DIMENSION], bool vacant[][DIMENSION], short& digit, bool x_turn);
//initializes "vacant" array to have all values be true
void initialize_vacant(bool position[][DIMENSION]);
//takes a digit symbolizing a position (1-9) and returns a 2d coordinate
void coordinate_conversion(short digit, short& row, short& column);
//returns true if a player won by getting a horizontal combination
//returns false otherwise
bool horizontal_test(char position[][DIMENSION]);
//returns true if a player won by getting a vertical combination
//returns false otherwise
bool vertical_test(char position[][DIMENSION]);
//return true if a player won by getting a diagnol combination
//returns false otherwise
bool diagnol_test(char position[][DIMENSION]);
//returns true if someone won
//returns false otherwise
bool test_win(char position[][DIMENSION]);
//returns true if there are no more vacant spaces left
//returns false otherwise
bool test_vacancy(bool vacant[][DIMENSION]);
int main()
{
using namespace std;
char control = 'y';//this variable is used by user to stay in or exit out of do-while loop
do
{
short digit;
char player;
bool win = false;
bool x_turn = false;
char position[DIMENSION][DIMENSION];
bool vacant[DIMENSION][DIMENSION];
initialize_board(position);
initialize_vacant(vacant);
bool vacant_spaces = true;
while(!win && vacant_spaces)
{
if(x_turn)
{
cout << "Player: x\n";
player = 'x';
x_turn = !x_turn;
}
else
{
cout << "Player: o\n";
player = 'o';
x_turn = !x_turn;
}
clr();
disp(position);
cout << "Enter location on board: ";
cin >> digit;
move(position, vacant, digit, x_turn);
win = test_win(position);
vacant_spaces = test_vacancy(vacant);
if(win)
{
cout << "Player " << player << " wins!\n";
}
else if(!vacant_spaces)
{
cout << "DRAW\n";
}
}
cout << "\n\nWould you like to run the program again? (Y/N): ";
cin >> control;
while ((control != 'y') && (control != 'n'))
{
cout << "Incorrect input. Would you like to run the program again? (Y/N): ";
cin >> control;
control = tolower(control);
}
}while(control == 'y');
cout << "Press key to exit: ";
char exit;
cin >> exit;
return 0;
}
void initialize_board(char position[][DIMENSION])
{
using namespace std;
short count = 1;
for(short i = 0; i < DIMENSION; i++)
{
for(short j = 0; j < DIMENSION; j++)
{
position[i][j] = convert_digit(count);
count++;
}
}
return;
}
char convert_digit(short digit)
{
using namespace std;
if(digit == 1)
{
return '1';
}
else if(digit == 2)
{
return '2';
}
else if(digit == 3)
{
return '3';
}
else if(digit == 4)
{
return '4';
}
else if(digit == 5)
{
return '5';
}
else if(digit == 6)
{
return '6';
}
else if(digit == 7)
{
return '7';
}
else if(digit == 8)
{
return '8';
}
else if(digit == 9)
{
return '9';
}
}
void clr()
{
using namespace std;
cout << string(50, '\n');
return;
}
void disp(char position[][DIMENSION])
{
using namespace std;
upper_lower();
square_section(position, 0);
midsection();
square_section(position, 1);
midsection();
square_section(position, 2);
upper_lower();
return;
}
void upper_lower()
{
using namespace std;
cout << "-------\n";
return;
}
void square_section(char position[][DIMENSION], short row)
{
using namespace std;
cout << "|" << position[row][0] << "|" << position[row][1] << "|" << position[row][2] << "|\n";
return;
}
void midsection()
{
using namespace std;
cout << "+-+-+-+\n";
return;
}
void move(char position[][DIMENSION], bool vacant[][DIMENSION], short& digit, bool x_turn)
{
using namespace std;
char player;
short row, column;
if(x_turn)
{
player = 'x';
}
else
{
player = 'o';
}
coordinate_conversion( digit, row, column);
while(!vacant[row][column])
{
clr();
cout << endl << "Invalid position.\n";
disp(position);
cout << "Player " << player << " -- Enter another choice: ";
cin >> digit;
coordinate_conversion( digit, row, column);
}
position[row][column] = player;
vacant[row][column] = false;
return;
}
void initialize_vacant(bool position[][DIMENSION])
{
using namespace std;
for(short i = 0; i < DIMENSION; i++)
{
for(short j = 0; j < DIMENSION; j++)
{
position[i][j] = true;
}
}
return;
}
void coordinate_conversion(short digit, short& row, short& column)
{
using namespace std;
if(digit == 1)
{
row = 0;
column = 0;
}
else if(digit == 2)
{
row = 0;
column = 1;
}
else if(digit == 3)
{
row = 0;
column = 2;
}
else if(digit == 4)
{
row = 1;
column = 0;
}
else if(digit == 5)
{
row = 1;
column = 1;
}
else if(digit == 6)
{
row = 1;
column = 2;
}
else if(digit == 7)
{
row = 2;
column = 0;
}
else if(digit == 8)
{
row = 2;
column = 1;
}
else//(digit == 9)
{
row = 2;
column = 2;
}
return;
}
bool horizontal_test(char position[][DIMENSION])
{
using namespace std;
for(short i = 0; i < DIMENSION; i++)
{
short match = 0;
for(short j = 1; j < DIMENSION; j++)
{
if(position[i][0] == position [i][j])
{
match++;
}
}
if(match >= 2)
{
return true;
}
}
return false;
}
bool vertical_test(char position[][DIMENSION])
{
using namespace std;
for(short j = 0; j < DIMENSION; j++)
{
short match = 0;
for(short i = 1; i < DIMENSION; i++)
{
if(position[0][j] == position [i][j])
{
match++;
}
}
if(match >= 2)
{
return true;
}
}
return false;
}
bool diagnol_test(char position[][DIMENSION])
{
using namespace std;
short match(0);
for(short i = 1; i < DIMENSION; i++)
{
if(position[i][i] == position[0][0])
{
match++;
}
}
if(match == 2)
{
return true;
}
match = 0;
for(short k = 1; k < DIMENSION; k++)
{
if(position[0][2] == position[k][DIMENSION-1-k])
{
match++;
}
}
if(match == 2)
{
return true;
}
else
{
return false;
}
}
bool test_win(char position[][DIMENSION])
{
using namespace std;
if((diagnol_test(position)) || (vertical_test(position)) || (horizontal_test(position)))
{
return true;
}
else
{
return false;
}
}
bool test_vacancy(bool vacant[][DIMENSION])
{
using namespace std;
for(short i = 0; i < DIMENSION; i++)
{
for(short j = 0; j < DIMENSION; j++)
{
if(vacant[i][j] == true)
{
return true;
}
}
}
return false;
}
|