/*Program for Tic-Tac-Toe Game*/
#include<stdio.h>
void main()
{
char
ttt[3][3],p1='x',p2='o',wp1,wp2;
int
i,j,k,c,gp1=0,gp2=0,pos;
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
ttt[i][j]='
';
for(k=0;k<9;k++)
{
abc:
printf("\n\n1 | 2
| 3\n");
printf("\n4 | 5
| 6\n");
printf("\n7 | 8
| 9");
if(gp1==0)
{
printf("\nPlayer1::\nSelect the position::");
scanf("%d",&pos);
}
else
if(gp2==0)
{
printf("\nPlayer2::\nSelect the position::");
scanf("%d",&pos);
}
switch(pos)
{
case 1:
i=0,j=0;
break;
case 2:
i=0,j=1;
break;
case 3:
i=0,j=2;
break;
case 4:
i=1,j=0;
break;
case 5:
i=1,j=1;
break;
case 6:
i=1,j=2;
break;
case 7:
i=2,j=0;
break;
case 8:
i=2,j=1;
break;
case 9:
i=2,j=2;
break;
default:
printf("\nWrong Option.");
k--;
goto abc;
}
if(gp1==0)
{
if(ttt[i][j]!='x' && ttt[i][j]!='o')
{
ttt[i][j]=p1;
gp1=1;
gp2=0;
}
else
{
printf("\nAlready selected.");
k--;
continue;
}
}
else
if(gp2==0)
{
if(ttt[i][j]!='x' && ttt[i][j]!='o')
{
ttt[i][j]=p2;
gp2=1;
gp1=0;
}
else
{
printf("\nAlready selected.");
k--;
continue;
}
}
if(ttt[0][0]=='x'&&ttt[1][1]=='x'&&ttt[2][2]=='x')
{
wp1='w';
break;
}
else
if(ttt[0][2]=='x'&&ttt[1][1]=='x'&&ttt[2][0]=='x')
{
wp1='w';
break;
}
else
if(ttt[0][0]=='x'&&ttt[1][0]=='x'&&ttt[2][0]=='x')
{
wp1='w';
break;
}
else
if(ttt[0][1]=='x'&&ttt[1][1]=='x'&&ttt[2][1]=='x')
{
wp1='w';
break;
}
else
if(ttt[0][2]=='x'&&ttt[1][2]=='x'&&ttt[2][2]=='x')
{
wp1='w';
break;
}
else
if(ttt[0][0]=='x'&&ttt[0][1]=='x'&&ttt[0][2]=='x')
{
wp1='w';
break;
}
else
if(ttt[1][0]=='x'&&ttt[1][1]=='x'&&ttt[1][2]=='x')
{
wp1='w';
break;
}
else
if(ttt[2][0]=='x'&&ttt[2][1]=='x'&&ttt[2][2]=='x')
{
wp1='w';
break;
}
if(ttt[0][0]=='o'&&ttt[1][1]=='o'&&ttt[2][2]=='o')
{
wp2='w';
break;
}
else
if(ttt[0][2]=='o'&&ttt[1][1]=='o'&&ttt[2][0]=='o')
{
wp2='w';
break;
}
else
if(ttt[0][0]=='o'&&ttt[1][0]=='o'&&ttt[2][0]=='o')
{
wp2='w';
break;
}
else
if(ttt[0][1]=='o'&&ttt[1][1]=='o'&&ttt[2][1]=='o')
{
wp2='w';
break;
}
else
if(ttt[0][2]=='o'&&ttt[1][2]=='o'&&ttt[2][2]=='o')
{
wp2='w';
break;
}
else
if(ttt[0][0]=='o'&&ttt[0][1]=='o'&&ttt[0][2]=='o')
{
wp2='w';
break;
}
else
if(ttt[1][0]=='o'&&ttt[1][1]=='o'&&ttt[1][2]=='o')
{
wp2='w';
break;
}
else
if(ttt[2][0]=='o'&&ttt[2][1]=='o'&&ttt[2][2]=='o')
{
wp2='w';
break;
}
printf("\n---------------------");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("| %c |",ttt[i][j]);
}
printf("\n---------------------");
}
}
if(wp1!='w'&&wp2!='w')
printf("\nMatch Draw.");
else
if(wp1=='w')
printf("\nPlayer1 won the game.");
else
if(wp2=='w')
printf("\nPlayer2 won the game.");
printf("\n---------------------");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("| %c |",ttt[i][j]);
}
printf("\n---------------------");
}
}
No comments:
Post a Comment