#include<stdio.h>
#include<stdio.h>
#include<windows.h>
#define N 20
//N为棋盘大小
int array[N][N];
void display(int length)
{
for(int i=0;i<length;i++)
for(int j=0;j<length;j++)
{
if (array[i][j]==0)
printf("%c%c",'O',j==length-1? '\n' : '\0');
else if (array[i][j]==1)
printf("%c%c",'X',j==length-1? '\n' : '\0');
else
printf("%c%c",'V',j==length-1? '\n' : '\0');
}
}
void init(int length)
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
array[i][j]=0;
}
}
}
int identify(int length, int x, int y, int flag)
{
int count=0;
int temp1=x;
int temp2=y;
//先检查上下是否五子相连
for(;temp1>=0;temp1--)
{
if(array[temp1][temp2]==flag)
count++;
else
break;
}//UP
temp1=x;
for(;temp1<length;temp1++)
{
if(array[temp1][temp2]==flag)
count++;
else
break;
}//DOWN
if(count-1==5)
return 1;
//检查横向的
temp1=x;
temp2=y;
count=0;
for(;temp2>=0;temp2--)
{
if(array[temp1][temp2]==flag)
count++;
else
break;
}//LEFT
temp2=y;
for(;temp2<length;temp2++)
{
if(array[temp1][temp2]==flag)
count++;
else
break;
}//RIGHT
if(count-1==5)
return 1;
//检查左对角线
temp1=x;
temp2=y;
count=0;
for(;temp2>=0 && temp1>=0;temp2--,temp1--)
{
if(array[temp1][temp2]==flag)
count++;
else
break;
}//LEFT UP
temp1=x;
temp2=y;
for(;temp2<length && temp1<length;temp2++,temp1++)
{
if(array[temp1][temp2]==flag)
count++;
else
break;
}//RIGHT UP
if(count-1==5)
return 1;
//检查右对角线
temp1=x;
temp2=y;
count=0;
for(;temp2>=0&& temp1<length;temp2--,temp1++)
{
if(array[temp1][temp2]==flag)
count++;
else
break;
}//LEFT DWON
temp1=x;
temp2=y;
for(;temp2<length && temp1>=0;temp2++,temp1--)
{
if(array[temp1][temp2]==flag)
count++;
else
break;
}//RIGHT UP
if(count-1==5)
return 1;
//如果都不符合
return 0;
}
int main()
{
int flag;
while(1)
{
display(N);
puts("请决定谁先出(1表示 X ,-1表示 V)");
scanf("%d",&flag);//决定谁先出子
int x,y;
while (1)
{
if (flag==1)
{
puts("X下子");
while(scanf("%d %d",&x,&y))
{
if(array[x][y]==0)
break;
else
puts("此处有落子!");
}
array[x][y]=1;
Sleep(500);
display(N);
if(identify(N,x,y,1))
{
puts("X WIN!");
break;
}
flag=-flag;
}
else
{
puts("V下子");
while(scanf("%d %d",&x,&y))
{
if(array[x][y]==0)
break;
else
puts("此处有落子!");
}
array[x][y]=-1;
Sleep(500);
display(N);
if(identify(N,x,y,-1))
{
puts("V WIN!");
break;
}
flag=-flag;
}
}
Sleep(1000);
puts("是否选择重新开始?(Y/N)");
char ss;
scanf("%*c%c%*c",&ss);
if(ss=='N')
return 0;
init(N);
}
return 0;
}
游戏开始选定一方先出,然后轮流下子(输入落子坐标),五子相连则赢得此局。