// i m gettin segmantation fault in my c code for tic tac toe game.. can ny one help me ..
//Two player game of tic tac toe
#include<stdio.h>
int a[2][2]={0,0,0,0,0,0,0,0,0};
int pos1,pos2,flag=0,max=0;
void fill(int flag)
{
int l,m,n;
while(max<=9)
{
if(flag%2==0)
{
B:
printf("Player 1 enter pos1 and pos2 varying between 0 to 2\n");
scanf("%d %d",pos1,pos2);
if(a[pos1][pos2]==4)
{
printf("Entry is invalid/already occupied\n");
goto B;
}
else
{
a[pos1][pos2]=3;
flag++;
max++;
l=check_row(pos1,pos2);
m=check_column(pos1,pos2);
if(l==1 || m==1)
{result(flag);}
if((pos1-pos2)%2==0)
{
n=check_diagonal(pos1,pos2);
if(n==1)
{result(flag);}
}
}
}
else
{
A:
printf("Player 2 enter pos1 and pos2 varying between 0 to 2\n");
scanf("%d %d",pos1,pos2);
if(a[pos1][pos2]==3)
{
printf("Entry is invalid/already occupied\n");
goto A;
}
else
{
a[pos1][pos2]=4;
flag++;
max++;
check_row(pos1,pos2);
check_column(pos1,pos2);
if(l==1 || m==1)
{result(flag);}
if((pos1-pos2)%2==0)
{
check_diagonal(pos1,pos2);
if(n==1)
{result(flag);}
}
}
}
}
}
int check_diagonal(pos1,pos2)
{
int i,count=0;
if(pos1==pos2)
{
for(i=0;i<2;i++)
{
if(a==a[i+1][i+1])
{
count++;
}
}
if(count==2)
{
return 1;
}
}
else
{
i=1;
if(a==a[i+1][i-1]==a[i-1][i+1])
{
return 1;
}
}
}
int check_row(int pos1,int pos2)
{
if(a[pos1][0]==a[pos1][1]==a[pos1][2])
{return 1;}
}
int check_column(int pos1,int pos2)
{
if(a[0][pos2]==a[1][pos2]==a[2][pos2])
{return 1;}
}
result(int flag)
{
int k;
if(flag%2==0)
{
k=1;
}
else
{
k=2;
}
printf("Game over player %d wins,k");
}
void main()
{
int i;
printf("Game starts with player1\n");
fill(flag);
}