Last active
January 6, 2021 12:35
-
-
Save jainsamyak/e31a89c5b9225f61a6ab5a477ceeba1f to your computer and use it in GitHub Desktop.
A C++ program for the classic Tic-Tac-Toe game built on Dev C++
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
/* | |
MIT License | |
Copyright (c) [2017] [Samyak Jain] | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE | |
*/ | |
#include<iostream> | |
#include<conio.h> | |
#include<stdlib.h> | |
#include<stdio.h> | |
#include<string.h> | |
using namespace std; | |
void drawsym(char c,int i) //parameters: character to print,number of prints | |
{ | |
for(int j=0;j<i;j++) | |
{ | |
cout<<c; | |
} | |
cout<<"\n"; | |
} | |
int toss() //to generate a random number between 1-100 | |
{ | |
int i=rand(); | |
if(i%2==0) | |
{ | |
return 1; | |
} | |
else | |
{ | |
return 2; | |
} | |
} | |
void display_board(char board[3][3]) //parameters: board | |
{ | |
system("cls"); | |
cout<<"\n\t\t\t\tTIC-TAC-TOE\n"; | |
drawsym('-',80); | |
cout<<"\n__________ __________ __________"; | |
cout<<"\n | |"; | |
for(int i=0;i<3;i++) | |
{ | |
cout<<"\n "<<board[i][0]<<" | "<<board[i][1]<<" | "<<board[i][2]<<"\n"; | |
for(int j=0;j<3;j++) | |
{ | |
cout<<"___"<<"_______|"; | |
} | |
if(i!=2) | |
{ | |
cout<<"\n | |"; | |
cout<<"\n | |"; | |
} | |
} | |
cout<<"\n"; | |
} | |
int check_horizontal(char c,char board[3][3]) //parameters: character x or o and board | |
{ | |
int count; | |
for(int i=0;i<3;i++) | |
{ | |
count=0; | |
for(int j=0;j<3;j++) | |
{ | |
if(board[i][j]==c) | |
{ | |
count++; | |
} | |
if(count==3) | |
{ | |
return 1; | |
break; | |
} | |
} | |
} | |
} | |
int check_vertical(char c,char board[3][3]) //parameters: x or o and board | |
{ | |
int count; | |
for(int i=0;i<3;i++) | |
{ | |
count=0; | |
for(int j=0;j<3;j++) | |
{ | |
if(board[j][i]==c) | |
{ | |
count++; | |
} | |
if(count==3) | |
{ | |
return 1; | |
break; | |
} | |
} | |
} | |
} | |
int check_diagonal(char c,char board[3][3]) //parameters: x or o and board | |
{ | |
int count=0; | |
for(int i=0;i<3;i++) | |
{ | |
if(board[i][i]==c || board[i][2-i]==c) | |
{ | |
count++; | |
} | |
if(count==3) | |
{ | |
return 1; | |
break; | |
} | |
} | |
} | |
void input(char board[3][3],char p1[20],char p2[20],int e,char x) | |
{ | |
char move,y; | |
int p; | |
char *player1[20],*player2[20]; | |
int tracker,rch1,rcv1,rcd1,rch2,rcv2,rcd2; | |
if(x=='X' || x=='x') | |
{ | |
y='O'; | |
} | |
else if(x=='O' || x=='o') | |
{ | |
y='X'; | |
} | |
if(e==1) | |
{ | |
*player1=p1; | |
*player2=p2; | |
} | |
else if(e==2) | |
{ | |
*player1=p2; | |
*player2=p1; | |
} | |
for(p=1;p<10;p++) | |
{ | |
tracker=0; | |
if(p%2!=0) | |
{ | |
cout<<"\n"<<*player1<<" please enter your move: "; | |
move=getch(); | |
cout<<move; | |
} | |
if(p%2==0) | |
{ | |
cout<<"\n"<<*player2<<" please enter your move: "; | |
move=getch(); | |
cout<<move; | |
} | |
for(int i=0;i<3;i++) | |
{ | |
for(int j=0;j<3;j++) | |
{ | |
if(move==board[i][j] && p%2!=0) | |
{ | |
board[i][j]=x; | |
tracker+=1; | |
rch1=check_horizontal(x,board); | |
rcv1=check_vertical(x,board); | |
rcd1=check_diagonal(x,board); | |
} | |
if(move==board[i][j] && p%2==0) | |
{ | |
board[i][j]=y; | |
tracker+=1; | |
rch2=check_horizontal(y,board); | |
rcv2=check_vertical(y,board); | |
rcd2=check_diagonal(y,board); | |
} | |
} | |
} | |
if(tracker==0) | |
{ | |
p--; | |
cout<<"Unacceptable input!! Please try again...."; | |
} | |
else | |
{ | |
display_board(board); | |
} | |
if(rch1==1 || rcv1==1 || rcd1==1) | |
{ | |
drawsym('-',80); | |
cout<<*player1<<" WINS THE GAME!!\n"; | |
drawsym('-',80); | |
break; | |
} | |
if(rch2==1 || rcv2==1 || rcd2==1) | |
{ | |
drawsym('-',80); | |
cout<<*player2<<" WINS THE GAME!!\n"; | |
drawsym('-',80); | |
break; | |
} | |
} | |
if(p==10) | |
{ | |
cout<<"\n\nHey!! Game over.... No winner, no loser!"; | |
} | |
} | |
main() | |
{ | |
system("cls"); | |
char board[3][3]={'1','2','3', | |
'4','5','6', | |
'7','8','9'}; | |
int won,i=1,x1=1,x2=1; | |
char p1[20]={0},p2[20]={0},x; | |
cout<<"\n\t\t\t\tTIC-TAC-TOE\n"; | |
drawsym('-',80); | |
while(x1<2) | |
{ | |
if(strlen(p1)==0) | |
{ | |
cout<<"\nPlayer 1: Please enter your name: "; | |
gets(p1); | |
x1=1; | |
} | |
else | |
{ | |
cout<<"\n"; | |
drawsym('*',80); | |
x1=2; | |
} | |
} | |
while(x2<2) | |
{ | |
if(strlen(p2)==0) | |
{ | |
cout<<"\nPlayer 2: Please enter your name: "; | |
gets(p2); | |
x2=1; | |
} | |
else | |
{ | |
cout<<"\n"; | |
drawsym('*',80); | |
x2=2; | |
} | |
} | |
won=toss(); | |
if(won==1) | |
{ | |
cout<<p1<<" wins the toss!"; | |
while(i<2) | |
{ | |
cout<<"\nPlease enter your choice(X or O): "; | |
cin>>x; | |
if(x=='X' || x=='O' || x=='x' || x=='o') | |
{ | |
i=2; | |
} | |
else | |
{ | |
i=1; | |
} | |
} | |
} | |
else if(won==2) | |
{ | |
cout<<p2<<" wins the toss!"; | |
while(i<2) | |
{ | |
cout<<"\nPlease enter your choice(X or O): "; | |
cin>>x; | |
if(x=='X' || x=='O' || x=='x' || x=='o') | |
{ | |
i=2; | |
} | |
else | |
{ | |
i=1; | |
} | |
} | |
} | |
display_board(board); | |
input(board,p1,p2,won,x); | |
getch(); | |
} |
```c++ void drawsym(char c,int i) { for(int j=0;j<i;j++) { cout<<c; } cout<<"\n"; } void displayBoard(char arr[3][3]) { drawsym('-',120); cout<<"\n\t\t\t\t\t _______________________________"; cout<<"\n\t\t\t\t\t | | | |"; for(int i=0;i<3;i++) { cout<<"\n\t\t\t\t\t | "<<board[i][0]<<" | "<<board[i][1]<<" | "<<board[i][2]<<" |\n"; cout<<"\t\t\t\t\t |__________|__________|_________|"; if(i!=2) { cout<<"\n\t\t\t\t\t | | | |"; } } cout<<"\n"; }Here this is much cleaner. Great game tho.
Thanks for the contribution! Although this was nearly 4 years ago on the outdated Turbo C++, and I can no longer write c++😂
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here this is much cleaner. Great game tho.