home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CHESS.PAK / BOARD.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.0 KB  |  204 lines

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