Last active
November 17, 2023 21:13
-
-
Save Bahaaio/1a519bd2e8ea183db2db404e0b9daac5 to your computer and use it in GitHub Desktop.
Tic Tac Toe game (system("cls") function will only work on windows)
This file contains hidden or 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 <windows.h> | |
using namespace std; | |
char table[3][3]={'1','2','3','4','5','6','7','8','9'}; | |
char player='X'; | |
void draw() | |
{ | |
system("cls"); | |
cout<<endl<<"Tic-Tac-Toe"<<endl<<endl; | |
for(int i=0;i<3;i++) | |
{ | |
cout<<"| "; | |
for(int j=0;j<3;j++) | |
{ | |
cout<<table[i][j]<<" | "; | |
} | |
cout<<endl<<"-------------"<<endl; | |
} | |
} | |
void input() | |
{ | |
int a; | |
cout<<"player "<<player<<"'s turn"<<endl<<endl; | |
cout<<"Choose a number: "; | |
cin>>a; | |
if(a == 1) | |
{ | |
if(table[0][0] == '1') | |
table[0][0] = player; | |
else | |
{ | |
cout <<endl<<"Field already in use!" <<endl<<endl; | |
input(); | |
} | |
} | |
else if(a == 2) | |
{ | |
if(table[0][1] == '2') | |
table[0][1] = player; | |
else | |
{ | |
cout <<endl<<"Field already in use!" <<endl<<endl; | |
input(); | |
} | |
} | |
else if(a == 3) | |
{ | |
if(table[0][2] == '3') | |
table[0][2] = player; | |
else | |
{ | |
cout <<endl<<"Field already in use!" <<endl<<endl; | |
input(); | |
} | |
} | |
else if(a == 4) | |
{ | |
if(table[1][0] == '4') | |
table[1][0] = player; | |
else | |
{ | |
cout <<endl<<"Field already in use!" <<endl<<endl; | |
input(); | |
} | |
} | |
else if(a == 5) | |
{ | |
if(table[1][1] == '5') | |
table[1][1] = player; | |
else | |
{ | |
cout <<endl<<"Field already in use!" <<endl<<endl; | |
input(); | |
} | |
} | |
else if(a == 6) | |
{ | |
if(table[1][2] == '6') | |
table[1][2] = player; | |
else | |
{ | |
cout <<endl<<"Field already in use!" <<endl<<endl; | |
input(); | |
} | |
} | |
else if(a == 7) | |
{ | |
if(table[2][0] == '7') | |
table[2][0] = player; | |
else | |
{ | |
cout <<endl<<"Field already in use!" <<endl<<endl; | |
input(); | |
} | |
} | |
else if(a == 8) | |
{ | |
if(table[2][1] == '8') | |
table[2][1] = player; | |
else | |
{ | |
cout <<endl<<"Field already in use!" <<endl<<endl; | |
input(); | |
} | |
} | |
else if(a == 9) | |
{ | |
if(table[2][2] == '9') | |
table[2][2] = player; | |
else | |
{ | |
cout <<endl<<"Field already in use!" <<endl<<endl; | |
input(); | |
} | |
} | |
} | |
void playerswitch() | |
{ | |
if(player=='X') | |
{ | |
player='O'; | |
} | |
else | |
{ | |
player='X'; | |
} | |
} | |
char logic() | |
{ | |
if(table[0][0]=='X' && table[0][1]=='X' && table[0][2]=='X') | |
return 'X'; | |
else if(table[0][0]=='X' && table[1][0]=='X' && table[2][0]=='X') | |
return 'X'; | |
else if(table[0][2]=='X' && table[1][2]=='X' && table[2][2]=='X') | |
return 'X'; | |
else if(table[2][0]=='X' && table[2][1]=='X' && table[2][2]=='X') | |
return 'X'; | |
else if(table[0][0]=='X' && table[1][1]=='X' && table[2][2]=='X') | |
return 'X'; | |
else if(table[0][2]=='X' && table[1][1]=='X' && table[2][0]=='X') | |
return 'X'; | |
else if(table[0][0]=='O' && table[0][1]=='O' && table[0][2]=='O') | |
return 'O'; | |
else if(table[0][0]=='O' && table[1][0]=='O' && table[2][0]=='O') | |
return 'O'; | |
else if(table[0][2]=='O' && table[1][2]=='O' && table[2][2]=='O') | |
return 'O'; | |
else if(table[2][0]=='O' && table[2][1]=='O' && table[2][2]=='O') | |
return 'O'; | |
else if(table[0][0]=='O' && table[1][1]=='O' && table[2][2]=='O') | |
return 'O'; | |
else if(table[0][2]=='O' && table[1][1]=='O' && table[2][0]=='O') | |
{ return 'O';} | |
return '/'; | |
} | |
void startgame() | |
{ table[0][0]='1'; table[0][1]='2'; table[0][2]='3'; | |
table[1][0]='4'; table[1][1]='5'; table[1][2]='6'; | |
table[2][0]='7'; table[2][1]='8'; table[2][2]='9'; | |
string again; | |
draw(); | |
while(true) | |
{ | |
input(); | |
draw(); | |
playerswitch(); | |
if(logic()=='X') | |
{ | |
cout<<"Player x won!"<<endl<<endl<<"Play again? "; | |
cin>>again; | |
if(again=="yes" || again=="y") | |
{ | |
startgame(); | |
} | |
else if(again=="no" || again=="n") | |
{ | |
exit(0); | |
} | |
} | |
else if(logic()=='O') | |
{ | |
cout<<"Player O won!"<<endl<<endl<<"Play again? "; | |
cin>>again; | |
cout<<endl; | |
if(again=="yes" || again=="y") | |
{ | |
startgame(); | |
} | |
else if(again=="no" || again=="n") | |
{ | |
exit(0); | |
} | |
} | |
else if(table[0][0]!='1' && table[0][1]!='2' && table[0][2]!='3' && | |
table[1][0]!='4' && table[1][1]!='5' && table[1][2]!='6' && | |
table[2][0]!='7' && table[2][1]!='8' && table[2][2]!='9' && logic()!='O' && logic()!='X') | |
{ | |
cout<<"It's a draw!"<<endl<<endl<<"Play again? "; | |
cin>>again; | |
cout<<endl; | |
if(again=="yes" || again=="y") | |
{ | |
startgame(); | |
} | |
else if(again=="no" || again=="n") | |
{ | |
exit(0); | |
} | |
} | |
} | |
} | |
int main() | |
{ | |
startgame(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment