home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D11 / CHESSDLL.ZIP / LBOARD.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  6.7 KB  |  259 lines

  1. unit LBoard;
  2.  
  3. {internal board and move data and functions used by chessdll}
  4.  
  5. interface
  6.  
  7. uses GameRec;
  8.  
  9. function EqMove(var a,b : MoveType) : boolean;
  10. function Min(a,b : integer) : integer;
  11. function Max(a,b : integer) : integer;
  12. procedure InitBoard;
  13. procedure CalcPieceTab;
  14. procedure GenCastSquare(New1 : SquareType; var CastSquare,
  15.   CornerSquare : SquareType);
  16. procedure Perform(Move : MoveType; ResetMove : boolean);
  17. procedure MovePiece(New1,Old : SquareType);
  18. procedure DeletePiece(InSquare : SquareType);
  19. procedure InsertPiece(InPiece : PieceType; InColor : ColorType;
  20.   InSquare : SquareType);
  21. procedure ChangeType(NewType : PieceType; InSquare : SquareType);
  22.  
  23. const    { To make Perform's ResetMove parameter easier to read: }
  24.   DoIt = False;
  25.   UndoIt = True;
  26.  
  27. implementation
  28.  
  29. uses Strings;
  30.  
  31. function EqMove(var a,b : MoveType) : boolean;
  32. { Compares two moves }
  33. begin
  34.    EqMove := False;
  35.    if a.MovPiece = b.MovPiece then
  36.      if a.New1 = b.New1 then
  37.        if a.Old = b.Old then
  38.          if a.Content = b.Content then
  39.            if a.Spe = b.Spe then
  40.              EqMove := true;
  41. end; { EqMove }
  42.  
  43. function Min(a,b : integer) : integer;
  44. begin
  45.   if a < b then
  46.     Min := a
  47.   else
  48.     Min := b;
  49. end; { Min }
  50.  
  51. function Max(a,b : integer) : integer;
  52. begin
  53.   if a > b then
  54.     Max := a
  55.   else
  56.     Max := b;
  57. end; { Max }
  58.  
  59. procedure CalcPieceTab;
  60. { Calculates PieceTab from scratch }
  61. var   Square : SquareType;
  62.       Piece1 : PieceType;
  63.  
  64.   procedure ClearIndex;
  65.   { Clears indexes in Board and PieceTab }
  66.   var Square : SquareType;
  67.       Col :    ColorType;
  68.       Index :  IndexType;
  69.   begin
  70.      with CC do
  71.      begin
  72.        for Square := 0 to $77 do
  73.           Board[Square].Index := 16;
  74.        for Col := White to Black do
  75.          for Index := 0 to 15 do
  76.            PieceTab[Col,Index].IPiece := Empty;
  77.        PawnNo[White] := -1;
  78.        PawnNo[Black] := -1;
  79.        OfficerNo := PawnNo;
  80.      end;
  81.   end;
  82.  
  83. begin
  84.   ClearIndex;
  85.   for Piece1 := King to Pawn do    { Insert all the Pieces of the type }
  86.   with CC do
  87.   begin
  88.      if Piece1 = Pawn then
  89.         OfficerNo := PawnNo;      { Save Number of officers }
  90.      Square := 0;
  91.      repeat
  92.        with Board[Square] do
  93.          if Piece = Piece1 then
  94.          begin
  95.            PawnNo[Color] := PawnNo[Color] + 1;          { Count Pieces }
  96.            with PieceTab[Color,PawnNo[Color]] do        { Insert Piece }
  97.            begin
  98.              IPiece := Piece1;
  99.              ISquare := Square;
  100.              Index := PawnNo[Color];
  101.            end;
  102.          end;
  103.          Square := Square xor $77;     { Generate all squares from }
  104.          if (Square and 4) = 0 then    {  border to center }
  105.             if Square >= $70 then
  106.                Square := (Square + $11) and $73
  107.             else
  108.                Square := Square + $10;
  109.      until Square = 0;
  110.    end;
  111. end; { CalcPieceTab }
  112.  
  113. procedure GenCastSquare(New1 : SquareType;
  114.                         var CastSquare,
  115.                             CornerSquare : SquareType);
  116. { Calculates the squares for the Rook Move in a castling }
  117. begin
  118.    if (New1 and 7) >= 4 then     { Short }
  119.    begin
  120.       CastSquare := New1 - 1;
  121.       CornerSquare := New1 + 1;
  122.    end
  123.    else
  124.    begin                         { Long }
  125.      CastSquare := New1 + 1;
  126.      CornerSquare := New1 - 2;
  127.    end;
  128. end; { GenCastSquare }
  129.  
  130.  
  131. { Utility functions for Perform: }
  132.  
  133. procedure MovePiece(New1,Old : SquareType);
  134. { Is used to Move a Piece }
  135. var
  136.   B : BoardType;
  137. begin
  138.   with CC do
  139.   begin
  140.     B := Board[New1];
  141.     Board[New1] := Board[Old];
  142.     Board[Old] := B;
  143.     with Board[New1] do
  144.        PieceTab[Color,Index].ISquare := New1;
  145.   end;
  146. end; { MovePiece }
  147.  
  148. procedure DeletePiece(InSquare : SquareType);
  149. { Is used in captures. The Square must not be Empty }
  150. begin
  151.   with CC, Board[InSquare] do
  152.   begin
  153.     Piece := Empty;
  154.     PieceTab[Color,Index].IPiece := Empty;
  155.   end;
  156. end; { DeletePiece }
  157.  
  158. procedure InsertPiece(InPiece : PieceType;
  159.                       InColor : ColorType;
  160.                      InSquare : SquareType);
  161. { Is used to take Back captures }
  162. begin
  163.    with CC, Board[InSquare],PieceTab[InColor,Index] do
  164.    begin
  165.       Piece := InPiece;
  166.       Color := InColor;
  167.       IPiece := InPiece;
  168.       ISquare := InSquare;
  169.    end;
  170. end; { InsertPiece }
  171.  
  172. procedure ChangeType(NewType : PieceType; InSquare : SquareType);
  173. { Is used for Pawn promotion }
  174. begin
  175.    with CC, Board[InSquare] do
  176.    begin
  177.       Piece := NewType;
  178.       PieceTab[Color,Index].IPiece := NewType;
  179.       if OfficerNo[Color] < Index then
  180.          OfficerNo[Color] := Index;
  181.    end;
  182. end; { ChangeType }
  183.  
  184.  
  185. procedure InitBoard;
  186. { Clears the Board and initializes the Board-module }
  187. var
  188.   i : 0..7;
  189. begin
  190.   with CC do
  191.   begin
  192.     FillChar(Board, sizeof(Board), 0);
  193.     for i := 0 to 7 do                   { Setup Start position }
  194.     begin
  195.       InsertPiece(Pieces[i],White, i);
  196.       InsertPiece(Pawn,White, i + $10);
  197.       InsertPiece(Pawn,Black, i + $60);
  198.       InsertPiece(Pieces[i],Black, i + $70);
  199.     end;
  200.   end;
  201.   CalcPieceTab;  { init the PieceTable, closely coupled with the board }
  202. end; { InitBoard }
  203.  
  204.  
  205.  
  206. procedure Perform(Move : MoveType; ResetMove : boolean);
  207. { Performs or takes Back Move (takes Back if ResetMove if true),
  208.   and performs the updating of Board and PieceTab. Player must
  209.   contain the Color of the moving Player, Opponent the Color
  210.   of the Opponent.
  211.  
  212.   MovePiece, DeletePiece, InsertPiece and ChangeType
  213.   are used to Update the Board module
  214. }
  215.  
  216. var
  217.   New1,CastSquare,CornerSquare,EpSquare : SquareType;
  218. begin
  219.   with CC, Move do
  220.   begin
  221.     if ResetMove then              { Perform Move }
  222.     begin
  223.       MovePiece(Old,New1);
  224.       if Content <> Empty then
  225.         InsertPiece(Content,Opponent,New1);
  226.     end
  227.     else
  228.     begin
  229.       if Content <> Empty then
  230.         DeletePiece(New1);
  231.       MovePiece(New1,Old);
  232.     end;
  233.     if Spe then                   { Test if Move is special }
  234.       if MovPiece = King then
  235.       begin
  236.         GenCastSquare(New1,CastSquare,CornerSquare); { Castling Move }
  237.         if ResetMove then
  238.           MovePiece(CornerSquare,CastSquare)
  239.         else
  240.           MovePiece(CastSquare,CornerSquare);
  241.       end
  242.       else
  243.         if MovPiece = Pawn then
  244.         begin
  245.           EpSquare := (New1 and 7) + (Old and $70);    { E.p. capture }
  246.           if ResetMove then
  247.             InsertPiece(Pawn,Opponent,EpSquare)
  248.           else
  249.             DeletePiece(EpSquare);
  250.         end
  251.         else                                          { Pawn-promotion }
  252.           if ResetMove then
  253.             ChangeType(Pawn,Old)
  254.           else
  255.             ChangeType(MovPiece,New1);
  256.   end; { with }
  257. end; { Perform }
  258.  
  259. end.