Last active
August 29, 2015 14:10
-
-
Save soachishti/1a2f77256db6c0ba69a2 to your computer and use it in GitHub Desktop.
Tic Tac Toe
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
/* | |
* Name: Syed Owais Ali Chishti | |
* Tic Tac Toe | |
* Major: BS(CS) | |
*/ | |
#include<iostream> | |
#include<cstdlib> | |
#include<conio.h> | |
#include<iomanip> | |
using namespace std; | |
char box[9]; | |
bool won; | |
/* | |
* Print game to STDOUT | |
*/ | |
void printBox() { | |
system("cls"); | |
cout<<"TIC TAC TOE"<<endl<<endl; | |
for(int row = 0; row < 3;row++){ | |
for(int col = 0; col < 3;col++){ | |
cout<<" "<<box[row*3+col]; | |
if(col < 2) | |
cout<<" | "; | |
} | |
if(row < 2) | |
cout<<endl<<"---+----+---"<<endl; | |
} | |
} | |
/* | |
* Checker winner of game. | |
*/ | |
int winnerCheck(){ | |
int possibility[8][3] = { | |
{0,3,6}, | |
{1,4,7}, | |
{2,5,8}, | |
{0,1,2}, | |
{3,4,5}, | |
{6,7,8}, | |
{6,4,2}, | |
{0,4,8} | |
}; | |
for(int i=0;i<8;++i) | |
{ | |
if(box[possibility[i][0]] == box[possibility[i][1]] && box[possibility[i][1]] == box[possibility[i][2]]) | |
{ | |
printBox(); | |
cout<<endl<<endl<<box[possibility[i][0]]<<" WON!!!"; | |
won = true; | |
} | |
} | |
return false; | |
} | |
/* | |
* Gets input from user | |
*/ | |
int getChoice(){ | |
int choice; | |
while(true){ | |
cin>>choice; | |
if((choice < 10 && choice > 0) && (box[choice-1] != 'X' && box[choice-1] != 'O')){ | |
return choice-1; | |
} | |
else{ | |
cout<<"Sorry!!!, Try again: "; | |
} | |
} | |
} | |
/* | |
* Add numbers to array | |
*/ | |
void newArray(){ | |
for (int i = 0; i < 9 ; i++) { | |
box[i] = (i+1)+48; // adding 48 for converting to asci character | |
} | |
} | |
/* | |
* Game Finish | |
*/ | |
bool finishCheck() { | |
for (int i = 0; i < 9 ; i++) { | |
if(box[i] != 'O' && box[i] != 'X') | |
return false; | |
} | |
return true; | |
} | |
int main() { | |
char choice; | |
cout<<endl<<endl<<endl<<endl<<endl<<endl; | |
cout<<setw(45)<<"*******************"<<endl<<endl; | |
cout<<setw(45)<<"* TIC TAC TOE *"<<endl<<endl; | |
cout<<setw(45)<<"*******************"<<endl<<endl; | |
cout<<setw(46)<<"--- SOAC PROJECT ---"<<endl; | |
cout<<setw(49)<<"--- Roll no: P14-6011 ---"<<endl; | |
cout<<endl<<endl<<endl<<endl<<endl<<endl; | |
cout<<setw(80)<<"Press any key to continue."; | |
_getch(); | |
system("cls"); | |
do | |
{ | |
newArray(); | |
won = false; | |
while(true) { | |
//Player One | |
if(won == false && finishCheck() == false){ | |
printBox(); | |
cout<<endl<<endl<<"Player 1: "; | |
choice = getChoice(); | |
box[choice] = 'X'; | |
// Winner Check | |
winnerCheck(); | |
} | |
else { | |
break; | |
} | |
//Player Two | |
if(won == false && finishCheck() == false){ | |
printBox(); | |
cout<<endl<<endl<<"Player 2: "; | |
choice = getChoice(); | |
box[choice] = 'O'; | |
// Winner Check | |
winnerCheck(); | |
} | |
else { | |
break; | |
} | |
} | |
if(won == false) { | |
printBox(); | |
cout<<endl<<endl<<"DRAW!!!"; | |
} | |
cout<<endl<<endl<<"Do you want to play again (Y/N): "; | |
cin>>choice; | |
} | |
while(choice == 'Y' || choice == 'y'); | |
cout<<endl; | |
system("PAUSE"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment