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

  1. {************************************************}
  2. {                                                }
  3. {   Chess - Shared DLL Example                   }
  4. {   CHESS.DLL Move list and making managment     }
  5. {   Copyright (c) 1992 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. unit LMoves;
  10.  
  11. {$R-,Q-,S-,W-}
  12.  
  13. interface
  14.  
  15. uses GameRec, ChessInf;
  16.  
  17. const
  18.   LoseValue = $7D00;             { Evaluation constants }
  19.   MateValue = $7C80;
  20.   DepthFactor = $80;
  21.  
  22. procedure InitChessTime;
  23. procedure StopChessTime;
  24. procedure StartChessTime(Color : ColorType);
  25. function IllegalMove(Move : MoveType) : boolean;
  26. procedure MakeMove(Move : MoveType);
  27. procedure TakeBackMove(Move : MoveType);
  28. procedure EnterKeyMove;
  29. procedure ResetMoves;
  30. procedure AdjustMoves;
  31. procedure StoreMoves;
  32. procedure ClearHint;
  33. function  CalcSquare(a,b : char) : EdgeSquareType;
  34. function  MoveCheck(const Move : MoveType): TChessError;
  35. procedure EnterMove(Move : MoveType);
  36.  
  37. implementation
  38.  
  39. uses LBoard, LMoveGen, LTimer;
  40.  
  41. procedure InitChessTime;
  42. { Initializes the chess clocks }
  43. begin
  44.   CC.ChessTime[White].Init;
  45.   CC.ChessTime[Black].Init;
  46.   CC.Running := False;
  47. end; { InitChessTime }
  48.  
  49. procedure StopChessTime;
  50. { Stop the Running chess Clock }
  51. begin
  52.   with CC do
  53.   begin
  54.     if Running then
  55.       ChessTime[RunColor].Stop;
  56.     Running := False;
  57.   end;
  58. end; { StopChessTime }
  59.  
  60. procedure StartChessTime(Color : ColorType);
  61. { Stop the Running chess Clock and Start the Clock for Color }
  62. begin
  63.   StopChessTime;
  64.   with CC do
  65.   begin
  66.     RunColor := Color;
  67.     Running := True;
  68.     ChessTime[RunColor].Start;
  69.   end;
  70. end; { StartChessTime }
  71.  
  72. function IllegalMove(Move : MoveType) : boolean;
  73. { Tests whether the Move is Legal for
  74.   ProgramColor = Player in the position }
  75. begin
  76.   Perform(Move,DoIt);
  77.   with CC do
  78.     IllegalMove := Attacks(Opponent,PieceTab[Player,0].ISquare);
  79.   Perform(Move,UndoIt);
  80. end; { IllegalMove }
  81.  
  82. procedure MakeMove(Move : MoveType);
  83. { Makes Move for ProgramColor=Player, and updates variables }
  84. begin
  85.   with CC do
  86.   begin
  87.     Inc(Depth);
  88.     Inc(MoveNo);
  89.     Perform(Move, DoIt);
  90.     ProgramColor := Opponent;
  91.     Opponent := Player;
  92.     Player := ProgramColor;
  93.   end;
  94. end; { MakeMove }
  95.  
  96. procedure TakeBackMove(Move : MoveType);
  97. { Takes Back Move and updates variables }
  98. begin
  99.   with CC do
  100.   begin
  101.     ProgramColor := Opponent;
  102.     Opponent := Player;
  103.     Player := ProgramColor;
  104.     Perform(Move, UndoIt);
  105.     MoveNo := Pred(MoveNo);
  106.     Depth := Pred(Depth);
  107.   end;
  108. end; { TakeBackMove }
  109.  
  110. procedure ResetMoves;
  111. { Resets MovTab }
  112. begin
  113.   with CC do
  114.   begin
  115.     Depth := -1;
  116.     MovTab[-1] := ZeroMove;
  117.   end;
  118. end; { ResetMoves }
  119.  
  120. procedure AdjustMoves;
  121. { Moves MovTab to Depth=-1 }
  122. var   i :     integer;
  123. begin
  124.   if CC.Depth <> -1 then
  125.   with CC do
  126.   begin
  127.     for i := Depth downto Back do
  128.        MovTab[i - Succ(Depth)] := MovTab[i];
  129.     Depth := -1;
  130.   end;
  131. end; { AdjustMoves }
  132.  
  133. procedure StoreMoves;
  134. { Moves MovTab One Move Back }
  135. var   i : integer;
  136. begin
  137.   with CC do
  138.   begin
  139.     Dec(Depth);
  140.    for i := Back to Depth do
  141.       MovTab[i] := MovTab[Succ(i)];
  142.  
  143.     MovTab[Back] := ZeroMove;
  144.   end;
  145. end; { StoreMoves }
  146.  
  147. procedure ClearHint;
  148. { Clears HintLine }
  149. begin
  150.   with CC do
  151.   begin
  152.     HintLine[0] := ZeroMove;
  153.     HintEvalu := 0;
  154.   end;
  155. end; { ClearHint }
  156.  
  157.  
  158. procedure EnterMove(Move : MoveType);
  159. { Performs a Move in the game }
  160. begin
  161.    MakeMove(Move);
  162.    StartChessTime(CC.ProgramColor);
  163. end;
  164.  
  165. procedure EnterKeyMove;
  166. { Perform the Move entered from the keyboard (KeyMove) }
  167. begin
  168.   with CC do
  169.   begin
  170.     MovTab[Succ(Depth)] := KeyMove;
  171.     PlayerMove := KeyMove;
  172.     ClearHint;
  173.     EnterMove(MovTab[Succ(Depth)]);
  174.   end;
  175. end;
  176.  
  177.  
  178. function CalcSquare(a,b : char) : EdgeSquareType;
  179. { Converts the Square indicator from its input from to a
  180.   form that the Analysis part of the program understands  }
  181. begin
  182.    if (a in ['A'..'H']) and (b in ['1'..'8']) then
  183.       CalcSquare := (ord(b) - ord('1')) * 16 +
  184.                   (ord(a) - ord('A'))
  185.    else
  186.       CalcSquare := -1;
  187. end; { CalcSquare }
  188.  
  189.  
  190.  
  191. { This procedure checks to see if the given Move is Legal }
  192. function MoveCheck(const Move : MoveType): TChessError;
  193. begin
  194.   with CC do
  195.   begin
  196.     Inc(Depth);                    { Check Move }
  197.  
  198.     KeyMove := ZeroMove;
  199.     InitMovGen;
  200.     repeat
  201.       { Generate all moves and find One which matches the input }
  202.       MovGen;
  203.       if (NextMove.MovPiece <> Empty) and
  204.         ((NextMove.MovPiece = Move.MovPiece) or (Move.MovPiece = Empty)) and
  205.         ((NextMove.New1 = Move.New1) or (Move.New1 and $88 <> 0)) and
  206.         ((NextMove.Old = Move.Old) or (Move.Old and $88 <> 0)) then
  207.       begin
  208.         if KeyMove.MovPiece = Empty then
  209.           KeyMove := NextMove
  210.         else
  211.         begin
  212.           if (NextMove.MovPiece = Pawn) and (KeyMove.MovPiece <> Pawn) then
  213.             KeyMove := NextMove
  214.           else if (NextMove.MovPiece <> Pawn) and (KeyMove.MovPiece = Pawn) then
  215.           begin
  216.             { Leave KeyMove in place }
  217.           end
  218.           else 
  219.           begin
  220.             Dec(Depth);
  221.             MoveCheck := ceAmbiguousMove;
  222.             Exit;
  223.           end;
  224.         end;
  225.       end;
  226.     until NextMove.MovPiece = Empty;
  227.  
  228.     Dec(Depth);
  229.     if KeyMove.MovPiece = Empty then
  230.       MoveCheck := ceInvalidMove
  231.     else
  232.       if IllegalMove(KeyMove) then
  233.         MoveCheck := ceIllegalMove
  234.       else
  235.         MoveCheck := ceOK;
  236.  
  237.     { The entered Move has been checked for correctness. It has
  238.       been placed in KeyMove for Search }
  239.   end;  { with CC^ }
  240. end; { MoveCheck }
  241.  
  242. end.