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
|
#include <iostream>
#include <string>
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(void)
{
bool gameOver = false; // game loop control flag
char posLocation; // user input variable for position choice
char playAgain = 'A'; // program exit control flag
char playerSymbol = 'X'; // current player symbol
string pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9;
string pos[9]; // board display variables
pos[0] == pos1;
pos[1] == pos2;
pos[2] == pos3;
pos[3] == pos4;
pos[4] == pos5;
pos[5] == pos6;
pos[6] == pos7;
pos[7] == pos8;
pos[8] == pos9;
DisplayIntro(); // Display introduction screen
while( playAgain == 'A' )
{
InitializeGame(gameOver, playerSymbol, pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); // Initializes the game board
DisplayGameBoard(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); // Displays the game board
while( !gameOver ) // Loop until a winner is determined or all squars have been used
{
posLocation = GetValidUserInput(playerSymbol, pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); // Get vaild user inpur
SetGameBoardPosition(posLocation, playerSymbol, pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); // Set the appropriate board position
DisplayGameBoard(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); // Display the game board
gameOver = CheckForGameOver(playerSymbol, pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); // Check for end of game condition and change the player symbol
}
playAgain = RequestAdditionalGame(); // Determine whether to play the game again
}
DisplayCredits(); // Display the credit screen
return 0;
}
char RequestAdditionalGame(void)
{
char userEntry;
cout << "Enter \"A\" to play again, any other key to exit: " << endl << endl; // getting user input for whether the player wants to play again
cin >> userEntry;
userEntry = toupper(userEntry);
cout << endl << endl;
return(userEntry);
}
void UpdatePlayer(char& playerSymbol)
{
if(playerSymbol == 'O') // alternating between the X and O player
playerSymbol = 'X';
else
playerSymbol = 'O';
}
bool CheckForGameOver(char& playerSymbol, string pos1, string pos2, string pos3, string pos4, string pos5, string pos6, string pos7, string pos8, string pos9)
{
if( (pos1 == pos2 && pos1 == pos3) || (pos4 == pos5 && pos4 == pos6) || (pos7 == pos8 && pos7 == pos9) || // checking game victory conditions
(pos1 == pos4 && pos1 == pos7) || (pos2 == pos5 && pos2 == pos8) || (pos3 == pos6 && pos3 == pos9) ||
(pos1 == pos5 && pos1 == pos9) || (pos3 == pos5 && pos3 == pos7) )
{
cout << endl << "Player " << playerSymbol << " wins!! Whoohooo!!" << endl << endl << endl;
return true;
}
else if( pos1 != "1" && pos2 != "2" && pos3 != "3" && pos4 != "4" && pos5 != "5" && pos6 != "6" && pos7 != "7" && pos8 != "8" && pos9 != "9" )
{
cout << endl << "It's a draw!! Get'em next time." << endl << endl << endl;
return true;
}
else
{
UpdatePlayer(playerSymbol);
return false;
}
}
void SetGameBoardPosition(int posLocation, char playerSymbol, string& pos1, string& pos2, string& pos3, string& pos4, string& pos5, string& pos6, string& pos7, string& pos8, string& pos9)
{
if( posLocation == '1' ) // setting game variable based on the user input value
pos1 = playerSymbol;
else if( posLocation == '2')
pos2 = playerSymbol;
else if( posLocation == '3')
pos3 = playerSymbol;
else if( posLocation == '4')
pos4 = playerSymbol;
else if( posLocation == '5')
pos5 = playerSymbol;
else if( posLocation == '6')
pos6 = playerSymbol;
else if( posLocation == '7')
pos7 = playerSymbol;
else if( posLocation == '8')
pos8 = playerSymbol;
else if( posLocation == '9')
pos9 = playerSymbol;
}
char GetValidUserInput(char playerSymbol, string pos1, string pos2, string pos3, string pos4, string pos5, string pos6, string pos7, string pos8, string pos9)
{
bool validInput = false;
char entry;
while( !validInput)
{
cout << "Enter the location number where you want to place an \"" << playerSymbol << "\": ";
cin >> entry; // getting user input for the board position
if( entry >= '1' && entry <= '9')
{
if( (entry == '1' && pos1 == "1") || (entry == '2' && pos2 == "2") || (entry == '3' && pos3 == "3") ||
(entry == '4' && pos4 == "4") || (entry == '5' && pos5 == "5") || (entry == '6' && pos6 == "6") ||
(entry == '7' && pos7 == "7") || (entry == '8' && pos8 == "8") || (entry == '9' && pos9 == "9") )
{
validInput = true;
}
else
{
cout << "You have entered a position that has already been used. ";
validInput = false;
}
}
else
{
cout << "You have entered an incorrect value. Please enter a number from 1 to 9. ";
}
cout << endl << endl;
}
return entry;
}
void InitializeGame(bool& gameOver, char& playerSymbol, string& pos1, string& pos2, string& pos3, string& pos4, string& pos5, string& pos6, string& pos7, string& pos8, string& pos9)
{
gameOver = false; // initializing game variables
playerSymbol = 'O';
pos1 = "1";
pos2 = "2";
pos3 = "3";
pos4 = "4";
pos5 = "5";
pos6 = "6";
pos7 = "7";
pos8 = "8";
pos9 = "9";
}
void DisplayIntro(void)
{
cout << "**************************************************" << endl;
cout << " Welcome to *" << endl;
cout << " Awesome Tic Tac Toe *" << endl;
cout << " Well, kind of... *" << endl;
cout << "**************************************************" << endl;
cout << endl << endl;
system("pause");
cout << endl << endl;
}
void DisplayGameBoard(string pos1, string pos2, string pos3, string pos4, string pos5, string pos6, string pos7, string pos8, string pos9)
{
cout << " " << pos1 << " | " << pos2 << " | " << pos3 << endl; // displaying the initial game board
cout << "----------" << endl;
cout << " " << pos4 << " | " << pos5 << " | " << pos6 << endl;
cout << "----------" << endl;
cout << " " << pos7 << " | " << pos8 << " | " << pos9 << endl;
cout << endl;
}
void DisplayCredits(void)
{
cout << endl << endl;
cout << "************* CREDITS *************" << endl; // displaying the game credits
cout << " Designer: Me *" << endl;
cout << " Programmer: Me *" << endl;
cout << " Art Production: Me *" << endl;
cout << " Everything Else: All Me *" << endl;
cout << "*************************************" << endl;
cout << endl << endl;
system("pause");
}
|