这个题我觉得就是一个一个的查找,我调用一个函数,首先找到双方的k,然后以此为据点向四周辐射,看看有没有能吃掉他的棋子,值得注意的是要可能会有“蹩马腿”之类的现象,所以要仔细搜索以下是代码:
#include<stdio.h>
#include<string.h>
char number[8][9];
int black(int x,int y,int );
int main()
{
 int i,j,m=0,total,result,result_1;
 do
 {
  result_1=result=total=0;
  m++;
  if(m!=1)
   getchar();
  for(i=0;i<8;i++)
  {
   scanf("%s",number[i]);
   if(strcmp(number[i],"……..")==0)
    total++;
  }                        //完成输入;
  if(8==total)
   break;
  for(i=0;i<8;i++)
   for(j=0;j<8;j++)
   {
    if(number[i][j]=='K')
     result=black(i,j,1);
    if(number[i][j]=='k')
     result_1=black(i,j,2);
   }                        //寻找王,然后调用函数
  if(result==1)
   printf("Game #%d: white king is in check.\n",m);
  if(result_1==1)
   printf("Game #%d: black king is in check.\n",m);
  if(result==0&&result_1==0)
   printf("Game #%d: no king is in check.\n",m);

 }while(total!=64);
 return 0;
}

int black(int x,int y,int point)
{
 int i,j,hengshu=0,xiang=0;
 char qizi[7]="rqpbkn";        //能吃掉王的棋子;
 if(point==2)
 {
  for(i=0;qizi[i];i++)
   qizi[i]-=32;
 }
 for(j=0;j<8;j++)
 {
  if((number[x][j]==qizi[0]||number[x][j]==qizi[1])&&j!=y)
  {
   if(j<y)
   {
    for(i=j+1;i<y;i++)
     if(number[x][i]!='.')//保证中间没有棋子才能吃掉;
     {
      hengshu=1;
      break;
     }
    if(hengshu==0)
     return 1;
   }
   else
   {
    for(i=j-1;i>y;i–)
     if(number[x][i]!='.')
     {
      hengshu=1;
      break;
     }
    if(hengshu==0)
     return 1;
   }
  }
 }
 for(j=0;j<8;j++)
 {
  if((number[j][y]==qizi[0]||number[j][y]==qizi[1])&&j!=x)
  {
   if(j<x)
   {
    for(i=j+1;i<x;i++)
     if(number[i][y]!='.')
     {
      hengshu=1;
      break;
     }
    if(hengshu==0)
     return 1;
   }
   else
   {
    for(i=j-1;i>x;i–)
     if(number[i][y]!='.')
     {
      hengshu=1;
      break;
     }
    if(hengshu==0)
     return 1;
   }
  }
 }

 if(point==1)
 {
  i=x-1;
  j=y-1;
  if(i>=0&&j>=0&&number[i][j]==qizi[2])
    return 1;
  i=x-1;
  j=y+1;
  if(i>=0&&j<8&&number[i][j]==qizi[2])
    return 1;
 }
 if(point==2)
 {
  i=x+1;
  j=y-1;
  if(i<8&&j>=0&&number[i][j]==qizi[2])
    return 1;
  i=x+1;
  j=y+1;
  if(i<8&&j<8&&number[i][j]==qizi[2])
    return 1;

 }
 i=y-1;
 j=x-1;
 xiang=0;
 while(j>=0&&i>=0)
 {
  
  if(number[j][i]=='.')
  {
   j–;
   i–;
   continue;
  }
  if((number[j][i]==qizi[3]||number[j][i]==qizi[1])&&xiang==0)
   return 1;
  if(!(number[j][i]==qizi[3]||number[j][i]==qizi[1]||number[j][i]=='.'))
   xiang=1;
  j–;
  i–;
 
 }
 i=y-1;
 j=x+1;
 xiang=0;
 while(j<8&&i>=0)
 {
  
  if(number[j][i]=='.')
  {
   j++;
   i–;
   continue;
  }
  if((number[j][i]==qizi[3]||number[j][i]==qizi[1])&&xiang==0)
   return 1;
  if(!(number[j][i]==qizi[3]||number[j][i]==qizi[1]||number[j][i]=='.'))
   xiang=1;
  j++;
  i–;
 }
 i=y+1;
 j=x-1;
 xiang=0;
 while(j>=0&&i<8)
 {
  
  if(number[j][i]=='.')
  {
   j–;
   i++;
   continue;
  }
  if((number[j][i]==qizi[3]||number[j][i]==qizi[1])&&xiang==0)
   return 1;
  if(!(number[j][i]==qizi[3]||number[j][i]==qizi[1]||number[j][i]=='.'))
   xiang=1;
  j–;
  i++;
 }
 i=y+1;
 j=x+1;
 xiang=0;
 while(j<8&&i<8)
 {
  
  if(number[j][i]=='.')
  {
   j++;
   i++;
   continue;
  }
  if((number[j][i]==qizi[3]||number[j][i]==qizi[1])&&xiang==0)
   return 1;
  if(!(number[j][i]==qizi[3]||number[j][i]==qizi[1]||number[j][i]=='.'))
   xiang=1;
  j++;
  i++;
 }

 i=x-1;
 j=y-1;
 if(i>=0&&j>=0&&number[i][j]==qizi[4])
   return 1;
 i=x-1;
 j=y;
 if(i>=0&&number[i][j]==qizi[4])
   return 1;
 i=x-1;
 j=y+1;
 if(i>=0&&j<8&&number[i][j]==qizi[4])
   return 1;
 i=x;
 j=y-1;
 if(j>=0&&number[i][j]==qizi[4])
   return 1;
 i=x;
 j=y+1;
 if(j<8&&number[i][j]==qizi[4])
   return 1;
 i=x+1;
 j=y-1;
 if(i<8&&j>=0&&number[i][j]==qizi[4])
   return 1;
 i=x+1;
 j=y;
 if(i<8&&number[i][j]==qizi[4])
   return 1;
 i=x+1;
 j=y+1;
 if(i<8&&j<8&&number[i][j]==qizi[4])
   return 1;


 i=x-1;
 j=y-2;
 if(i>=0&&j>=0&&number[i][j]==qizi[5])
   return 1;
 i=x-2;
 j=y-1;
 if(i>=0&&j>=0&&number[i][j]==qizi[5])
   return 1;
 i=x+1;
 j=y-2;
 if(i<80&&j>=0&&number[i][j]==qizi[5])
   return 1;
 i=x+2;
 j=y-1;
 if(i<8&&j>=0&&number[i][j]==qizi[5])
   return 1;
 i=x-1;
 j=y+2;
 if(i>=0&&j<8&&number[i][j]==qizi[5])
   return 1;
 i=x-2;
 j=y+1;
 if(i>=0&&j<8&&number[i][j]==qizi[5])
   return 1;
 i=x+1;
 j=y+2;
 if(i<8&&j<8&&number[i][j]==qizi[5])
   return 1;
 i=x+2;
 j=y+1;
 if(i<8&&j<8&&number[i][j]==qizi[5])
   return 1;
 return 0;
}

发表评论

电子邮件地址不会被公开。

Post Navigation