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

  1. {************************************************}
  2. {                                                }
  3. {   Chess - Shared DLL Example                   }
  4. {   CHESS.DLL Opening library managment.         }
  5. {   Copyright (c) 1992 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. unit LOpenLib;
  10.  
  11. {$R-,Q-,S-,W-}
  12.  
  13. {$IFDEF WINDOWS}
  14. {$R LOPENLIB.RES}
  15. {$ENDIF}
  16.  
  17. interface
  18.  
  19. procedure CalcLibNo;
  20. procedure FindOpeningMove;
  21.  
  22. implementation
  23.  
  24. uses
  25.  
  26. {$IFDEF WINDOWS}
  27.   WinApi,
  28. {$ENDIF}
  29.   GameRec, LBoard, LMoveGen, LMoves;
  30.  
  31. const UnPlayMark = $3F;
  32.  
  33. type
  34.   POpeningLib = ^TOpeningLib;
  35.   TOpeningLib = array [0..31999] of byte;
  36.  
  37. var
  38.   Openings : POpeningLib;
  39.  
  40. procedure PreviousLibNo(var Lib: Integer);
  41. { Sets LibNo to the previous Move in the block }
  42. var   n : integer;
  43. begin
  44.   n := 0;
  45.   repeat
  46.     Dec(Lib);
  47.     if Openings^[Lib] >= 128 then
  48.       Inc(n);
  49.     if (Openings^[Lib] and 64) <> 0 then
  50.       Dec(n);
  51.   until n = 0;
  52. end; { PreviousLibNo }
  53.  
  54. procedure FirstLibNo(var Lib: Integer);
  55. { Sets LibNo to the First Move in the block }
  56. begin { FirstLibNo }
  57.   while (Openings^[Lib - 1] and 64) = 0 do
  58.     PreviousLibNo(Lib);
  59. end; { FirstLibNo }
  60.  
  61.  
  62. procedure NextLibNo(var Lib: Integer; Skip : Boolean);
  63. { Sets LibNo to the Next Move in the block.
  64.   Unplayable moves are skipped if skip is set }
  65. var
  66.   n : integer;
  67. begin
  68.   if Openings^[Lib] >= 128 then
  69.     FirstLibNo(Lib)
  70.   else
  71.   begin
  72.     n := 0;
  73.     repeat
  74.       if (Openings^[Lib] and 64) <> 0 then
  75.         Inc(n);
  76.       if Openings^[Lib] >= 128 then
  77.         Dec(n);
  78.       Inc(Lib);
  79.     until n = 0;
  80.     if Skip and (Openings^[Lib] = UnPlayMark) then
  81.       FirstLibNo(Lib);
  82.   end;
  83. end; { NextLibNo }
  84.  
  85.  
  86. procedure CalcLibNo;
  87. { Sets LibNo to the block corresponding to the position }
  88.  
  89. var
  90.   LibDepth : DepthType;
  91.   Found    : boolean;
  92.  
  93.    procedure FindNode;
  94.    { Find the node corresponding to the correct block }
  95.    begin
  96.      with CC do
  97.      begin
  98.        Inc(LibNo);
  99.        if Depth > LibDepth then
  100.        begin
  101.           Found := true;
  102.           Exit;
  103.        end;
  104.        OpCount := -1;
  105.        InitMovGen;
  106.        repeat
  107.           OpCount := OpCount + 1;
  108.           MovGen;
  109.        until (NextMove.MovPiece = Empty) or EqMove(NextMove,MovTab[Depth]);
  110.        if NextMove.MovPiece <> Empty then
  111.        begin
  112.           while ((Openings^[LibNo] and 63) <> OpCount) and
  113.                  (Openings^[LibNo] < 128) do
  114.              NextLibNo(LibNo, False);
  115.           if (Openings^[LibNo] and 127) = 64 + OpCount then
  116.           begin
  117.              MakeMove(MovTab[Depth]);
  118.              FindNode;
  119.              TakeBackMove(MovTab[Depth - 1]);
  120.           end;
  121.        end;
  122.      end; { with }
  123.    end; { FindNode }
  124.  
  125. begin { CalcLibNo }
  126.   with CC do
  127.   begin
  128.     LibNo := 0;
  129.     if Openings = nil then
  130.     begin
  131.       UseLib := 200;
  132.       Exit;
  133.     end;
  134.     if MoveNo < UseLib then
  135.     begin
  136.        LibDepth := Depth;
  137.        while MovTab[Depth].MovPiece <> Empty do
  138.           TakeBackMove(MovTab[Depth]);
  139.        Found := False;
  140.        if MovTab[Depth].Content = King then
  141.        begin
  142.          Inc(Depth);
  143.          FindNode;
  144.          Dec(Depth);
  145.        end;
  146.        while Depth < LibDepth do
  147.          MakeMove(MovTab[Depth + 1]);
  148.        if Found then
  149.           UseLib := 200
  150.        else
  151.        begin
  152.           UseLib := MoveNo;
  153.           LibNo := 0;
  154.        end;
  155.     end;
  156.   end;
  157. end; { CalcLibNo }
  158.  
  159.  
  160. procedure FindOpeningMove;
  161. { Finds an opening move from the library }
  162. const Weight : array[0..6] of byte = (7,10,12,13,14,15,16);
  163. var
  164.   Cnt, r, p, CountP : byte;
  165. begin
  166.   r := Random(17);      { Calculate weighted Random Number in 0..16 }
  167.   p := 0;
  168.   while r >= Weight[p] do
  169.    Inc(p);
  170.   with CC do
  171.   begin
  172.     for CountP := 1 to p do                 { Find corresponding node }
  173.       NextLibNo(LibNo, True);
  174.     OpCount := Openings^[LibNo] and 63;      { Generate the Move       }
  175.     InitMovGen;
  176.     for Cnt := 0 to OpCount do
  177.       MovGen;
  178.     MainLine[0] := NextMove;             { Store the Move in MainLine }
  179.     MainLine[1] := ZeroMove;
  180.     MainEvalu := 0;
  181.     MaxDepth := 0;
  182.     LegalMoves := 0;
  183.     Nodes := 0;
  184.   end;
  185. end; { FindOpeningMove }
  186.  
  187. {$IFDEF WINDOWS}
  188. var
  189.   SaveExit : Pointer;
  190.  
  191. procedure ReleaseOpeningLib; far;
  192. begin
  193.   ExitProc := SaveExit;
  194.   if Openings <> nil then
  195.   begin
  196.     UnlockResource(GlobalHandle(Seg(Openings^)));
  197.     FreeResource(GlobalHandle(Seg(Openings^)));
  198.   end;
  199. end;
  200.  
  201. { Read the opening library from the DLL resources here }
  202. procedure LoadOpening;
  203. begin
  204.   SaveExit := ExitProc;
  205.   ExitProc := @ReleaseOpeningLib;
  206.   Openings := LockResource(LoadResource(hInstance,
  207.     FindResource(hInstance, 'OPENINGMOVES', 'OPENINGLIB')));
  208. end;
  209.  
  210. {$ELSE}
  211.  
  212. { Read the opening library from OPENING.LIB }
  213. procedure LoadOpening;
  214. const
  215.   LibFileName = 'OPENING.LIB';    { note: can edit and include a path }
  216. var
  217.   LibFile : file of TOpeningLib;
  218. begin
  219.   Assign(LibFile, LibFileName);
  220.   {$I-}
  221.   Reset(LibFile);
  222.   if IOresult = 0 then
  223.   begin
  224.     New(Openings);
  225.     Read(LibFile,Openings^);
  226.     Close(LibFile);
  227.     Openings^[0] := $FF;
  228.   end;
  229. end;
  230.  
  231. {$ENDIF}
  232.  
  233. begin
  234.   LoadOpening;
  235. end.
  236.  
  237. end.