home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 2.ddi / CHESS.ZIP / BOARD.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  5.7 KB  |  245 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. #include <windows.h>
  4. #include "wcdefs.h"
  5. #include "externs.h"
  6.  
  7. /*
  8.  *  globals
  9.  */
  10.  
  11. int OfficerNo[2], PawnNo[2];
  12.  
  13.  
  14. /*
  15.  *  Clear the board and initialize the board-module
  16.  */
  17.  
  18. void ClearBoard()
  19. {
  20.    SQUARETYPE Square;
  21.     for (Square = 0; Square <= 0x77; Square++)
  22.     {
  23.         Board[Square].piece = empty;
  24.         Board[Square].color = white;
  25.     }
  26. }
  27.  
  28.  
  29. /*
  30.  *  Clears indexes in board and piecetab
  31.  */
  32.  
  33. void ClearIndex(void)
  34. {
  35.     SQUARETYPE square;
  36.     COLORTYPE col;
  37.     INDEXTYPE index;
  38.  
  39.     for (square = 0; square <= 0x77; square++)
  40.         Board[square].index = 16;
  41.     for (col = white; col <= black; ((int)col)++)
  42.         for (index = 0; index < 16; index++)
  43.             PieceTab[col][index].ipiece = empty;
  44.     OfficerNo[white] = PawnNo[white] = -1;
  45.     OfficerNo[black] = PawnNo[black] = -1;
  46. }
  47.  
  48.  
  49. /*
  50.  *  Calcualates Piece table from scratch
  51.  */
  52.  
  53. void CalcPieceTab(void)
  54. {
  55.     SQUARETYPE square;
  56.     PIECETYPE piece1;
  57.  
  58.     ClearIndex();
  59.  
  60.     for (piece1 = king; piece1 <= pawn; ((int)piece1)++)
  61.      {
  62.         if (piece1 == pawn)
  63.         {
  64.             OfficerNo[white] = PawnNo[white];
  65.             OfficerNo[black] = PawnNo[black];
  66.         }
  67.         square = 0;
  68.         do
  69.         {
  70.             if (Board[square].piece == piece1)
  71.             {
  72.                 PawnNo[Board[square].color]++;
  73.                 PieceTab[Board[square].color][PawnNo[Board[square].color]].ipiece
  74.                   = piece1;
  75.                 PieceTab[Board[square].color][PawnNo[Board[square].color]].isquare
  76.                   = square;
  77.                 Board[square].index = PawnNo[Board[square].color];
  78.             }
  79.             square ^=  0x77;
  80.             if (!(square & 4))
  81.             {
  82.                 if (square >= 0x70)
  83.                     square = (square + 0x11) & 0x73;
  84.                 else
  85.                     square += 0x10;
  86.             }
  87.         } while (square);
  88.     }
  89. }
  90.  
  91.  
  92. /*
  93.  *  move a piece to a new location on the board
  94.  */
  95.  
  96. inline void MovePiece(SQUARETYPE new1, SQUARETYPE old)
  97. {
  98.     BOARDTYPE b;
  99.     b = Board[new1];
  100.     Board[new1] = Board[old];
  101.     Board[old] = b;
  102.     PieceTab[Board[new1].color][Board[new1].index].isquare = new1;
  103. }
  104.  
  105.  
  106. /*
  107.  *  Calculate the squares for the rook move in castling
  108.  */
  109.  
  110. void GenCastSquare(SQUARETYPE new1, SQUARETYPE *castsquare,
  111.                     SQUARETYPE *cornersquare)
  112. {
  113.     if ((new1 & 7) >= 4)          /* short castle */
  114.     {
  115.         *castsquare = new1 - 1;
  116.         *cornersquare = new1 + 1;
  117.     }
  118.     else                           /* long castle */
  119.     {
  120.         *castsquare = new1 + 1;
  121.         *cornersquare = new1 - 2;
  122.     }
  123. }
  124.  
  125.  
  126. /*
  127.  *  This function used in captures.  insquare must not be empty.
  128.  */
  129.  
  130. inline void DeletePiece(SQUARETYPE insquare)
  131. {
  132.     Board[insquare].piece = empty;
  133.     PieceTab[Board[insquare].color][Board[insquare].index].ipiece = empty;
  134. }
  135.  
  136.  
  137. /*
  138.  *  Take back captures
  139.  */
  140.  
  141. inline void InsertPTabPiece(PIECETYPE inpiece, COLORTYPE incolor,
  142.    SQUARETYPE insquare)
  143. {
  144.     Board[insquare].piece = PieceTab[incolor][Board[insquare].index].ipiece
  145.             = inpiece;
  146.     Board[insquare].color = incolor;
  147.     PieceTab[incolor][Board[insquare].index].isquare = insquare;
  148. }
  149.  
  150.  
  151. /*
  152.  *  Used for pawn promotion
  153.  */
  154.  
  155. inline void ChangeType(PIECETYPE newtype, SQUARETYPE insquare)
  156. {
  157.     Board[insquare].piece
  158.         = PieceTab[Board[insquare].color][Board[insquare].index].ipiece
  159.             = newtype;
  160.     if (OfficerNo[Board[insquare].color] < Board[insquare].index)
  161.         OfficerNo[Board[insquare].color] = Board[insquare].index;
  162. }
  163.  
  164.  
  165.  
  166. /*
  167.  *  Perform or take back move (takes back if resetmove is true),
  168.  *  and perform the updating of Board and PieceTab.  Player must
  169.  *  contain the color of the moving player, Opponent the color of the
  170.  *  Opponent.
  171.  *
  172.  *  MovePiece, DeletePiece, InsertPTabPiece and ChangeType are used to update
  173.  *  the Board module.
  174.  */
  175.  
  176. void Perform(MOVETYPE *move, BOOL resetmove)
  177. {
  178.     SQUARETYPE /*new1*/ castsquare, cornersquare, epsquare;
  179.  
  180.     if (resetmove)
  181.     {
  182.         MovePiece(move->old, move->new1);
  183.         if (move->content != empty)
  184.             InsertPTabPiece(move->content, Opponent, move->new1);
  185.     }
  186.     else         
  187.     {
  188.         if (move->content != empty)
  189.             DeletePiece(move->new1);
  190.         MovePiece(move->new1, move->old);
  191.     }
  192.  
  193.     if (move->spe)
  194.     {
  195.         if (move->movpiece == king)
  196.         {
  197.             GenCastSquare(move->new1, &castsquare, &cornersquare);
  198.             if (resetmove)
  199.             {
  200.                 MovePiece(cornersquare, castsquare);
  201.             }
  202.             else
  203.             {
  204.                 MovePiece(castsquare, cornersquare);
  205.             }
  206.         }
  207.         else
  208.         {
  209.             if (move->movpiece == pawn)
  210.             {
  211.                 epsquare = (move->new1 & 7) + (move->old & 0x70); /* E.p. capture */
  212.                 if (resetmove)
  213.                     InsertPTabPiece(pawn, Opponent, epsquare);
  214.                 else
  215.                     DeletePiece(epsquare);
  216.             }
  217.             else
  218.             {
  219.                 if (resetmove)
  220.                     ChangeType(pawn, move->old);
  221.                 else
  222.                     ChangeType(move->movpiece,move->new1);
  223.             }
  224.         }
  225.     }
  226. }
  227.  
  228.  
  229. /*
  230.  * Compare two moves
  231.  */
  232.  
  233. BOOL EqMove(MOVETYPE *a, MOVETYPE *b)
  234. {
  235.     if (a->movpiece == b->movpiece)
  236.         if (a->new1 == b->new1)
  237.             if (a->old == b->old)
  238.                 if (a->content == b->content)
  239.                     if (a->spe == b->spe)
  240.                         return TRUE;
  241.     return FALSE;
  242. }
  243.  
  244.  
  245.