Created
April 15, 2013 23:19
-
-
Save MastodonHQ/5392101 to your computer and use it in GitHub Desktop.
Tick - A tic tac toe game (Terminal/Command Line version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
using namespace std; | |
const string VERSION = "1.1.2"; | |
/* | |
[0][1][2] | |
[3][4][5] | |
[6][7][8] | |
*/ | |
class tick | |
{ | |
private: | |
int currentPlayer; | |
int board[9]; | |
bool lock[9]; | |
typedef enum {X = 1, O = 2, Blank = 0} x_or; | |
typedef enum {Locked = true, Unlocked = false} l_u; | |
public: | |
void setPlayer(); | |
void setPiece(int spot); | |
void resetBoard(); | |
void resetLocks(); | |
void setLock(int spot); | |
void printBoard(); | |
void checkGrid(int x, int y, int z, x_or b); | |
void checkWin(); //debug only | |
void run(); | |
tick () | |
{ | |
tick::currentPlayer = 1; | |
tick::resetBoard(); | |
tick::resetLocks(); | |
} | |
}; | |
void tick::setPlayer() | |
{ | |
if(tick::currentPlayer == 1) | |
{ | |
tick::currentPlayer = 2; | |
} | |
else | |
{ | |
//tick::currentPlayer is 2 so we set to 1 | |
tick::currentPlayer = 1; | |
} | |
} | |
void tick::resetBoard() | |
{ | |
int i = 0; | |
for(i=0; i!=9; i++) | |
{ | |
tick::board[i] = Blank; // zero is used to check if the board->spot is empty | |
} | |
} | |
void tick::resetLocks() | |
{ | |
int i = 0; | |
for(i=0; i!=9; i++) | |
{ | |
tick::lock[i] = Unlocked; | |
} | |
} | |
void tick::setLock(int spot) | |
{ | |
tick::lock[spot] = Locked; | |
} | |
void tick::setPiece(int spot) | |
{ | |
if(tick::lock[spot] == Locked) | |
{ | |
cout << "This spot has been played already! Choose a Different Spot" << endl; | |
} | |
else | |
{ | |
if(tick::currentPlayer == 1) | |
{ | |
tick::board[spot] = X; | |
tick::setLock(spot); | |
tick::setPlayer(); | |
} | |
else | |
{ | |
tick::board[spot] = O; | |
tick::setLock(spot); | |
tick::setPlayer(); | |
} | |
} | |
} | |
void tick::printBoard() | |
{ | |
int i = 0; | |
for(i = 0; i!=3; i++) | |
{ | |
if(tick::board[i] == X) | |
{ | |
cout << "[X]"; | |
} | |
else if (tick::board[i] == O) | |
{ | |
cout << "[O]"; | |
} | |
else if (tick::board[i] == Blank) | |
{ | |
cout << "[ ]"; | |
} | |
} | |
cout << endl; | |
for(i = 3; i != 6; i++) | |
{ | |
if(tick::board[i] == X) | |
{ | |
cout << "[X]"; | |
} | |
else if (tick::board[i] == O) | |
{ | |
cout << "[O]"; | |
} | |
else if (tick::board[i] == Blank) | |
{ | |
cout << "[ ]"; | |
} | |
} | |
cout << endl; | |
for(i = 6; i != 9; i++) | |
{ | |
if(tick::board[i] == X) | |
{ | |
cout << "[X]"; | |
} | |
else if (tick::board[i] == O) | |
{ | |
cout << "[O]"; | |
} | |
else if (tick::board[i] == Blank) | |
{ | |
cout << "[ ]"; | |
} | |
} | |
cout <<endl; | |
} | |
void tick::checkGrid(int x, int y, int z, x_or b) | |
{ | |
if(tick::board[x] == b && tick::board[y] == b && tick::board[z] == b) | |
{ | |
if(b == X) | |
{ | |
cout <<"Player One Won the game"<<endl; | |
} | |
else if(b == O) | |
{ | |
cout <<"Player Two Won the game"<<endl; | |
} | |
cout <<"Starting a New Game"<< endl; | |
tick::resetBoard(); | |
tick::resetLocks(); | |
tick::currentPlayer = 1; | |
} | |
else | |
{ | |
//do nothing because the game hasn't been won by either player | |
} | |
} | |
void tick::checkWin(){ | |
/* | |
horizontal indice: (0,1,2) , (3,4,5) , (6,7,8) | |
Vertical indice: (0,3,6) , (1,4,7) , (2,5,8) | |
diagonal indice: (0,4,8) , (2,4,6) | |
*/ | |
tick::checkGrid(0,1,2,X); | |
tick::checkGrid(0,1,2,O); | |
tick::checkGrid(3,4,5,X); | |
tick::checkGrid(3,4,5,O); | |
tick::checkGrid(6,7,8,X); | |
tick::checkGrid(6,7,8,O); | |
tick::checkGrid(0,3,6,X); | |
tick::checkGrid(0,3,6,O); | |
tick::checkGrid(1,4,7,X); | |
tick::checkGrid(1,4,7,O); | |
tick::checkGrid(2,5,8,X); | |
tick::checkGrid(2,5,8,O); | |
tick::checkGrid(0,4,8,X); | |
tick::checkGrid(0,4,8,O); | |
tick::checkGrid(2,4,6,X); | |
tick::checkGrid(2,4,6,O); | |
} | |
void tick::run() | |
{ | |
cout << "tick: a tick Tac Toe game\nVersion: " << VERSION << "\nAuthor:Ziekan\n" << endl; | |
bool running = true; | |
string input; | |
while(running) | |
{ | |
tick::printBoard(); | |
tick::checkWin(); //debug only | |
cout << "Player[" << tick::currentPlayer << "]: "; | |
cin >> input; | |
if(input == "help") | |
{ | |
cout <<"Tick: A tic tac toe game\nVersion: " << VERSION << "\nEnter 1 - 9, type quit, or type help\n" << endl; | |
} | |
else if (input == "1") | |
{ | |
//input was {ONE} | |
tick::setPiece(0); | |
} | |
else if (input == "2") | |
{ | |
//input was {TWO} | |
tick::setPiece(1); | |
} | |
else if (input == "3") | |
{ | |
//input was {THREE} | |
tick::setPiece(2); | |
} | |
else if (input == "4") | |
{ | |
//input was {FOUR} | |
tick::setPiece(3); | |
} | |
else if (input == "5") | |
{ | |
tick::setPiece(4); | |
} | |
else if (input == "6") | |
{ | |
tick::setPiece(5); | |
} | |
else if (input == "7") | |
{ | |
tick::setPiece(6); | |
} | |
else if (input == "8") | |
{ | |
tick::setPiece(7); | |
} | |
else if (input == "9") | |
{ | |
tick::setPiece(8); | |
} | |
else if (input == "q" || input == "quit" || input == "Quit") | |
{ | |
running = false; | |
} | |
else | |
{ | |
cout << "INVALID INPUT: Please Enter 1 - 9 or Enter Help" << endl; | |
} | |
} | |
cout << "Thanks for playing!!!" << endl; | |
} | |
int main(int argc, char ** argv) | |
{ | |
tick mygame; | |
mygame.run(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment