home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 10.ddi / CHESS.ZIP / LBOARD.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  6.8 KB  |  294 lines

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