五子棋实现代码
#include
#define M 9
#define N 9
void print(int a[M][N])
{
int i,j;
for(i=0;i
{
for(j=0;j
printf(" %d",a[i][j]);
printf("\n");
}
}
int check(int a[M][N])
{
int i,j;
for(i=0;i
{
for(j=0;j
{
if((a[i][j]==1)&&(a[i+1][j]==1)&&(a[i+2][j]==1)&&(a[i+3][j]==1)&&(a[i+4][j]==1))
{
printf("player1 win\n");
return 0;
break;
}
else if((a[i][j]==1)&&(a[i][j+1]==1)&&(a[i][j+2]==1)&&(a[i][j+3]==1)&&(a[i][j+4]==1))
{
printf("player1 win\n");
return 0;
break;
}
else if((a[i][j]==1)&&(a[i+1][j+1]==1)&&(a[i+2][j+2]==1)&&(a[i+3][j+3]==1)&&(a[i+4][j+4]==1))
{
printf("player1 win\n");
return 0;
break;
}
else if((a[M-i][j]==1)&&(a[M-i-1][j+1]==1)&&(a[M-i-2][j+2]==1)&&(a[M-i-3][j+3]==1)&&(a[M-i-4][j+4]==1))
{
printf("player1 win\n");
return 0;
break;
}
if((a[i][j]==2)&&(a[i+1][j]==2)&&(a[i+2][j]==2)&&(a[i+3][j]==2)&&(a[i+4][j]==2))
{
printf("player2 win\n");
return 0;
break;
}
else if((a[i][j]==2)&&(a[i][j+1]==2)&&(a[i][j+2]==2)&&(a[i][j+3]==2)&&(a[i][j+4]==2))
{
printf("player2 win\n");
return 0;
break;
}
else if((a[i][j]==2)&&(a[i+1][j+1]==2)&&(a[i+2][j+2]==2)&&(a[i+3][j+3]==2)&&(a[i+4][j+4]==2))
{
printf("player2 win\n");
return 0;
break;
}
else if((a[M-i][j]==2)&&(a[M-i-1][j+1]==2)&&(a[M-i-2][j+2]==2)&&(a[M-i-3][j+3]==2)&&(a[M-i-4][j+4]==2))
{
printf("player2 win\n");
return 0;
break;
}
}
}
return 1;
}
int main()
{
int a[M][N]={0},b[M][N]={0};
print(a);
int i,j;
int count=1,num=0;
while(count)
{
num++;
if(num%2==1)
{
printf("player1 input:");
scanf("%d",&i);
scanf("%d",&j);
a[i-1][j-1]=1;
print(a);
count=check(a);
for(i=0;i
for(j=0;j
b[i][j]=a[i][j];
}
else if(num%2==0)
{
printf("player2 input:");
scanf("%d",&i);
scanf("%d",&j);
b[i-1][j-1]=2;
print(b);
count=check(b);
for(i=0;i
for(j=0;j
a[i][j]=b[i][j];
}
}
return 0;
}
在redhat上运行