现在的位置: 首页 > 综合 > 正文

五子棋的procedure

2018年04月01日 ⁄ 综合 ⁄ 共 2233字 ⁄ 字号 评论关闭

//For an explanation of five chess ,Number one and number two players take turns playing chess, 1 black, 2 white

 

#include <stdio.h>
/*
 1 2 3 4 5 6 7 8 9 A B C D E F
1   + + + + + + + + + + + + + + +
2   + + + + + + + + + + + + + + +
3   + + + + + + + + + + + + + + +
4   + + + + + + + + + + + + + + +
5   + + + + + + + + + + + + + + +
6   + + + + + + + + + + + + + + +
7   + + + + + + + + + + + + + + +
8   + + + + + + + + + + + + + + +
9   + + + + + + + + + + + + + + +
A   + + + + + + + + + + + + + + +
B   + + + + + + + + + + + + + + +
C   + + + + + + + + + + + + + + +
D   + + + + + + + + + + + + + + +
E   + + + + + + + + + + + + + + +
F   + + + + + + + + + + + + + + +

*/
void print(int (*a)[15],int n)    //Refresh the current state board printing
{
 int i,j;
 printf("  1 2 3 4 5 6 7 8 9 A B C D E F\n");
 for(i = 0; i < n; i++)
 {
  printf("%X ",i + 1);
  for(j = 0; j < 15; j++)
  {
   switch(a[i][j])
   {
    case 0: printf("+ ");break; 
    case 1: printf("@ ");break; 
    case 2: printf("0 ");break; 
    defult: break;
   }
  }
  putchar('\n');
 }
}

int input(int (*a)[15],int n,int x,int y,int id)   //Under the current players
{
 if(x > 0 && x < n + 1 && y > 0 && y < 0x10 && a[x - 1][y - 1] == 0)
 {
  a[x - 1][y - 1] = id;
  return 0;
 }
 return -1;
}

int proc(int (*a)[15],int n)            //Play chess player process
{
 int x,y,id = 1;
// while(id = id == 1 ? 2 : 1)
// while(id = id ^ 3 )
  while(id = ~id & 3 )   //Players take turns to play chess
 {
  do
  {
   printf("[%d] > ", id);
   fflush(stdout);   //Brush the buffer
   scanf("%x%x",&x, &y);
  }
  while (input(a,15,x,y,id));
  print(a,15);
  if(judge(a,15,x,y))   //Chess process rules
  {
   printf("your victory!%d\n",id);
   return 0;
  }
 }
// scanf("%x%x",&x, &y);
}
int judge(int (*a)[15],int n,int x,int y) //Rules comparison of the five chess
{
 x--,y--;
 int i;
 for(i = 0; i < 5; i++)
 {
 // printf("a[%d][%d],a[%d][%d],a[%d][%d],a[%d][%d],a[%d][%d]\n",x,y-i,x,y+1-i,x,y+2-i,x,y+3-i,x,y+4-i);
  if( y - i >= 0
   && y + 4 -i <= 0xF
   && a[x][y - i] == a[x][y + 1 -i]
   && a[x][y - i] == a[x][y + 2 -i]
   && a[x][y - i] == a[x][y + 3 -i]
   && a[x][y - i] == a[x][y + 4 -i]
   ||x - i >= 0
   && x + 4 -i <= 0xF
   && a[x - i][y] == a[x + 1 - i][y]
   && a[x - i][y] == a[x + 2 - i][y]
   && a[x - i][y] == a[x + 3 - i][y]
   && a[x - i][y] == a[x + 4 - i][y]
   ||y - i >= 0
   && y + 4 -i <=0xF
   && x -i >= 0
   && x + 4 - i <= 0xF
   &&a[x - i][y - i] == a[x + 1 - i][y + 1 - i]
   &&a[x - i][y - i] == a[x + 2 - i][y + 2 - i]
   &&a[x - i][y - i] == a[x + 3 - i][y + 3 - i]
   &&a[x - i][y - i] == a[x + 4 - i][y + 4 - i]
   ||y - i >= 0
   && y + 4 - i <= 0xF
   && x + i >=0
   && x -4 + i <= 0xF
   &&a[x + i][y - i] == a[x - 1 + i][y + 1 - i]
   &&a[x + i][y - i] == a[x - 2 + i][y + 2 - i]
   &&a[x + i][y - i] == a[x - 3 + i][y + 3 - i]
   &&a[x + i][y - i] == a[x - 4 + i][y + 4 - i]
   )
  {
   return 1;
  }
 }

 return 0;
}

int main()
{
 int a[15][15] = {0};

 print(a,15);
 proc(a,15);

 return 0;

}

抱歉!评论已关闭.