Created
March 27, 2020 08:26
-
-
Save sheikhaafaq/bbec0b7f6f9f362b932d8c7840e4db6f to your computer and use it in GitHub Desktop.
tick cross game
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<stdlib.h> | |
using namespace std; | |
char table[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}}; | |
int turn='X'; | |
int column,row; | |
bool draw = false; | |
void display_table() | |
{ | |
system("cls"); | |
cout <<"\t A A F A Q _ R A S H I D"; | |
cout << "\n\t\t T I C T A C T O E "<<endl; | |
cout << "\n\t\t PLAYER:1 [X]"<<endl<<"\t\t PLAYER:2 [O]"<<endl; | |
cout << "\t\t _________________\n"; | |
cout << "\t\t| | | |\n"; | |
cout << "\t\t| "<<table[0][0]<<" | "<<table[0][1]<<" | "<<table[0][2]<<" |\n"; | |
cout << "\t\t|_____|_____|_____|\n"; | |
cout << "\t\t| | | |\n"; | |
cout << "\t\t| "<<table[1][0]<<" | "<<table[1][1]<< " | "<<table[1][2]<< " |\n"; | |
cout << "\t\t|_____|_____|_____|\n"; | |
cout << "\t\t| | | |\n"; | |
cout << "\t\t| "<<table[2][0]<<" | "<<table[2][1]<< " | "<<table[2][2]<< " |\n"; | |
cout << "\t\t|_____|_____|_____|\n"; | |
} | |
void player_turn() | |
{ | |
int choice; | |
if(turn== 'X') | |
{cout << endl<<"\t\tPLAYER:1 [X] turn: ";} | |
if(turn== 'O') | |
{cout << endl<<"\t\tPLAYER:2 [O] turn: ";} | |
cin>> choice; | |
switch(choice) | |
{ | |
case 1: row=0,column=0; break; | |
case 2: row=0,column=1; break; | |
case 3: row=0,column=2; break; | |
case 4: row=1,column=0; break; | |
case 5: row=1,column=1; break; | |
case 6: row=1,column=2; break; | |
case 7: row=2,column=0; break; | |
case 8: row=2,column=1; break; | |
case 9: row=2,column=2; break; | |
default: | |
cout<<"\n ILLEGAL MOVE\n"; break; | |
} | |
if(turn=='X' && table[row][column]!='X' && table[row][column]!='O') | |
{ | |
table[row][column]= 'X'; | |
turn='O'; | |
} | |
else if(turn=='O' && table[row][column]!='X' && table[row][column]!='O') | |
{ | |
table[row][column]= 'O'; | |
turn ='X'; | |
} | |
else if(table[row][column]=='X' || table[row][column]=='O') //BOX ALREADY FILLED | |
{ cout<<" T R Y A G A I N !!"; | |
player_turn(); | |
} | |
display_table(); | |
} | |
bool game_over() | |
{//check win: | |
int i,j; | |
if( | |
(table[0][0]==table[1][1] && table[0][0]==table[2][2])|| //diagonal check | |
(table[0][2]==table[1][1] && table[0][2]==table[2][0])) //diagonal check | |
return(false); | |
for(i=0;i<=2;i++) | |
{ | |
if(table[i][0]==table[i][1] && table[i][0]==table[i][2]) /*row check*/ | |
return (false); | |
} | |
for(j=0;j<=2;j++) | |
{ | |
if( table[0][j]==table[1][j] && table[0][j]==table[2][j]) /* column check*/ | |
return (false); | |
} | |
for(i=0;i<=2;i++) | |
for(j=0;j<=2;j++) | |
{ | |
if(table[i][j]!= 'X'&&table[i][j]!='O') /* if boxes are empty or table is not filled completely*/ | |
return (true); | |
} | |
draw=true; | |
return (false); | |
} | |
int main() | |
{ | |
while(game_over()) | |
{ | |
display_table(); | |
player_turn(); | |
game_over(); | |
} | |
if(turn =='X' && draw == false) | |
cout<<"\tplayer:2 [O] Wins!\n\tCONGRATULATIONS\n"; | |
if(turn =='O' && draw ==false) | |
cout<<"\tplayer:1 [X] Wins!\n\tCONGRATULATIONS\n"; | |
if(draw==true) | |
cout<<"\n \t\tGAME DRAW !!"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
help me to improve it more