人机对异五子棋源程序

/* This program is maked by nanchang*/
/* univercisty student ,The mechine */
/**/
/**/
/**/
/**/


#include<stdio.h>
#include<dos.h>
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<bios.h>
#include<string.h>
#include<math.h>

#define ESC 0x11b
#define LEFT 0x4b00
#define UP 0x4800
#define RIGHT 0x4d00
#define DOWN 0x5000
#define SPACE 0x3920
#define F2 0x3c00
#define F3 0x3d00
#define F4 0x3e00


/*x0 y0 scren is the course init postion*/
int x0,y0,map[15][15];/**/
int regret[125][2];/**/
int Found_Best,First_Arrive;/**/
int Depth_TT,Best_Num;
typedef struct ThreeTree/**/
{
    int i;           /**/
    int j;          /**/
    int num;       /**/
    struct ThreeTree *parent;/**/
    struct ThreeTree *LChild;/**/
    struct ThreeTree *MChild;/**/
    struct ThreeTree *RChild;/**/
}TNode,*TTree;              /**/

 

void InitGraph();/**/
void CloseGraph();/**/
void DrawMap();  /**/
void DrawChess(); /**/
void GameControl(); /**/
void DrawCourse(int Course_I,int Course_J,int flag);/**/
void DrawRegretChess(int Course_I,int Course_J); /**/
void SaveMap(int regret[][2],int Regret_Count);
void LoadMap(int regret[][2],int *Regret_Count);

int DepthTT(TTree TT);
void CreatTT(TTree TT,int mode);
int RearshBest(TTree TT,int *C_I,int *C_J);
void DestoryTT(TTree TT);
void ComputerThink(int *C_I,int *C_J,int mode);
void SetMap(TTree TT);
void ClearMap(TTree TT);
void GetComputerIJ(TTree TT,int *C_I,int *C_J);
void GetPersonIJ(TTree TT,int *C_I,int *C_J);


int LinkSS4(int C_I,int C_J);/*211110 or 211101 or 211011*/
        /*210111 or 11110 or 11101 */
        /*11011 or 10111*/
int LinkSS3(int C_I,int C_J);/*211100 or 211010 or 211001*/
       /*210110 or 210101 or 21011*/
       /*11100 or 10110 or 10011 */
       /*10101*/
int LinkS5(int C_I,int C_J);/*1011101*/
int LinkS4(int C_I,int C_J);/*1010101*/
int LinkS3(int C_I,int C_J);/*1010100*/
int LinkF5(int C_I,int C_J);/*11111*/
int LinkF4(int C_I,int C_J);/*11110*/
int LinkF3(int C_I,int C_J);/*011100 or 010110*/
int LinkF2(int C_I,int C_J);/*011000 or 010100 or 010010*/
int LinkF1(int C_I,int C_J);/*1*/
void DrawTextXY(int x1,int y1,int x2,int y2,char s[]);
int PriorityGet(int C_I,int C_J,int num);
void  ScanMap(int b[3][3],int *N,int mode);

 


main(){
   InitGraph();
   x0=(getmaxx()-470)/2;
   y0=(getmaxy()-330)/2;
   DrawMap();
   GameControl();
   CloseGraph();
}

 


void GameControl(){
  int Course_I=7,Course_J=7,key,mode=1,Regret_Count=0;
  int Computer_I,Computer_J,Game_Over=0,i;
  char str[20];
  strcpy(str,"Five Chess");
  DrawTextXY(x0+350,y0+10,x0+470,y0+30,str);
  strcpy(str,"");
  DrawCourse(Course_I,Course_J,1);

  do
  {
     Computer_I=-1;
     Computer_J=-1;
     key=bioskey(0);
     switch(key){
 case UP:
      DrawCourse(Course_I,Course_J,0);
      if(Course_I>0)Course_I--;
      else Course_I=14;
      break;
 case LEFT:
      DrawCourse(Course_I,Course_J,0);
      if(Course_J>0)Course_J--;
      else Course_J=14;
      break;
 case RIGHT:
      DrawCourse(Course_I,Course_J,0);
      if(Course_J<14)Course_J++;
      else Course_J=0;
      break;
 case DOWN:
      DrawCourse(Course_I,Course_J,0);
      if(Course_I<14)Course_I++;
      else Course_I=0;
      break;
 case SPACE:
      if(!map[Course_I][Course_J])
      {
   map[Course_I][Course_J]=mode;
   regret[Regret_Count][0]=Course_I;
   regret[Regret_Count][1]=Course_J;
   Regret_Count++;
   if(LinkF5(Course_I,Course_J))
   {
       Game_Over=1;
       break;
   }
   mode=3-mode;
   DrawChess();
   DrawTextXY(x0+325,y0+190,x0+478,y0+210," Thinking...");
   ComputerThink(&Computer_I,&Computer_J,mode);
   DrawTextXY(x0+325,y0+190,x0+478,y0+210," ");
   if(Computer_I!=-1)
   {
        map[Computer_I][Computer_J]=mode;
        regret[Regret_Count][0]=Computer_I;
        regret[Regret_Count][1]=Computer_J;
        Regret_Count++;
        DrawCourse(Course_I,Course_J,0);
        Course_I=Computer_I;
        Course_J=Computer_J;
        if(LinkF5(Course_I,Course_J))
        {
    Game_Over=1;
    break;
        }
        mode=3-mode;

    }

      }
      break;
 case F2:
      SaveMap(regret,Regret_Count--);
      break;
 case F3:
      LoadMap(regret,&Regret_Count);
      Course_I=regret[Regret_Count-1][0];
      Course_J=regret[Regret_Count-1][1];
      break;
 case F4:
      if(Regret_Count>0)
      {
    if(Regret_Count%2)mode=2;
    for(i=0;i<2;i++)
    {
        Regret_Count--;
        DrawRegretChess(regret[Regret_Count][0],regret[Regret_Count][1]);
        map[regret[Regret_Count][0]][regret[Regret_Count][1]]=0;
        DrawCourse(Course_I,Course_J,0);
        Course_I=regret[Regret_Count][0];
        Course_J=regret[Regret_Count][1];
        mode=3-mode;
    }
      }
      break;
     }/*end switch*/

     DrawCourse(Course_I,Course_J,1);
     DrawChess();
  }while(key!=ESC&&Game_Over!=1);
  if(mode==1)
  {
       DrawTextXY(x0+350,y0+190,x0+470,y0+210,"You Win");
  }
  else
  {
      DrawTextXY(x0+350,y0+190,x0+470,y0+210,"Computer Win");
  }
  sleep(2);
  CloseGraph();
}

void SaveMap(int regret[][2],int Regret_Count)
{
   char File_Name[12],str[50];
   char ch;
   int i=0;
   FILE *fp;
   DrawTextXY(x0+350,y0+190,x0+470,y0+210,"Input File name");
   ch='a';
   for(i=0;ch!='/r'&&i<8;i++)
   {
     ch=getch();
     if(i>0&&ch=='/b')
     {
  File_Name[i-1]=File_Name[i];
  i-=2;
     }
     else if(ch!='/r'&&ch!='b')
     {    File_Name[i]=ch;
   File_Name[i+1]='/0';
     }
     DrawTextXY(x0+350,y0+190,x0+470,y0+210,File_Name);
   }
   strcat(File_Name,".map");
   strcpy(str,"");
   strcpy(str,"Cann't open ");
   strcat(str,File_Name);
   if((fp=fopen(File_Name,"a+"))==NULL)
   {
 DrawTextXY(x0+350,y0+190,x0+470,y0+210,str);
 sleep(2);
 return;
   }

   for(i=0;i<Regret_Count;i++)
   {
      fprintf(fp,"%d %d/n",regret[i][0],regret[i][1]);
   }
   fclose(fp);

   DrawTextXY(x0+350,y0+190,x0+470,y0+210,"File success save");
   sleep(2);
   DrawTextXY(x0+350,y0+190,x0+470,y0+210,"");
}

void LoadMap(int regret[][2],int *Regret_Count)
{
   char File_Name[12],str[50];
   char ch;
   int i=0,mode;
   FILE *fp;
   DrawTextXY(x0+350,y0+190,x0+470,y0+210,"Input File name");
   ch='a';
   for(i=0;ch!='/r'&&i<8;i++)
   {
     ch=getch();
     if(i>0&&ch=='/b')
     {
  File_Name[i-1]=File_Name[i];
  i-=2;
     }
     else if(ch!='/r'&&ch!='b')
     {    File_Name[i]=ch;
   File_Name[i+1]='/0';
     }
     DrawTextXY(x0+350,y0+190,x0+470,y0+210,File_Name);
   }
   strcat(File_Name,".map");
   strcpy(str,"");
   strcpy(str,"Cann't open ");
   strcat(str,File_Name);
   if((fp=fopen(File_Name,"r+"))==NULL)
   {
 DrawTextXY(x0+350,y0+190,x0+470,y0+210,str);
 sleep(2);
 return;
   }
   for(i=0;i<(*Regret_Count);i++)
   {
       map[regret[*Regret_Count][0]][regret[*Regret_Count][1]]=0;
   }
   *Regret_Count=0;
   while(!feof(fp))
   {
       fscanf(fp,"%d%d",&regret[*Regret_Count][0],&regret[*Regret_Count][1]);
       if((*Regret_Count)%2==0)mode=2;
       else mode=1;
       map[regret[*Regret_Count][0]][regret[*Regret_Count][1]]=mode;
       (*Regret_Count)++;
   }
   fclose(fp);

}

void ComputerThink(int *C_I,int *C_J,int mode)
{
  TTree HeadTT,T;
  Found_Best=0;
  First_Arrive=0;
  Depth_TT=-1;
  Best_Num=0;

  HeadTT=(TTree)malloc(sizeof(TNode));
  HeadTT->parent=NULL;
  HeadTT->LChild=NULL;
  HeadTT->MChild=NULL;
  HeadTT->RChild=NULL;
  HeadTT->i=-1;
  HeadTT->j=-1;
  HeadTT->num=-1;
  T=HeadTT;

  CreatTT(T,mode);
  RearshBest(HeadTT,C_I,C_J);
  DestoryTT(HeadTT);
}


int RearshBest(TTree TT,int *C_I,int *C_J)
{
   int N;
   if(Found_Best==1)return;
   if(TT->LChild)RearshBest(TT->LChild,C_I,C_J);
   if(TT->MChild)RearshBest(TT->MChild,C_I,C_J);
   if(TT->RChild)RearshBest(TT->RChild,C_I,C_J);

   N=DepthTT(TT);
   if(First_Arrive!=1)
   {
       GetComputerIJ(TT,C_I,C_J);
       Depth_TT=N;
   }
   First_Arrive=1;

   if(N==1&&TT->num==17)
   {
       GetComputerIJ(TT,C_I,C_J);
       Found_Best=1;
   }

   if(N==2&&TT->num==17&&TT->parent->num!=17&&Found_Best==0)
   {
       GetPersonIJ(TT,C_I,C_J);
       Found_Best=1;
   }

   if(Found_Best==0)
   {
       if(N==1&&TT->num>10)
       {
    GetComputerIJ(TT,C_I,C_J);
    Found_Best=1;
    Depth_TT=N;
       }
       else if(N==2&&TT->num>9&&TT->parent->num<11)
       {
    GetPersonIJ(TT,C_I,C_J);
    Found_Best=1;
    Depth_TT=N;
       }
       else if(N==2&&TT->num>6&&TT->LChild->LChild->num>10&&TT->LChild->num<10)
       {
    GetPersonIJ(TT,C_I,C_J);
    Found_Best=1;
    Depth_TT=N;
       }
       else if(N==2&&TT->num==10&&TT->parent->num<10)
       {
   GetPersonIJ(TT,C_I,C_J);
   Found_Best=1;
   Depth_TT=N;
       }

       if(N<Depth_TT&&TT->num>9&&(N%2)==0)
       {
    GetPersonIJ(TT,C_I,C_J);
    Depth_TT=N;
       }
       if(N<Depth_TT&&TT->num>9&&(N%2))
       {
    GetComputerIJ(TT,C_I,C_J);
    Depth_TT=N;
       }
       if(N<=Depth_TT&&TT->num>Best_Num&&(N%2))
       {
    GetComputerIJ(TT,C_I,C_J);
    Depth_TT=N;
       }
       if(N<=Depth_TT&&TT->num>Best_Num&&(N%2)==0)
       {
    GetPersonIJ(TT,C_I,C_J);
    Depth_TT=N;
       }
   }

}


void GetComputerIJ(TTree TT,int *C_I,int *C_J)
{
   TTree T;
   T=TT;

   while(T->parent->i!=-1&&T->parent)
   {
       T=T->parent;
   }

   *C_I=T->i;
   *C_J=T->j;
   Best_Num=T->num;
}

void GetPersonIJ(TTree TT,int *C_I,int *C_J)
{
  TTree T;
  T=TT;
  while(T->parent->parent->i!=-1&&T->parent->parent)
  {
      T=T->parent;
  }
  *C_I=T->i;
  *C_J=T->j;
  Best_Num=T->num;
}

void DestoryTT(TTree TT)
{
    TNode *T;
    if(TT->LChild)DestoryTT(TT->LChild);
    if(TT->MChild)DestoryTT(TT->MChild);
    if(TT->RChild)DestoryTT(TT->RChild);
    T=TT;
    free(T);
}


void CreatTT(TTree TT,int mode)
{
   int num[3][3],NN,i;
   TTree NewTT,T;
   T=TT;
   NN=DepthTT(T);
   if(NN>5||TT->num==17)return;

   if(NN==0||NN%2==0)mode=2;
   else mode=1;

   NN=0;
   T=TT;
   SetMap(T);
   ScanMap(num,&NN,mode);
   T=TT;
   ClearMap(T);

   for(i=0;i<NN;i++)
   {
       NewTT=(TTree)malloc(sizeof(TNode));
       if(NewTT!=NULL)
       {
    NewTT->parent=TT;
    NewTT->i=num[i][0];
    NewTT->j=num[i][1];
    NewTT->num=num[i][2];
    NewTT->LChild=NULL;
    NewTT->MChild=NULL;
    NewTT->RChild=NULL;
    if(i==0)TT->LChild=NewTT;
    if(i==1)TT->MChild=NewTT;
    if(i==2)TT->RChild=NewTT;
       }  /*end if*/
   }/*end for*/

    if(TT->LChild)CreatTT(TT->LChild,mode);
    if(TT->MChild)CreatTT(TT->MChild,mode);
    if(TT->RChild)CreatTT(TT->RChild,mode);
}


void SetMap(TTree TT)
{
   TTree T;
   int N,mode;

   T=TT;
   N=DepthTT(T);

   if(N<1)return;
   if(N%2)mode=2;
   else mode=1;

   T=TT;
   while(T)
   {
 if(T->i!=-1)map[T->i][T->j]=mode;
 T=T->parent;
 mode=3-mode;
   }
}


void ClearMap(TTree TT)
{
  TTree T;
  T=TT;
  while(T)
  {
      if(T->i!=-1)map[T->i][T->j]=0;
      T=T->parent;
  }
}

int DepthTT(TTree TT)
{
  int depth=-1;
  while(TT)
  {
      depth++;
      TT=TT->parent;
  }
  return depth;
}

void ScanMap(int num[3][3],int *N,int mode)
{
  int i,j,k,count,direct,direct_num;
  int b[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
  for(k=17;k>0&&((*N)<3);k--)
  {
      i=7;
      j=7;
      count=0;
      direct=0;
      direct_num=1;

      while(i<15&&j<15)
      {

   if(map[i][j]==0){
       map[i][j]=mode;
       if(PriorityGet(i,j,k)==1)
       {
    num[(*N)][0]=i;
    num[(*N)][1]=j;
    num[(*N)][2]=k;
    map[i][j]=0;
    (*N)++;
   if((*N)>2) break;
       }/*end if(priorityGet(i,j,15)*/
       map[i][j]=0;
   }/*end if map[i][j]=0*/

   count++;
   if(count>direct_num)
   {
       direct=(direct+1)%4;
       if(direct%2==0)
       {
     direct_num++;
       }/*end if direct%2==0*/

       count=1;
   }  /*end if count>direct_num*/

   i+=b[direct][0];
   j+=b[direct][1];
      }/*end while*/
   }/*end k*/
}


int PriorityGet(int C_I,int C_J,int num)
{
  switch(num)
  {
      case 17:
    if(LinkF5(C_I,C_J))
    {
       return 1;
    }
    break;
      case 16:
    if(LinkF4(C_I,C_J))
    {
        return 1;
    }
    break;
      case 15:
    if(LinkSS4(C_I,C_J>2))
    {
        return 1;
    }
    break;
      case 14:
    if(LinkS5(C_I,C_J))
    {
        return 1;
    }
    break;
      case 13:
    if(LinkSS4(C_I,C_J)&&LinkS4(C_I,C_J))
    {
  return 1;
    }
    break;
      case 12:
    if(LinkSS4(C_I,C_J)&&LinkF3(C_I,C_J))
    {
        return 1;
    }
    break;
      case 11:
    if(LinkS4(C_I,C_J)&&LinkF3(C_I,C_J))
    {
        return 1;
    }
    break;
      case 10:
    if(LinkF3(C_I,C_J)>2)
    {
        return 1;
    }
    break;
      case 9:
    if(LinkS4(C_I,C_J))
    {
        return 1;
    }
    break;
      case 8:
    if(LinkSS4(C_I,C_J))
    {
        return 1;
    }
    break;
      case 7:
    if(LinkF3(C_I,C_J))
    {
        return 1;
    }
    break;
      case 6:
    if(LinkSS3(C_I,C_J)>1)
    {
        return 1;
    }
    break;
      case 5:
    if(LinkS3(C_I,C_J)>1)
    {
        return 1;
    }
    break;
      case 4:
    if(LinkF2(C_I,C_J)>2)
    {
        return 1;
    }
    break;
      case 3:
    if(LinkF2(C_I,C_J))
    {
  return 1;
    }
    break;
      case 2:
    if(LinkSS3(C_I,C_J))
    {
        return 1;
    }
    break;
      case 1:
    if(LinkF1(C_I,C_J))
    {
        return 1;
    }
    break;
   }/*end while*/
   return 0;
}


int LinkSS4(int C_I,int C_J)
{
  int a[8][6]={{2,1,1,1,1,0},{2,1,0,1,1,1},{2,1,1,0,1,1},
        {2,1,1,1,0,1},{1,2,2,2,2,0},{1,2,0,2,2,2},
        {1,2,2,0,2,2},{1,2,2,2,0,2}};
  int b[4][2]={{-1,0},{0,-1},{-1,-1},{-1,1}};
  int i,j,k,M_I,M_J,count=0,A_J,Old_C_I,Old_C_J,num;

  num=map[C_I][C_J];
  Old_C_I=C_I;
  Old_C_J=C_J;

  for(i=0;i<8;i++)
  {/*The chess type*/
      if(num!=a[i][1])continue;
      for(j=0;j<2;j++)
      { /*the program sreash direct*/
   for(k=0;k<4;k++){
       C_I=Old_C_I;
       C_J=Old_C_J;
       while(C_I>=0&&C_J<15&&C_I<15&&C_J>=0)
       {
     M_I=C_I;
     M_J=C_J;
     if(j%2) A_J=5;
     else A_J=0;
     while(M_I>=0&&M_I<15&&M_J>=0&&M_J<15
   &&(map[M_I][M_J]==a[i][A_J])&&(A_J>=0&&A_J<6))
     {
         M_I-=b[k][0];
         M_J-=b[k][1];
         if(j%2)A_J--;
         else A_J++;
     }/*en while*/
     if((A_J==6&&j==0)||(A_J<0&&j==1))count++;
     C_I+=b[k][0];
     C_J+=b[k][1];
        } /*end while*/
   }/*end for i*/
       } /*end for j*/
  }/*end for k*/

  for(i=0;i<8;i++)
  {
      if(num!=a[i][1])continue;
      for(j=0;j<2;j++)
      {
   for(k=0;k<4;k++)
   {
       C_I=Old_C_I;
       C_J=Old_C_J;

       while(C_I>=0&&C_J<15&&C_I<15&&C_J>=0)
       {
    M_I=C_I;
    M_J=C_J;
    if(j%2) A_J=5;
    else A_J=1;

    while(M_I>=0&&M_I<15&&M_J>=0&&M_J<15
        &&(map[M_I][M_J]==a[i][A_J])
        &&(A_J>0&&A_J<6))
    {
        M_I-=b[k][0];
        M_J-=b[k][1];
        if(j%2)A_J--;
        else A_J++;
    }/*en while*/

   if((A_J==6&&j==0)||(A_J==0&&j==1))
   {
       if((C_J==0&&M_J!=0)||(C_I==0&&M_I!=0)
    ||(C_J==14&&M_J<14))count++;
       if((M_I==15&&C_I<14)||(M_J==15&&C_J<14))count++;

    }

    C_I+=b[k][0];
    C_J+=b[k][1];

      } /*end while*/

  }/*end for i*/
     } /*end for j*/
  }/*end for k*/

  return count;
}


int LinkSS3(int C_I,int C_J){
  int a[12][6]={{2,1,1,1,0,0},{2,1,1,0,1,0},{2,1,1,0,0,1},
        {2,1,0,1,1,0},{2,1,0,1,0,1},{2,1,0,0,1,1},
        {1,2,2,2,0,0},{1,2,2,0,2,0},{1,2,2,0,0,2},
        {1,2,0,2,2,0},{1,2,0,2,0,2},{1,2,0,0,2,2}};
  int b[4][2]={{-1,0},{0,-1},{-1,-1},{-1,1}};
  int i,j,k,M_I,M_J,count=0,A_J,Old_C_I,Old_C_J,num;

  num=map[C_I][C_J];
  Old_C_I=C_I;
  Old_C_J=C_J;


  for(i=0;i<12;i++)
  {
      if(num!=a[i][1])continue;
      for(j=0;j<2;j++)
      {
   for(k=0;k<4;k++)
   {
       C_I=Old_C_I;
       C_J=Old_C_J;
       while(C_I>=0&&C_J<15&&C_I<15&&C_J>=0)
       {
    M_I=C_I;
    M_J=C_J;
    if(j%2) A_J=5;
    else A_J=0;
    while(M_I>=0&&M_I<15&&M_J>=0&&M_J<15
        &&(map[M_I][M_J]==a[i][A_J])
        &&(A_J>=0&&A_J<6))
    {
        M_I-=b[k][0];
        M_J-=b[k][1];
        if(j%2)A_J--;
        else A_J++;
   }/*en while*/
   if((A_J==6&&j==0)||(A_J<0&&j==1))count++;
   C_I+=b[k][0];
   C_J+=b[k][1];
     } /*end while*/
  } /*end for i*/
      } /*end for j*/
  }/*end for k*/

  for(i=0;i<12;i++)
  {
      if(num!=a[i][1])continue;
      for(j=0;j<2;j++)
      {
   for(k=0;k<4;k++)
   {
       C_I=Old_C_I;
       C_J=Old_C_J;
       while(C_I>=0&&C_J<15&&C_I<15&&C_J>=0)
       {
    M_I=C_I;
    M_J=C_J;
    if(j%2) A_J=5;
    else A_J=1;
    while(M_I>=0&&M_I<15&&M_J>=0&&M_J<15
        &&(map[M_I][M_J]==a[i][A_J])
        &&(A_J>0&&A_J<6))
    {
        M_I-=b[k][0];
        M_J-=b[k][1];
        if(j%2)A_J--;
        else A_J++;
    }/*en while*/
   if((A_J==6&&j==0)||(A_J==0&&j==1))
   {
       if((C_J==0&&M_J!=0)||(C_I==0&&M_I!=0)
   ||(C_J==14&&M_J<14))count++;
       if((M_I==15&&C_I<14)||(M_J==15&&C_J<14))count++;

   }
   C_I+=b[k][0];
   C_J+=b[k][1];
     } /*end while*/
  }/*end for i*/
     } /*end for j*/
  }/*end for k*/

  return count;

}


int LinkS5(int C_I,int C_J)
{
  int a[2][7]={{1,0,1,1,1,0,1},{2,0,2,2,2,0,2}};
  int b[4][2]={{-1,0},{0,-1},{-1,-1},{-1,1}};
  int i,j,k,M_I,M_J,count=0,A_J,Old_C_I,Old_C_J,num;
  num=map[C_I][C_J];
  Old_C_I=C_I;
  Old_C_J=C_J;
  for(i=0;i<2;i++)
  {
      if(num!=a[i][0])continue;
      for(j=0;j<2;j++)
      {
   for(k=0;k<4;k++){
       C_I=Old_C_I;
       C_J=Old_C_J;
       while(C_I>=0&&C_J<15&&C_I<15&&C_J>=0
       &&(map[C_I][C_J]==0||map[C_I][C_J]==num))
       {
     M_I=C_I;
     M_J=C_J;
     if(j%2) A_J=6;
     else A_J=0;
     while(M_I>=0&&M_I<15&&M_J>=0&&M_J<15
     &&(map[M_I][M_J]==a[i][A_J])&&(A_J>=0&&A_J<7))
     {
   M_I-=b[k][0];
   M_J-=b[k][1];
   if(j%2)A_J--;
   else A_J++;
      }/*en while*/
     if((A_J==7&&j==0)||(A_J<0&&j==1))count++;
     C_I+=b[k][0];
     C_J+=b[k][1];
       } /*end while*/
   }/*end for i*/
      } /*end for j*/
  }/*end for k*/

  return count;
}

int LinkS4(int C_I,int C_J)
{
  int a[2][7]={{1,0,1,0,1,0,1},{2,0,2,0,2,0,2}};
  int b[4][2]={{-1,0},{0,-1},{-1,-1},{-1,1}};
  int i,j,k,M_I,M_J,count=0,A_J,Old_C_I,Old_C_J,num;
  num=map[C_I][C_J];
  Old_C_I=C_I;
  Old_C_J=C_J;
  for(i=0;i<2;i++)
  {
      if(num!=a[i][0])continue;
      for(j=0;j<2;j++)
      {
   for(k=0;k<4;k++){
       C_I=Old_C_I;
       C_J=Old_C_J;
       while(C_I>=0&&C_J<15&&C_I<15&&C_J>=0
       &&(map[C_I][C_J]==0||map[C_I][C_J]==num))
       {
     M_I=C_I;
     M_J=C_J;
     if(j%2) A_J=6;
     else A_J=0;
     while(M_I>=0&&M_I<15&&M_J>=0&&M_J<15
     &&(map[M_I][M_J]==a[i][A_J])&&(A_J>=0&&A_J<7))
     {
   M_I-=b[k][0];
   M_J-=b[k][1];
   if(j%2)A_J--;
   else A_J++;
      }/*en while*/
     if((A_J==7&&j==0)||(A_J<0&&j==1))count++;
     C_I+=b[k][0];
     C_J+=b[k][1];
       } /*end while*/
   }/*end for i*/
      } /*end for j*/
  }/*end for k*/

  return count;
}


int LinkS3(int C_I,int C_J){
  int a[2][7]={{1,0,1,0,1,0,0},{2,0,2,0,2,0,0}};
  int b[4][2]={{-1,0},{0,-1},{-1,-1},{-1,1}};
  int i,j,k,M_I,M_J,count=0,A_J,Old_C_I,Old_C_J,num;
  num=map[C_I][C_J];
  Old_C_I=C_I;
  Old_C_J=C_J;
  for(i=0;i<2;i++)
  {
      if(num!=a[i][0])continue;
      for(j=0;j<2;j++)
      {
   for(k=0;k<4;k++)
   {
        C_I=Old_C_I;
        C_J=Old_C_J;
        while(C_I>=0&&C_J<15&&C_I<15&&C_J>=0
        &&(map[C_I][C_J]==0||map[C_I][C_J]==num))
        {
      M_I=C_I;
      M_J=C_J;
      if(j%2) A_J=6;
      else A_J=0;
      while(M_I>=0&&M_I<15&&M_J>=0&&M_J<15
      &&(map[M_I][M_J]==a[i][A_J])&&(A_J>=0&&A_J<7))
      {
     M_I-=b[k][0];
     M_J-=b[k][1];
     if(j%2)A_J--;
     else A_J++;
       }/*en while*/
      if((A_J==7&&j==0)||(A_J<0&&j==1))count++;
      C_I+=b[k][0];
      C_J+=b[k][1];
        } /*end while*/
    }/*end for i*/
       } /*end for j*/
  }/*end for k*/

  return count;
}


int LinkF5(int C_I,int C_J)
{
  int a[2][6]={{1,1,1,1,1},{2,2,2,2,2}};
  int b[4][2]={{-1,0},{0,-1},{-1,-1},{-1,1}};
  int i,j,k,M_I,M_J,count=0,A_J,Old_C_I,Old_C_J,num;

  num=map[C_I][C_J];
  Old_C_I=C_I;
  Old_C_J=C_J;

  for(i=0;i<2;i++)
  {
      if(num!=a[i][1])continue;
      for(j=0;j<2;j++)
      {
   for(k=0;k<4;k++)
   {
       C_I=Old_C_I;
       C_J=Old_C_J;
       while(C_I>=0&&C_J<15&&C_I<15&&C_J>=0
      &&(map[C_I][C_J]==0||map[C_I][C_J]==num))
       {
     M_I=C_I;
     M_J=C_J;
     if(j%2) A_J=4;
     else A_J=0;
     while(M_I>=0&&M_I<15&&M_J>=0&&M_J<15
    &&(map[M_I][M_J]==a[i][A_J])&&(A_J>=0&&A_J<6))
     {
    M_I-=b[k][0];
    M_J-=b[k][1];
    if(j%2)A_J--;
    else A_J++;
      }/*en while*/
      if((A_J==5&&j==0)||(A_J<0&&j==1))count++;
      C_I+=b[k][0];
      C_J+=b[k][1];
        } /*end while*/
    }/*end for i*/
       } /*end for j*/
   }/*end for k*/
   return count;
}

int LinkF4(int C_I,int C_J)
{
  int a[2][6]={{0,1,1,1,1,0},{0,2,2,2,2,0}};
  int b[4][2]={{-1,0},{0,-1},{-1,-1},{-1,1}};
  int i,j,k,M_I,M_J,count=0,A_J,Old_C_I,Old_C_J,num;

  num=map[C_I][C_J];
  Old_C_I=C_I;
  Old_C_J=C_J;

  for(i=0;i<2;i++)
  {
      if(num!=a[i][1])continue;
      for(j=0;j<2;j++)
      {
   for(k=0;k<4;k++)
   {
       C_I=Old_C_I;
       C_J=Old_C_J;
       while(C_I>=0&&C_J<15&&C_I<15&&C_J>=0
      &&(map[C_I][C_J]==0||map[C_I][C_J]==num))
       {
     M_I=C_I;
     M_J=C_J;
     if(j%2) A_J=5;
     else A_J=0;
     while(M_I>=0&&M_I<15&&M_J>=0&&M_J<15
    &&(map[M_I][M_J]==a[i][A_J])&&(A_J>=0&&A_J<6))
     {
    M_I-=b[k][0];
    M_J-=b[k][1];
    if(j%2)A_J--;
    else A_J++;
      }/*en while*/
      if((A_J==6&&j==0)||(A_J<0&&j==1))count++;
      C_I+=b[k][0];
      C_J+=b[k][1];
        } /*end while*/
    }/*end for i*/
       } /*end for j*/
   }/*end for k*/
   return count;
}


int LinkF3(int C_I,int C_J)
{
  int a[4][6]={{0,1,1,0,1,0},{0,1,1,1,0,0},{0,2,2,0,2,0},{0,2,2,2,0,0}};
  int b[4][2]={{-1,0},{0,-1},{-1,-1},{-1,1}};
  int i,j,k,M_I,M_J,count=0,A_J,Old_C_I,Old_C_J,num;

  num=map[C_I][C_J];
  Old_C_I=C_I;
  Old_C_J=C_J;

  for(i=0;i<4;i++)
  {
      if(num!=a[i][1])continue;
      for(j=0;j<2;j++)
      {
   for(k=0;k<4;k++)
   {
       C_I=Old_C_I;
       C_J=Old_C_J;
       while(C_I>=0&&C_J<15&&C_I<15&&C_J>=0
      &&(map[C_I][C_J]==0||map[C_I][C_J]==num))
       {
     M_I=C_I;
     M_J=C_J;
     if(j%2) A_J=5;
     else A_J=0;
     while(M_I>=0&&M_I<15&&M_J>=0&&M_J<15
    &&(map[M_I][M_J]==a[i][A_J])&&(A_J>=0&&A_J<6))
     {
    M_I-=b[k][0];
    M_J-=b[k][1];
    if(j%2)A_J--;
    else A_J++;
      }/*en while*/
      if((A_J==6&&j==0)||(A_J<0&&j==1))count++;
      C_I+=b[k][0];
      C_J+=b[k][1];
        } /*end while*/
    }/*end for i*/
       } /*end for j*/
  }/*end for k*/
  return count;
}


int LinkF2(int C_I,int C_J){
  int a[6][6]={{0,1,1,0,0,0},{0,1,0,1,0,0},{0,1,0,0,1,0},
        {0,2,2,0,0,0},{0,2,0,2,0,0},{0,2,0,0,2,0}};
  int b[4][2]={{-1,0},{0,-1},{-1,-1},{-1,1}};
  int i,j,k,M_I,M_J,count=0,A_J,Old_C_I,Old_C_J,num;
  num=map[C_I][C_J];
  Old_C_I=C_I;
  Old_C_J=C_J;
  for(i=0;i<6;i++)
  {
      if(num!=a[i][1])continue;
      for(j=0;j<2;j++)
      {
   for(k=0;k<4;k++)
   {
       C_I=Old_C_I;
       C_J=Old_C_J;
       while(C_I>=0&&C_J<15&&C_I<15&&C_J>=0
      &&(map[C_I][C_J]==0||map[C_I][C_J]==num))
       {
    M_I=C_I;
    M_J=C_J;
    if(j%2) A_J=5;
    else A_J=0;
    while(M_I>=0&&M_I<15&&M_J>=0&&M_J<15
         &&(map[M_I][M_J]==a[i][A_J])&&(A_J>=0&&A_J<6))
    {
         M_I-=b[k][0];
         M_J-=b[k][1];
         if(j%2)A_J--;
         else A_J++;
    }/*en while*/
    if((A_J==6&&j==0)||(A_J<0&&j==1))count++;
    C_I+=b[k][0];
    C_J+=b[k][1];
      } /*end while*/
  }/*end for i*/
     } /*end for j*/
  }/*end for k*/
  return count;
}


int LinkF1(int C_I,int C_J)
{
   if(C_I<15&&C_I>=0&&C_J<15&&C_J>=0)return 1;
}


void DrawTextXY(int x1,int y1,int x2,int y2,char s[])
{
  setcolor(BLUE);
  setfillstyle(SOLID_FILL,BLUE);
  bar(x1,y1,x2,y2);
  setcolor(RED);
  outtextxy(x1+2,y1+2,s);
}


void DrawRegretChess(int Course_I,int Course_J)
{
  int x1,y1;
  setcolor(BLUE);
  x1=x0+20+Course_J*20;
  y1=y0+20+Course_I*20;
  setfillstyle(SOLID_FILL,BLUE);
  fillellipse(x1,y1,9,9);
}


void DrawCourse(int Course_I,int Course_J,int flag)
{
  int x1,y1,x2,y2;

  if(flag)setcolor(RED);
  else setcolor(BLUE);

  x1=x0+10+Course_J*20+2;
  y2=y1=y0+10+Course_I*20+2;
  x2=x1+5;
  line(x1,y1,x2,y2);
  x2=x1;
  y2=y1+5;
  line(x1,y1,x2,y2);

  y2=y1=y1+16;
  x2=x1+5;
  line(x1,y1,x2,y2);
  x2=x1;
  y2=y1-5;
  line(x1,y1,x2,y2);

  x1=x1+16;
  y1=y1-16;
  y2=y1;
  x2=x1-5;
  line(x1,y1,x2,y2);
  x2=x1;
  y2=y1+5;
  line(x1,y1,x2,y2);

  y1=y1+16;
  y2=y1-5;
  x2=x1;
  line(x1,y1,x2,y2);
  y2=y1;
  x2=x1-5;
  line(x1,y1,x2,y2);
}

void DrawChess()
{
   int i,j,x1,y1;
   setcolor(BLUE);
   for(i=0;i<15;i++)
   {
       for(j=0;j<15;j++)
       {
    x1=x0+20+j*20;
    y1=y0+20+i*20;
    if(map[i][j]==1)
    {
  setfillstyle(SOLID_FILL,DARKGRAY);
    }
    if(map[i][j]==2)
    {
   setfillstyle(SOLID_FILL,WHITE);
    }
   if(map[i][j])fillellipse(x1,y1,8,8);
       }
   }

}

void DrawMap()
{
   int i,j,x1,y1,x2,y2;
   char str[20],s[2];

   setcolor(GREEN);
   rectangle(x0,y0,x0+480,y0+320);
   rectangle(x0+1,y0+1,x0+479,y0+319);
   rectangle(x0+9,y0+9,x0+311,y0+311);

   s[0]=0x18;
   s[1]='/0';
   strcpy(str,"");
   strcat(str,s);
   strcat(str," UP");
   DrawTextXY(x0+385,y0+30,x0+450,y0+50,str);

   strcpy(str,"");
   strcat(str,"LEFT ");
   s[0]=0x1b;
   strcat(str,s);
   strcat(str,"  ");
   s[0]=0x1a;
   strcat(str," ");
   strcat(str,s);
   strcat(str," RIGHT");
   DrawTextXY(x0+330,y0+50,x0+450,y0+70,str);

   strcpy(str,"");
   s[0]=0x19;
   strcat(str,"DOWN ");
   strcat(str,s);
   DrawTextXY(x0+345,y0+70,x0+450,y0+90,str);

   strcpy(str,"");
   strcat(str,"SPACE  Play Chess");
   DrawTextXY(x0+325,y0+90,x0+478,y0+110,str);

   strcpy(str,"");
   strcat(str,"F2 Save Chess Map");
   DrawTextXY(x0+325,y0+110,x0+478,y0+130,str);

   strcpy(str,"");
   strcat(str,"F3 Load Chess Map");
   DrawTextXY(x0+325,y0+130,x0+478,y0+150,str);

   strcpy(str,"");
   strcat(str,"F4 Regret Play");
   DrawTextXY(x0+325,y0+150,x0+478,y0+170,str);

   setcolor(GREEN);
   line(x0+315,y0+175,x0+475,y0+175);


   for(i=0;i<16;i++)
   {
       x2=x1=x0+10+i*20;
       y1=y0+10;
       y2=y0+310;
       line(x1,y1,x2,y2);
    }
   for(i=0;i<16;i++)
   {
       x1=x0+10;
       x2=x0+310;
       y2=y1=y0+10+i*20;
       line(x1,y1,x2,y2);
   }
}


void InitGraph()
{
  int driver,mode,x,y;
  driver=DETECT;

  registerbgidriver(EGAVGA_driver);
  initgraph(&driver,&mode,"G://program//tc");
  setbkcolor(BLUE);
  x=getmaxx();
  y=getmaxy();
  if(x<x0+480||y<y0+320)
  {
    outtextxy(10,10,"The scree is too small");
    getch();
    CloseGraph();
  }
}

void CloseGraph()http://download1.csdn.net/down3/20070602/02133316155.rar
{
   cleardevice();
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   outtextxy(150,100,"|* Thank you for you use this program          *|");
   outtextxy(150,130,"|* This program was made in 2005               *|");
   outtextxy(150,160,"|* This program was made in nanchang university*|");
   sleep(1);
   closegraph();
   exit(0);

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值