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
|
#include <iostream>
#include <string>
using namespace std;
//prints board
void display(char grid[5][5]){
for(int i = 0; i < 5; i++){
cout << endl;
for (int j = 0; j < 5; j++){
cout << grid[i][j];
}
}
}
int main() {
//loop to play again
while (true){
//vars
int turn = 1;
int move;
int z = 0;
bool slvd = false;
bool gameOver = false;
char grid[5][5] = {
{'1', '|', '2', '|', '3'},
{'-', '+', '-', '+', '-'},
{'4', '|', '5', '|', '6'},
{'-', '+', '-', '+', '-'},
{'7', '|', '8', '|', '9'}};
display(grid);
while (gameOver == false){
slvd = false;
if (turn == 1){
cout << endl << "X's Turn";
}
else if(turn == 2){
cout << endl << "O's Turn";
}
cout << endl << "Enter the square you want to go in (1-9): ";
cin >> move;
//determines move
while (slvd == false){
if(move == z && z < 10){
if (grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2-2] != 'X' && grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2-2] != 'O'){
if(turn == 1){
slvd = true;
grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2-2] = 'X';
display(grid);
turn = 2;
}
else if(turn == 2){
slvd = true;
grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2-2] = 'O';
display(grid);
turn = 1;
}
}
else{
cout << "Not a valid option" << endl << endl;
break;
}
}
else if(move != z){
z = z + 1;
}
else if(z < 0 or z > 9){
cout << "Not a valid option.";
break;
}
//win detection
//checks rows
if (((move+2)%3+1)*2 == 'X' && ((move+2)%3+1)*2-4 == 'X'){
cout << "X Wins" << endl << endl;
gameOver = true;
break;
}
else if (((move+2)%3+1)*2-4 == 'X' && ((move+2)%3+1)*2-6 == 'X'){
cout << "X Wins" << endl << endl;
gameOver = true;
break;
}
else if (((move+2)%3+1)*2 == 'X' && ((move+2)%3+1)*2+2 == 'X'){
cout << "X Wins" << endl << endl;
gameOver = true;
break;
}
else if (((move+2)%3+1)*2 == 'X' && ((move+2)%3+1)*2-4 == 'X'){
cout << "X Wins" << endl << endl;
gameOver = true;
break;
}
else if (((move+2)%3+1)*2-4 == 'X' && ((move+2)%3+1)*2-6 == 'X'){
cout << "X Wins" << endl << endl;
gameOver = true;
break;
}
else if (((move+2)%3+1)*2 == 'X' && ((move+2)%3+1)*2+2 == 'X'){
cout << "X Wins" << endl << endl;
gameOver = true;
break;
}
else{
continue;
}
//checks columns
//checks diagonals
}
}
break;
}
return 0;
}
|