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
|
#include <iostream>
using namespace std;
int main()
{
//Menu
//Want to play Tic Tac Toe
//Player Chooses Yes or No
//Game Starts and Displays Board
//Menu Choice
int MenuChoice;
//Board Variables
char Square1('1');
char Square2('2');
char Square3('3');
char Square4('4');
char Square5('5');
char Square6('6');
char Square7('7');
char Square8('8');
char Square9('9');
//Player Variable
int PlayerTurn(1);
//Whether the game is over or not in the while loop
bool GameOverDecider(true);
//Main Menu
cout << "\tWould you like to play some TIC-TAC-TOE today?" << endl;
cout << endl << endl;
cout << "\t(1)Game Start(2 players required)" << endl;
cout << "\t(2)Quit Game" << endl;
cout << endl;
cout << "Choice( 1 or 2):";
cin >> MenuChoice;
if (MenuChoice == 1){
do {
//Player Variable
int PlayerTurn(1);
//Whether the game is over or not in the while loop
bool GameOverDecider(true);
//Main Game Board
cout << " " << Square1 << " | " << Square2 << " | " << Square3 << endl;
cout << " -----+-----+-----" << endl;
cout << " " << Square4 << " | " << Square5 << " | " << Square6 << endl;
cout << " -----+-----+-----" << endl;
cout << " " << Square7 << " | " << Square8 << " | " << Square9 << endl;
cout << " -----+-----+-----" << endl;
//Set Player Marker, Player 1 uses X and Player 2 uses O
char PlayerMarker;
if (PlayerTurn = 1){
PlayerMarker = 'X';
}else{
PlayerMarker = 'O';
}
//Start the game, asking for each Player for a move
//Bool Variable in order to check if the move is valid
bool ValidTurn;
//Run a do while loop to check if their hasn't been a square filled in or
do{
//Char Variable to get next move
char CurrentMove;
cout << "Player" << PlayerTurn << "'s turn, set move on what square: " << endl;
cin >> CurrentMove;
ValidTurn = true;
//If Statment Checking if the move is invalid
//If the move equals 1-9 and a number, and if no one has marked the space yet
//Checks Each Square
if (CurrentMove == '1' && Square1 == '1') {
Square1 = PlayerMarker;
} else if (CurrentMove == '2' && Square1 == '2') {
Square2 = PlayerMarker;
} else if (CurrentMove == '3' && Square1 == '3') {
Square3 = PlayerMarker;
} else if (CurrentMove == '4' && Square1 == '4') {
Square4 = PlayerMarker;
} else if (CurrentMove == '5' && Square1 == '5') {
Square5 = PlayerMarker;
} else if (CurrentMove == '6' && Square1 == '6') {
Square6 = PlayerMarker;
} else if (CurrentMove == '7' && Square1 == '7') {
Square7 = PlayerMarker;
} else if (CurrentMove == '8' && Square1 == '8') {
Square8 = PlayerMarker;
} else if (CurrentMove == '9' && Square1 == '9') {
Square9 = PlayerMarker;
} else {
cout << "Invalid Move, make another one:" << endl;
ValidTurn = false;
}
} while(!ValidTurn);
// GameOverDecider Bool Variable now set to false, showing the game is over
GameOverDecider = false;
// New Bool variable
bool WinGame = true;
//Win Condition that's through either square 1, 4, and 7 or 1, 2, and 3
if(Square1 != '1'){
//1, 2, and 3
if (Square2 == Square1 && Square3 == Square1) {
GameOverDecider = true;
}
//1, 4, and 7
if (Square4 == Square1 && Square7 == Square1) {
GameOverDecider = true;
}
}
//Win Condition that's through either square 3, 6, and 9 or 7, 8, and 9
if(Square1 != '9'){
//3, 6, and 9
if (Square3 == Square9 && Square6 == Square9) {
GameOverDecider = true;
}
//7, 8, and 9
if (Square7 == Square9 && Square8 == Square9) {
GameOverDecider = true;
}
}
//Win Conditions through the middle square, 4 conditions
if(Square1 != '5'){
//1, 5, and 9
if (Square1 == Square5 && Square9 == Square5) {
GameOverDecider = true;
}
//2, 5, and 8
if (Square2 == Square5 && Square8 == Square5) {
GameOverDecider = true;
}
//4, 5, and 6
if (Square4 == Square5 && Square6 == Square5) {
GameOverDecider = true;
}
//3, 5, and 7
if (Square3 == Square5 && Square7 == Square5) {
GameOverDecider = true;
}
}
//Check if their are draws
if (Square1 != '1' && Square2 != '2' && Square3 != '3' &&
Square4 != '4' && Square5 != '5' && Square6 != '6' &&
Square7 != '7' && Square8 != '8' && Square9 != '9' && !GameOverDecider){
GameOverDecider = true;
WinGame = false;
if(GameOverDecider){
if(WinGame){
cout << "Player " << PlayerTurn << " totally wins!" << endl;
}
//Print the result board
cout << " " << Square1 << " | " << Square2 << " | " << Square3 << endl;
cout << " -----+-----+-----" << endl;
cout << " " << Square4 << " | " << Square5 << " | " << Square6 << endl;
cout << " -----+-----+-----" << endl;
cout << " " << Square7 << " | " << Square8 << " | " << Square9 << endl;
cout << " -----+-----+-----" << endl;
//Choice to whether to play the game again
cout << "\tGame Over!" << endl;
cout << "\tWant to play again?(Y/N)?: ";
char PlayAgain;
cin >> PlayAgain;
//Clears Board Back to the original numbers
if(PlayAgain = 'y'){
GameOverDecider = false;
Square1 = '1';
Square2 = '2';
Square3 = '3';
Square4 = '4';
Square5 = '5';
Square6 = '6';
Square7 = '7';
Square8 = '8';
Square9 = '9';
}
PlayerTurn = 1;
} else {
// If the game is still going on, alternate the players turns
if (PlayerTurn == 1){
PlayerTurn = 2;
} else {
PlayerTurn = 1;
}
}
// End of Main Do While Loop
} while (!GameOverDecider);
//End of Menu Choice If Statement
} else(MenuChoice == 2) {
cout << "Ok....." << endl;
}
system ("pause");
return 0;
}
|