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

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