Skip to content

Instantly share code, notes, and snippets.

@shubham9411
Last active March 18, 2017 19:48
Show Gist options
  • Select an option

  • Save shubham9411/c1e82864364b6aeff8863e72c8fc0f2d to your computer and use it in GitHub Desktop.

Select an option

Save shubham9411/c1e82864364b6aeff8863e72c8fc0f2d to your computer and use it in GitHub Desktop.
Tic Tac Toe implemented in CPP, Multiplayer
// Created 15 ‎January ‎2016, ‏‎6.49.10 PM 😛
#include<stdio.h>
#include<conio.h>
#include<process.h>
int ar[10],i,j,count=0,choice,turn;
void win(int des)
{
if(des==0)
{
printf("\n\n************Congretulations !!!!!!!!!!!!!!!!!\n"); //typos
printf("\a\a\t\tPlayer 1 wins!!!!!!!!\v\v\a\a");
exit(1000);
}
else
{
printf("\n\n*************Congretulations !!!!!!!!!!!!!!!!!\n"); //typos again
printf("\a\a\t\tPlayer 2 wins!!!!!!!!\v\v\a\a ");
exit(1000);
}
}
void check()
{
if(ar[0]==0&&ar[1]==0&&ar[2]==0)
win(0);
else if(ar[0]==1&&ar[1]==1&&ar[2]==1)
win(1);
else if(ar[3]==0&&ar[4]==0&&ar[5]==0)
win(0);
else if(ar[3]==1&&ar[4]==1&&ar[5]==1)
win(1);
else if(ar[6]==0&&ar[7]==0&&ar[8]==0)
win(0);
else if(ar[6]==1&&ar[7]==1&&ar[8]==1)
win(1);
else if(ar[0]==0&&ar[4]==0&&ar[8]==0)
win(0);
else if(ar[0]==1&&ar[4]==1&&ar[8]==1)
win(1);
else if(ar[2]==0&&ar[4]==0&&ar[6]==0)
win(0);
else if(ar[2]==1&&ar[4]==1&&ar[6]==1)
win(1);
else if(ar[0]==0&&ar[3]==0&&ar[6]==0)
win(1);
else if(ar[0]==1&&ar[3]==1&&ar[6]==1)
win(0);
else if(ar[1]==0&&ar[4]==0&&ar[7]==0)
win(0);
else if(ar[1]==1&&ar[4]==1&&ar[7]==1)
win(1);
else if(ar[2]==0&&ar[5]==0&&ar[8]==0)
win(0);
else if(ar[2]==1&&ar[5]==1&&ar[8]==1)
win(0);
}
void show()
{
for(i=0;i<9;i=i+3)
{
printf("\t\t\t");
for(j=i;j<i+3;j++)
{
if(ar[j]==-1){
printf(" %d ||",(j+1));
}
else if(ar[j]==0)
{
printf(" O ||");
}
else
printf(" X ||");
}
printf("\n\t\t\t----------------------\n");
}
}
int main()
{
for(i=0;i<9;i++)
{
ar[i]=-1;
}
show();
while(count<9)
{
turn=count%2;
printf("Trun of player %d\n Enter your choice : ",(turn+1));
scanf("%d",&choice);
count++;
if(ar[choice-1]==0||ar[choice-1]==1)
{
printf("invalid choice retry \n");
count--;
getch();
system("cls");
show();
continue;
}
else
{
if(turn==0){
ar[choice-1]=0;
}
else if(turn==1)
{
ar[choice-1]=1;
}
}
system("cls");
show();
check();
}
if(count==9){
printf("\n\n\tOOPs no one wins !!!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment