⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 movegener.cpp

📁 《C++Builder程序设计范例--中国象棋》配书盘自述文件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "CDefines.h"
#include "Global.h"

/*
 *  全局变量
 */

static ATTACKTABTYPE attack[308];
ATTACKTABTYPE *AttackTable = &attack[154];
SETOFPIECE BitTab[8] = {0, 1, 2, 4, 8, 0x10, 0x20, 0x40};
int DirectionTab[4] = { 1, -1, 0x10, -0x10};
int AssistDir[4] = {0x0F, -0x0F, 0x11, -0x11};
int BishopDir[4] = {0x1E, -0x1E, 0x22, -0x22};
int KnightDir[8] = {0x0E, -0x0E, 0x12, -0x12, 0x1F, -0x1F, 0x21, -0x21};
MOVETYPE Next;
int BufCount, BufPnt;
MOVETYPE Buffer[81];

extern MOVETYPE *MoveTable;

/*
计算攻击表
对于每个棋子通过合法步数所能到达的位置,不管是对方的棋子还是自己的棋子,
还是没有棋子,则所能到的位置为受攻击的对象。
马走日字,此时不考虑躄脚 ;象走田字,不考虑象心有无棋子;炮纵横
任意走数格,不管有无炮架;士每次斜走一格,王或纵或横走一格,此时不一定要在九宫;
车纵横任意走数格,不管有无棋子挡住;
*/
void CalcAttackTable(void)
{
    DIRTYPE dir;
    int sq;
    unsigned char i;

    for (sq = -0x99; sq <= 0x99; sq++)
    {
	AttackTable[sq].pieceset = 0;
        AttackTable[sq].direction = 0;
    }

   for (dir = 3; dir >=0; dir--)
    {
	for (i = 1; i < 10; i++)
        {
	    AttackTable[DirectionTab[dir]*i].pieceset = SETOFPIECE(BitTab[rook] + BitTab[gunner] +
						BitTab[king]) ;
            AttackTable[DirectionTab[dir]*i].direction = DirectionTab[dir];
	}
    }

    for (dir = 3; dir >=0; dir--)
    {
	    AttackTable[AssistDir[dir]].pieceset = SETOFPIECE(BitTab[assist]);
	    AttackTable[AssistDir[dir]].direction = AssistDir[dir];
    }
    for (dir = 3; dir >=0; dir--)
    {
	    AttackTable[BishopDir[dir]].pieceset = SETOFPIECE(BitTab[bishop]);
	    AttackTable[BishopDir[dir]].direction = BishopDir[dir];
    }
    for (dir = 7; dir >=0; dir--)
    {
        AttackTable[KnightDir[dir]].pieceset = SETOFPIECE(BitTab[knight]);
        AttackTable[KnightDir[dir]].direction = KnightDir[dir];
    }

     for (dir = 3; dir >=0; dir--)
    {
	    AttackTable[DirectionTab[dir]].pieceset = SETOFPIECE(BitTab[pawn] + BitTab[rook] + BitTab[gunner] +
						BitTab[king]) ;
            AttackTable[DirectionTab[dir]].direction = DirectionTab[dir] ;
    }
}


/*
计算方格中的棋子是否攻击另外某个方格
 */

