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

  1. unit GameTask;
  2.  
  3. {$W-}
  4.  
  5. interface
  6.  
  7. const
  8.   tmFindMove    = 1;
  9.   tmThinkAhead  = 2;
  10.   tmResume      = 3;
  11.   tmComplete    = 4;
  12.   tmEnterMove   = 5;
  13.   tmTimeExpired = 6;
  14.   tmTerminate   = $FFFF;
  15.  
  16. procedure DoGameTask;
  17.  
  18. implementation
  19.  
  20. uses GameRec, LMoves, LMoveGen, LOpenLib, LEval, TaskMgr;
  21.  
  22. procedure EnterOppMove;
  23. begin
  24.   AdjustMoves;
  25.   EnterKeyMove;
  26.   StoreMoves;
  27.   Exclude(CC.State, MovePending);
  28. end;
  29.  
  30. procedure RecordFindMove;
  31. begin
  32.   with CC do
  33.   begin
  34.     MovTab[0] := MainLine[0];       { Copy the MainLine to HintLine }
  35.     Move(MainLine[1], HintLine[0], Sizeof(MainLine) - Sizeof(MoveType));
  36.     HintEvalu := MainEvalu;
  37.     if MovTab[0].MovPiece = Empty then
  38.     begin
  39.       HintLine[0] := ZeroMove;      { No Possible Move }
  40.       Include(State, GameOver);
  41.       Exit;
  42.     end; { if }
  43.  
  44.     EnterMove(MovTab[Depth + 1]);
  45.     StoreMoves;
  46.     PlayerMove := ZeroMove;
  47.     Exclude(State, Analysis);
  48.   end;
  49. end;
  50.  
  51. procedure StartMove;
  52. { The program moves }
  53. var
  54.   Result: Integer;
  55.   Dep: DepthType;
  56. begin
  57.   Include(CC.State, Analysis);
  58.   Exclude(CC.State, OppAnalysis);
  59.  
  60.   { Wait for a Think }
  61.   repeat until Message(tmComplete) = tmResume;
  62.  
  63.   AdjustMoves;
  64.   CalcLibNo;                        { Try to find a Move in }
  65.   with CC do                       {  the opening library  }
  66.   begin
  67.     Depth := 0;
  68.     if LibNo > 0 then
  69.     begin
  70.  {!     OpeningLibMsg;}
  71.       Include(State, InLibrary);
  72.       FindOpeningMove;
  73.     end
  74.     else
  75.     begin
  76.       Exclude(State, InLibrary);
  77.       FindMove(MaxLevel);           { Perform the Search }
  78.     end;
  79.     Depth := -1;
  80.     RecordFindMove;
  81.   end;  { with }
  82. end; { StartMove }
  83.  
  84. procedure ThinkAhead;
  85. begin
  86. { Perform analysis in the opponents time of reflection.
  87.   The program assumes that the Opponent will Perform the
  88.   Hint Move, and starts analysing On it counter Move }
  89.  
  90.   with CC do
  91.   begin
  92.     if HintLine[0].MovPiece = Empty then
  93.       Exit;
  94.  
  95.     Exclude(State, Analysis);
  96.     Include(State, OppAnalysis);
  97.  
  98.     { Wait for a Think }
  99.     repeat until Message(tmComplete) = tmResume;
  100.  
  101.     AdjustMoves;                     { Setup surroundings as if the }
  102.     MovTab[Depth + 1] := HintLine[0];      { Opponent had performed }
  103.     MakeMove(MovTab[Depth + 1]);                   {  the hint move }
  104.     StoreMoves;
  105.     AdjustMoves;
  106.     Depth := 0;                        { Analyse until something is }
  107.     FindMove(MaxLevel);                {  entered from the keyboard }
  108.  
  109.     Depth := -1;
  110.     Exclude(State, OppAnalysis);
  111.  
  112.     if MovePending in State then
  113.     begin
  114.       { Here if we received a tmEnterMove and the opponent did not make
  115.         the hint move.  Find the move the old fashioned way }
  116.       TakeBackMove(MovTab[Depth]);
  117.  
  118.       { Enter opponents move }
  119.       EnterOppMove;
  120.  
  121.       { Only legal message to receive now is tmFindMove }
  122.       repeat until Message(tmFindMove) = tmFindMove;
  123.  
  124.       { Start the move }
  125.       StartMove;
  126.     end
  127.     else
  128.       RecordFindMove;
  129.   end;
  130. end; { ThinkAwhile }
  131.  
  132. { Game background task's main loop }
  133.  
  134. procedure DoGameTask;
  135. begin
  136.   repeat
  137.     case Message(tmComplete) of
  138.       tmEnterMove:  EnterOppMove;
  139.       tmFindMove:   StartMove;
  140.       tmThinkAhead: ThinkAhead;
  141.       tmTerminate:  Exit;
  142.     end;
  143.   until False;
  144. end;
  145.  
  146. end.