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

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