short PieceAttacks(PIECETYPE apiece, COLORTYPE acolor,
                     SQUARETYPE asquare, SQUARETYPE square)
{
    EDGESQUARETYPE sq;
    int x;
    int tmpx;
    int guncap = 0;  //记录棋子的移动路线上是否有别的棋子

    x = square - asquare;
  //   if (apiece == pawn)   /*  兵的攻击性。包括过河兵和未过河兵 */
    /*  如果别的棋子能移到该格子,则该棋格将受到攻击  */

    if (AttackTable[x].pieceset & BitTab[apiece])
    {
	if ( apiece == pawn )
	   if (( asquare  >= 0x30) && ( asquare  <= 0x98))
	 if (ChessBoard[asquare].color == red)
	   if   ( x ==0x10)
	  return 1;

	  if ( apiece == pawn )
	   if (( asquare  >= 0x50) && ( asquare  <= 0x98))
	 if (ChessBoard[asquare].color == red)
	   if   ((abs(x)==1) ||( x == 0x10))
	  return 1;

	  if ( apiece == pawn )
	   if (( asquare  <= 0x68) && ( asquare  <= 0x98))
	 if (ChessBoard[asquare].color == black)
	   if   ( x == -0x10)
	  return 1;

	  if ( apiece == pawn )
	   if (( asquare  <= 0x48) && ( asquare  <= 0x98))
	 if (ChessBoard[asquare].color == black)
	   if   ((abs(x)==1) || ( x == -0x10))
	  return 1;
       /*
       王只对位于九宫内的棋子构成威胁
       */
        if ( apiece == king )
	   if (( asquare  >=0) && ( asquare  <= 0x28) && ((asquare & 0xf) >=3)
			&& ((asquare & 0xf) <=5))
	 if (ChessBoard[asquare].color == red)
	  if (( square  >=0) && ( square  <= 0x28) && ((square % 16) >=3)
			&& ((square & 0xf) <=5))
	   if ((abs(x)==1) || (abs(x)==0x10))
				 return 1;
       	 if ( apiece == king )
	 if (( asquare  >= 0x70) && ( asquare  <= 0x98) && ((asquare % 16) >=3)
			&& ((asquare % 16) <=5))
	 if (ChessBoard[asquare].color == black)
	  if (( square  >= 0x70) && ( square  <= 0x98) && ((square % 16) >=3)
			&& ((square % 16) <=5))
	  if ((abs(x)==1) || (abs(x)==0x10))
				 return 1;

      if ((apiece == king ))
        if (ChessBoard[asquare].color == red)
	   if ((square  >= 0x70) && (square  <= 0x98))
	     if  (((square % 16) >=3) && ((square % 16) <=5))
	       if ((ChessBoard[square].piece == king ))
	         if ((ChessBoard[square].color == black ))
                   if (x==0x50)
        {
      /*  中间有棋子阻挡吗? 因为两个王不能照面 */
 	       sq = asquare;
	       guncap = 0;
            do
	    {
		sq += 0x10;
	  	if  (ChessBoard[sq].piece != empty )
		   guncap =guncap + 1;
	    } while ( sq  != square );
	    return (((guncap-1)==0) && (sq == square)) ;
      	    }

     	if ((apiece == king ))
	   if (ChessBoard[asquare].color == red)
		if ((square  >= 0x70) && (square  <= 0x98))
		  if  (((square % 16) >=3) && ((square % 16) <=5))
                     if ((ChessBoard[square].piece == king ))
	               if ((ChessBoard[square].color == black ))
                          if (x==0x60)
        {
      /*  中间有棋子阻挡吗? 因为两个王不能照面 */
  	       sq = asquare;
	       guncap = 0;
            do
	    {
		sq += 0x10;
	  	if  (ChessBoard[sq].piece != empty )
		   guncap =guncap + 1;
	    } while ( sq  != square );
	    return (((guncap-1)==0) && (sq == square)) ;
	    }

	if ((apiece == king ))
	  if (ChessBoard[asquare].color == red)
	        if ((square  >= 0x70) && (square  <= 0x98))
		 if  (((square % 16) >=3) && ((square % 16) <=5))
	           if ((ChessBoard[square].piece == king ))
	              if ((ChessBoard[square].color == black ))
                        if (x==0x70)
        {
        /*  中间有棋子阻挡吗? 因为两个王不能照面 */
	      sq = asquare;
	      guncap = 0;
            do
	    {
		sq += 0x10;
	       if  (ChessBoard[sq].piece != empty )
		   guncap =guncap + 1;
	    } while ( sq != square );
	    return (((guncap-1)==0) && (sq == square)) ;
	    }

	 if ((apiece == king ))
	   if (ChessBoard[asquare].color == red)
	       if ((square  >= 0x70) && (square  <= 0x98))
		 if  (((square % 16) >=3) && ((square % 16) <=5))
	           if ((ChessBoard[square].piece == king ))
	              if ((ChessBoard[square].color == black ))
                         if (x==0x80)
        {
       /*  中间有棋子阻挡吗? 因为两个王不能照面 */
	       sq = asquare;
	       guncap = 0;
            do
	    {
		sq += 0x10;
	        	if  (ChessBoard[sq].piece != empty )
		            guncap =guncap + 1;
	    } while ( sq  != square );
	    return (((guncap-1)==0) && (sq == square)) ;
	     }
	 if ((apiece == king ))
	   if (ChessBoard[asquare].color == red)
	       if ((square  >= 0x70) && (square  <= 0x98))
		 if  (((square % 16) >=3) && ((square % 16) <=5))
	            if ((ChessBoard[square].piece == king ))
	               if ((ChessBoard[square].color == black ))
                         if (x==0x90)
        {
       /*  中间有棋子阻挡吗? 因为两个王不能照面 */
	       sq = asquare;
	       guncap = 0;
            do
	    {
		sq += 0x10;
	           	if  (ChessBoard[sq].piece != empty )
		          guncap =guncap + 1;
	    } while ( sq  != square );
	    return (((guncap-1)==0) && (sq == square)) ;
       }
  	if (apiece == king )
	   if (ChessBoard[asquare].color == black)
		if ((square  >= 0) && (square <= 0x28))
		  if (((square % 16) >=3) && ((square % 16) <=5))
	             if (ChessBoard[square].piece == king )
	                if ((ChessBoard[square].color == red ))
                          if (x==-0x50)
        {
        /*  中间有棋子阻挡吗? 因为两个王不能照面 */
  	    sq = asquare;
	    guncap = 0;
           do
	    {
		sq -= 0x10;
		      if  (ChessBoard[sq].piece != empty )
		           guncap =guncap + 1;
	    }  while ( sq  != square );
	    return (((guncap-1)==0) && (sq == square)) ;
        }


      	if (apiece == king )
	   if (ChessBoard[asquare].color == black)
	       if ((square >= 0) && (square  <= 0x28))
		 if  (((square % 16) >=3) && ((square % 16) <=5))
                   if  (ChessBoard[square].piece == king )
	             if ((ChessBoard[square].color == red ))
                       if (x==-0x60)
        {
       /*  中间有棋子阻挡吗? 因为两个王不能照面 */
	    sq = asquare;
	    guncap = 0;
           do
	    {
		sq -= 0x10;
	  	      if  (ChessBoard[sq].piece != empty )
		         guncap =guncap + 1;
	    }  while ( sq  != square);
	    return (((guncap-1)==0) && (sq == square)) ;
        }

	if (apiece == king )
	   if (ChessBoard[asquare].color == black)
	        if ((square  >= 0) && (square  <= 0x28))
		 if (((square % 16) >=3) && ((square % 16) <=5))
           	   if (ChessBoard[square].piece == king )
	              if ((ChessBoard[square].color == red ))
                        if (x==-0x70)
        {
        /*  中间有棋子阻挡吗? 因为两个王不能照面 */
	     sq = asquare;
	     guncap = 0;
           do
	    {
		sq -= 0x10;
	        	if  (ChessBoard[sq].piece != empty )
		            guncap =guncap + 1;
	    }  while ( sq  != square );
	    return (((guncap-1)==0) && (sq == square)) ;
       }

	 if (apiece == king )
	   if (ChessBoard[asquare].color == black)
	       if ((square >= 0) && (square  <= 0x28))
		 if  (((square % 16) >=3) && ((square % 16) <=5))
	           if  (ChessBoard[square].piece == king )
	             if ((ChessBoard[square].color == red ))
                       if (x==-0x80)
        {
       /*  中间有棋子阻挡吗? 因为两个王不能照面 */
	    sq = asquare;
	    guncap = 0;
           do
	    {
		sq -= 0x10;
		if  (ChessBoard[sq].piece != empty )
		     guncap =guncap + 1;
	    }  while ( sq  != square );
	    return (((guncap-1)==0) && (sq == square)) ;
	}

        /*  中间有棋子阻挡吗? 因为两个王不能照面 */
	if (apiece == king )
	   if (ChessBoard[asquare].color == black)
	       if ((square >= 0) && (square <= 0x28))
		  if  (((square % 16) >=3) && ((square % 16) <=5))
		    if  (ChessBoard[square].piece == king )
	              if ((ChessBoard[square].color == red ))
                        if (x==-0x90)
        {
        /*  中间有棋子阻挡吗? 因为两个王不能照面 */
	    sq = asquare;
	    guncap = 0;
           do
	    {
		sq -= 0x10;
		      if  (ChessBoard[sq].piece != empty )
		            guncap =guncap + 1;
	    }  while ( sq  != square );
	    return (((guncap-1)==0) && (sq == square)) ;
        }
       /*
       士只对位于九宫内的棋子构成威胁
       */
       if  ( apiece == assist )
	    if (ChessBoard[asquare].color == red)
		 if (( square >=0) && ( square  <= 0x28) && ((square % 16) >=3)
			&& ((square % 16) <=5))
				 return 1;
	 if  ( apiece == assist )
	      if (ChessBoard[asquare].color == black)
		  if (( square >= 0x70) && ( square <= 0x98) && ((square % 16) >=3)
			&& ((square % 16) <=5))
				 return 1;
        /*
        如果田心没有棋子,象将对位于其对角的棋子构成威胁
        */
	if ( apiece == bishop )
           {
		if  (x== 0x1E)
		    tmpx = 0x0F;
		if  (x== -0x1E)
		    tmpx= -0x0F;
		if  (x== 0x22)
		    tmpx= 0x11;
		if  (x== -0x22)
		    tmpx= -0x11;
		if (( ChessBoard[square -  tmpx].piece == empty ))
		   if (ChessBoard[asquare].color == red)
		     if (( square >=0) && ( square  <= 0x48))
		       return 1;
		if  (x== 0x1E)
		    tmpx = 0x0F;
		if  (x== -0x1E)
		    tmpx= -0x0F;
		if  (x== 0x22)
		    tmpx= 0x11;
		if  (x== -0x22)
		    tmpx= -0x11;
		  if (( ChessBoard[square -  tmpx].piece == empty ))
		    if (ChessBoard[asquare].color == black)
		       if (( square  >= 0x50) && ( square  <= 0x98))
		          return 1;
           }

         /*
        如果马没有绊脚,将对位于其对角的棋子构成威胁
        */
        if ( apiece == knight )
	    {
	        if  (x == 0x0E)
		    tmpx= 0x0F;
		if  ( x== -0x0E)
		    tmpx= -0x0F;
		if  ( x== 0x12)
		    tmpx = 0x11;
		if  ( x== -0x12)
		    tmpx = -0x11;
		if  ( x== 0x1F)
		    tmpx = 0x0F;
		if  (x == -0x1F)
		    tmpx = -0x0F;
		if  ( x== 0x21)
		    tmpx = 0x11;
		if  ( x== -0x21)
		    tmpx = -0x11;
		if ( ChessBoard[square -  tmpx].piece == empty )
                  return 1;
            }


        /*
        如果直线上没有别的棋子,车将对位于其射程的棋子构成威胁
        */
       if (apiece == rook )
        {
        /*  直线上有没有别的棋子? */
	    sq = asquare;
            do
	    {
		sq += AttackTable[x].direction;
                   sq = sq;

	    } while (sq != square && ChessBoard[sq].piece == empty );
	    return ((sq == square) &&
		   ((asquare >> 4) == (square >> 4) || (asquare % 16) == (square % 16)));
	}

         /*
        如果直线上有炮架子,炮将对位于其射程的棋子构成威胁
        */
       if (apiece == gunner )
        {
          /*  直线上有没有别的棋子? */
	    sq = asquare;
	    guncap = 0;
            do
	    {
		sq += AttackTable[x].direction;
                   sq = sq;
		 if  (ChessBoard[sq].piece != empty )
		   guncap =guncap + 1;
	    } while (sq != square );
	    return (((sq == square) && ((guncap-1)==1)) &&
		    ((asquare >> 4) == (square >> 4) || (asquare % 16) == (square % 16))) ;
	}
    }
  	return 0;
}


/*
   计算acolor方是否攻击 square
 */

short Attacks(COLORTYPE acolor, SQUARETYPE square)
{
    INDEXTYPE i;

    for (i = PawnNumber[acolor]; i >= 0; i--)
	if ((PieceTable[acolor][i].ipiece != empty))
            if (PieceAttacks(PieceTable[acolor][i].ipiece, acolor,
		    PieceTable[acolor][i].isquare, square))
		return 1;
    return 0;
}

/*
检查移动过的棋子有没有被吃过。
 */

inline short RepeatMove(MOVETYPE *move)
{
    return (move->movpiece != empty && move->content == empty );
}

/****************************************************************************/

/*
 六十回合中没有吃子的话算为平局
 */

SIXTYTYPE SixtyMoveCnt(void)
{
    SIXTYTYPE cnt = 0;

    while (RepeatMove(&MoveTable[Depth - cnt]))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -