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
|
#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 choice;
char quit;
int turn = 1;
int move;
int z = 0;
bool slvd = false;
bool gameOver = false;
//menu
cout << "**************************" << endl << "* *" << endl << "* 1. Player VS Player *" << endl << "* 2. Player VS Computer *" << endl << "* 3. Instructions *" << endl << "* *" << endl << "**************************" << endl << endl;
//menu choice
cin >> choice;
switch (choice){
//player vs player
case 1:{
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);
}
else if(turn == 2){
slvd = true;
grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2-2] = 'O';
display(grid);
}
}
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
//check X
if (turn == 1){
if (grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2] == 'X' && grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2-4] == 'X'){
cout << "X Wins" << endl << endl;
gameOver = true;
break;
}
else if (grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2-6] == 'X' && grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2-4] == 'X'){
cout << "X Wins" << endl << endl;
gameOver = true;
break;
}
else if (grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2] == 'X' && grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2+2] == 'X'){
cout << "X Wins" << endl << endl;
gameOver = true;
break;
}
else{
turn = 2;
continue;
}
}
//check O
else if (turn == 2){
if (grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2] == 'O' && grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2-4] == 'O'){
cout << "O Wins" << endl << endl;
gameOver = true;
break;
}
else if (grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2-6] == 'O' && grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2-4] == 'O'){
cout << "O Wins" << endl << endl;
gameOver = true;
break;
}
else if (grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2] == 'O' && grid[(int(move/3.01)+1)*2-2][((move+2)%3+1)*2+2] == 'O'){
cout << "O Wins" << endl << endl;
gameOver = true;
break;
}
else{
turn = 1;
continue;
}
}
//checks columns
//checks diagonals
}
break;
}
//player vs computer
case 2:{
cout << "" << endl;
break;
}
//instructions
case 3:{
cout << "Instructions" << endl << "The game is played on a grid that's 3 squares by 3 squares. Player 1 is X, and player 2 or the computer is O. Players take turns putting their marks in empty squares. The first player to get 3 of their marks in a row (up, down, across, or diagonally) is the winner." << endl << endl << "Press Enter to Continue." << endl;
system("read");
continue;
}
//invalid choice
default:{
cout << "Not a valid option" << endl << endl;
break;
}
}
cout << "To go back to main menu press 'm' followed by enter" << endl << "To quit press 'q' followed by enter" << endl << endl;
cin >> quit;
if(quit == 'm'){
continue;
}
else if(quit == 'q'){
break;
}
}
return 0;
}
|