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
|
// Date: April, 2013
#include <fstream>
#include <iostream>
#include <string>
#include "main.h"
#include <iomanip>
using namespace std;
// Const, Enums, User Defined Data Types
enum GameState{
GAME_WIN,
GAME_LOSE,
GAME_TIE,
GAME_MAX
};
//Function Prototypes
void DisplayIntro(void);
void InitializeGame(bool& gameOver, char& playerSymbol, char gameSquares[]);
void DisplayGameBoard(char gameSquares[]);
char GetValidUserInput(char playerSymbol, char gameSquares[]);
void SetGameBoardPosition(char posLocation, char playerSymbol, char gameSquares[]);
bool CheckForGameOver(char& playerSymbol, char gameSquares[], GameState& gameOver );
void UpdatePlayer(char& playerSymbol, char symbol1, char symbol2 );
char RequestAdditionalGame(void);
void DisplayCredits(void);
// Main Function
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; // current player symbol
char gameSquares[9]; // board display variables
GameState gameOverState = GAME_MAX; //game state ( win, lose or tie )
DisplayIntro(); // Display introduction screen
while( playAgain == 'A' )
{
InitializeGame(gameOver, playerSymbol, gameSquares); // Initializes the game board
DisplayGameBoard(gameSquares); // Displays the game board
while( !gameOver ) // Loop until a winner is determined or all squars have been used
{
posLocation = GetValidUserInput(playerSymbol, gameSquares); // Get vaild user inpur
SetGameBoardPosition(posLocation, playerSymbol, gameSquares); // Set the appropriate board position
DisplayGameBoard(gameSquares); // Display the game board
gameOver = CheckForGameOver(playerSymbol, gameSquares, gameOverState); // 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;
}
void UpdatePlayer(char& playerSymbol, char symbol1, char symbol2 )
{
if(playerSymbol == symbol1) // alternating between the X and O player
playerSymbol = symbol2;
else
playerSymbol = symbol1;
}
bool CheckForGameOver(char& playerSymbol, char gameSquares[], GameState& gameOver )
{
char buffer[2];
bool freeMove = false;
if( (gameSquares[0] == gameSquares[1] && gameSquares[1] == gameSquares[2]) || (gameSquares[3] == gameSquares[4] && gameSquares[4] == gameSquares[5]) || (gameSquares[6] == gameSquares[7] && gameSquares[7] == gameSquares[8]) || // checking game victory conditions
(gameSquares[0] == gameSquares[3] && gameSquares[3] == gameSquares[6]) || (gameSquares[1] == gameSquares[4] && gameSquares[4] == gameSquares[7]) || (gameSquares[2] == gameSquares[5] && gameSquares[5] == gameSquares[8]) ||
(gameSquares[0] == gameSquares[4] && gameSquares[4] == gameSquares[8]) || (gameSquares[2] == gameSquares[4] && gameSquares[4] == gameSquares[6]) )
{
gameOver = GAME_WIN;
cout << endl << playerSymbol<< " WINS!!" << endl << endl << endl;
return true;
}
else
{
int i = 0;
while( i < 9 && !freeMove)
{
itoa(i+1,buffer,10);
if( gameSquares[i] == buffer[0] )
freeMove = true;
i++;
}
if( freeMove )
{
UpdatePlayer(playerSymbol, 'O', 'X' );
return false;
}
else
{
cout << endl << "It's a tie!!" << endl << endl << endl;
gameOver = GAME_TIE;
return true;
}
}
}
void SetGameBoardPosition(char posLocation, char playerSymbol, char gameSquares[])
{
char buffer[2];
buffer[0] = posLocation;
int location = atoi(buffer);
gameSquares[location-1] = playerSymbol;
}
char GetValidUserInput( char playerSymbol, char gameSquares[])
{
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' && gameSquares[0] == '1') || (entry == '2' && gameSquares[1] == '2') || (entry == '3' && gameSquares[2] == '3') ||
(entry == '4' && gameSquares[3] == '4') || (entry == '5' && gameSquares[4] == '5') || (entry == '6' && gameSquares[5] == '6') ||
(entry == '7' && gameSquares[6] == '7') || (entry == '8' && gameSquares[7] == '8') || (entry == '9' && gameSquares[8] == '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;
}
char RequestAdditionalGame(void)
{
char userEntry;
cout << "Enter \"A\" to play again, any other key to go back to Main Menu>>: " << endl << endl; // getting user input for whether the player wants to play again
cin >> userEntry;
userEntry = toupper(userEntry);
cout << endl << endl;
return(userEntry);
}
void InitializeGame(bool& gameOver, char& playerSymbol, char gameSquares[])
{
gameOver = false; // initializing game variables
playerSymbol = 'O';
char buffer[2]; // this is where the integer is converted to
for(int i = 0; i < 9; i++)
{
itoa(i+1,buffer,10);
gameSquares[i] = buffer[0];
}
}
void DisplayIntro(void)
{
system("cls");
cout << " ---------------------------------------------------------------------------- \n";
cout << " WELCOME TO TIC TAC TOE " << endl;
cout << " ---------------------------------------------------------------------------- \n";
cout << endl << endl;
system("pause");
system("cls");
cout << endl << endl;
}
void DisplayGameBoard(char gameSquares[])
{
cout << " " << gameSquares[0] << " | " << gameSquares[1] << " | " << gameSquares[2] << endl; // displaying the initial game board
cout << "----------" << endl;
cout << " " << gameSquares[3] << " | " << gameSquares[4] << " | " << gameSquares[5] << endl;
cout << "----------" << endl;
cout << " " << gameSquares[6] << " | " << gameSquares[7] << " | " << gameSquares[8] << endl;
cout << endl;
}
void DisplayCredits(void)
{
cout << endl << endl;
cout << " ------------------------------- GAME CREDITS ------------------------------- \n";
cout << " Designer: Me"<< endl;
cout << " Programmer: Me" << endl;
cout << " ---------------------------------------------------------------------------- \n";
cout << endl << endl;
system("pause");
}
|