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

  1. {
  2.                               CHESS 4.0
  3.             Copyright (c) 1985, 87 by Borland International, Inc.
  4.  
  5.   This module implements many of the user commands from the
  6.   main menu:  Move, Hint, etc.
  7.  
  8. }
  9. unit LMoves;
  10.  
  11. interface
  12.  
  13. uses GameRec, ChessInf;
  14.  
  15. const
  16.   LoseValue = $7D00;             { Evaluation constants }
  17.   MateValue = $7C80;
  18.   DepthFactor = $80;
  19.  
  20. procedure InitChessTime;
  21. procedure StopChessTime;
  22. procedure StartChessTime(Color : ColorType);
  23. function IllegalMove(Move : MoveType) : boolean;
  24. procedure MakeMove(Move : MoveType);
  25. procedure TakeBackMove(Move : MoveType);
  26. procedure EnterKeyMove;
  27. procedure ResetMoves;
  28. procedure AdjustMoves;
  29. procedure StoreMoves;
  30. procedure ClearHint;
  31. function  CalcSquare(a,b : char) : EdgeSquareType;
  32. function  MoveCheck(const Move : MoveType): TChessError;
  33. procedure EnterMove(Move : MoveType);
  34.  
  35. implementation
  36.  
  37. uses LBoard, LMoveGen, LTimer;
  38.  
  39. procedure InitChessTime;
  40. { Initializes the chess clocks }
  41. begin
  42.   CC.ChessTime[White].Init;
  43.   CC.ChessTime[Black].Init;
  44.   CC.Running := False;
  45. end; { InitChessTime }
  46.  
  47. procedure StopChessTime;
  48. { Stop the Running chess Clock }
  49. begin
  50.   with CC do
  51.   begin
  52.     if Running then
  53.       ChessTime[RunColor].Stop;
  54.     Running := False;
  55.   end;
  56. end; { StopChessTime }
  57.  
  58. procedure StartChessTime(Color : ColorType);
  59. { Stop the Running chess Clock and Start the Clock for Color }
  60. begin
  61.   StopChessTime;
  62.   with CC do
  63.   begin
  64.     RunColor := Color;
  65.     Running := True;
  66.     ChessTime[RunColor].Start;
  67.   end;
  68. end; { StartChessTime }
  69.  
  70. function IllegalMove(Move : MoveType) : boolean;
  71. { Tests whether the Move is Legal for
  72.   ProgramColor = Player in the position }
  73. begin
  74.   Perform(Move,DoIt);
  75.   with CC do
  76.     IllegalMove := Attacks(Opponent,PieceTab[Player,0].ISquare);
  77.   Perform(Move,UndoIt);
  78. end; { IllegalMove }
  79.  
  80. procedure MakeMove(Move : MoveType);
  81. { Makes Move for ProgramColor=Player, and updates variables }
  82. begin
  83.   with CC do
  84.   begin
  85.     Inc(Depth);
  86.     Inc(MoveNo);
  87.     Perform(Move, DoIt);
  88.     ProgramColor := Opponent;
  89.     Opponent := Player;
  90.     Player := ProgramColor;
  91.   end;
  92. end; { MakeMove }
  93.  
  94. procedure TakeBackMove(Move : MoveType);
  95. { Takes Back Move and updates variables }
  96. begin
  97.   with CC do
  98.   begin
  99.     ProgramColor := Opponent;
  100.     Opponent := Player;
  101.     Player := ProgramColor;
  102.     Perform(Move, UndoIt);
  103.     MoveNo := Pred(MoveNo);
  104.     Depth := Pred(Depth);
  105.   end;
  106. end; { TakeBackMove }
  107.  
  108. procedure ResetMoves;
  109. { Resets MovTab }
  110. begin
  111.   with CC do
  112.   begin
  113.     Depth := -1;
  114.     MovTab[-1] := ZeroMove;
  115.   end;
  116. end; { ResetMoves }
  117.  
  118. procedure AdjustMoves;
  119. { Moves MovTab to Depth=-1 }
  120. var   i :     integer;
  121. begin
  122.   if CC.Depth <> -1 then
  123.   with CC do
  124.   begin
  125.     for i := Depth downto Back do
  126.        MovTab[i - Succ(Depth)] := MovTab[i];
  127. {    Move( MovTab[Back], MovTab[Back -1 -Depth], abs(Back - Depth) * sizeof(MoveType));}
  128.     Depth := -1;
  129.   end;
  130. end; { AdjustMoves }
  131.  
  132. procedure StoreMoves;
  133. { Moves MovTab One Move Back }
  134. var   i : integer;
  135. begin
  136.   with CC do
  137.   begin
  138.     Dec(Depth);
  139.    for i := Back to Depth do
  140.       MovTab[i] := MovTab[Succ(i)];
  141.  
  142. {    Move( MovTab[Back+1], MovTab[Back],abs(Back - Depth-1) * sizeof(MoveType));}
  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.