Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 7.82 KB | None | 0 0
  1. unit TaticasJogo;
  2.  
  3. interface
  4.  
  5. uses LogicaJogo, Classes, Dialogs, UITypes;
  6.  
  7. procedure LoadLib(FileName: string= '');
  8. procedure SaveLib(FileName: string = '');
  9. function FormatPosition(const Position: TPosition): string;
  10. function SelectMove(var Board: TPosition; MaxBufLen: Integer; var CurrentEstimate: Integer): Integer;
  11.  
  12. var
  13.   Lib: TStringList;
  14.  
  15. implementation
  16.  
  17. uses Windows, SysUtils, UnitGeral;
  18.  
  19. var
  20.   MySide: ShortInt;
  21.   Deep: Integer;
  22.   MaxBufferLen: Integer;
  23.   CurrentN: Integer;
  24.  
  25. const
  26.   NO_MOVES = 1;
  27.  
  28.   SingleCosts: array[0..31] of Integer =
  29.     ( 7, 11, 13, 10,
  30.      10, 12, 11, 10,
  31.       8, 12, 12, 11,
  32.      11, 13, 15, 11,
  33.      10, 16, 10,  9,
  34.      11, 13, 16, 11,
  35.      10, 10, 10, 10,
  36.       0,  0, 0,  0);
  37.  
  38.   MamCosts: array[0..31] of Integer =
  39.     (12, 11, 10, 13,
  40.      13, 10, 13, 13,
  41.      11, 14, 14, 13,
  42.      10, 15, 14, 10,
  43.      11, 14, 15, 10,
  44.      13, 14, 14, 11,
  45.      13, 13, 10, 13,
  46.      13, 11, 11, 12);
  47.  
  48.    Diag: array[0..31] of Integer =
  49.      (
  50.        1, 0, 0, 0,
  51.        1, 0, 0, 0,
  52.        0, 1, 0, 0,
  53.        0, 1, 0, 0,
  54.        0, 0, 1, 0,
  55.        0, 0, 1, 0,
  56.        0, 0, 0, 1,
  57.        0, 0, 0, 1
  58.      );
  59.  
  60. function LibPosition(const Position: TPosition; var Estimate: Integer): Boolean;
  61. var
  62.   Index: Integer;
  63.   PositionStr: string;
  64. begin
  65.   PositionStr := FormatPosition(Position);
  66.   Index := Lib.IndexOf(PositionStr);
  67.   Result := Index <> -1;
  68.   if Result then
  69.     Estimate := Integer(Lib.Objects[Index]);
  70. end;
  71.  
  72. function Estimate(const Board: TPosition): Integer;
  73. var
  74.   I: Integer;
  75.   C: Integer;
  76.   WS, BS, WM, BM: Integer;
  77.   WhiteDiag: Boolean;
  78.   BlackDiag: Boolean;
  79. begin
  80.   Result := 0;
  81.   WS := 0; BS := 0;
  82.   WM := 0; BM := 0;
  83.   WhiteDiag := False;
  84.   BlackDiag := False;
  85.   C := 0;
  86.  
  87.   for I := 0 to 31 do
  88.   begin
  89.     case Board.Field[I] of
  90.       brWhiteSingle:
  91.         begin
  92.           C := SingleCosts[I];
  93.           WS := WS + 1;
  94.         end;
  95.       brBlackSingle:
  96.         begin
  97.           C := SingleCosts[31-I];
  98.           BS := BS + 1;
  99.         end;
  100.       brWhiteMam:
  101.         begin
  102.           C := MamCosts[I];
  103.           WM := WM + 1;
  104.           if Diag[I] = 1 then WhiteDiag := True;
  105.         end;
  106.       brBlackMam:
  107.         begin
  108.           C := MamCosts[31-I];
  109.           BM := BM + 1;
  110.           if Diag[I] = 1 then BlackDiag := True;
  111.         end;
  112.       else Continue
  113.     end;  
  114.     Result := Result + C*Board.Field[I];
  115.   end;
  116.   if (BM <> 0) and (WS <> 0) then Result := Result div 2;
  117.   if (WS=0) and (BS=0) then
  118.   begin
  119.     if (WM=1) and (BM=1) then Result := 0;
  120.     if (WM=2) and (BM=1) then Result := 0;
  121.     if (WM=1) and (BM=2) then Result := 0;
  122.     if (WM=3) and (BM=1) and BlackDiag then Result := 0;
  123.     if (WM=1) and (BM=3) and WhiteDiag then Result := 0;
  124.   end;
  125.   if WhiteDiag then Result := Result + 100;  
  126.   if BlackDiag then Result := Result - 100;
  127. end;
  128.  
  129.  
  130.  
  131. function RecurseEstimate(var Position: TPosition): Integer;
  132. var
  133.   SaveCurrentN: Integer;
  134.   PositionCount: Integer;
  135.   I: Integer;
  136.   Temp: Integer;
  137.   Board: TPosition;
  138. begin
  139.   Board := Position;
  140.   if CurrentN > MaxBufferLen then
  141.   begin
  142.     Result := Estimate(Board);
  143.     Exit;
  144.   end;
  145.  
  146.   Deep := Deep + 20;
  147.   SaveCurrentN := CurrentN;
  148.   if Board.Active = ActiveWhite
  149.     then PositionCount := GetMovesWhite(SaveCurrentN, Board)
  150.     else PositionCount := GetMovesBlack(SaveCurrentN, Board);
  151.   CurrentN := CurrentN + PositionCount;
  152.  
  153.   if PositionCount = 0 then
  154.   begin
  155.     if Board.Active = ActiveWhite
  156.       then Result := -100000 + Deep
  157.       else Result := +100000 - Deep;
  158.   end
  159.   else if PositionCount = 1 then
  160.   begin
  161.     Result := RecurseEstimate(Buffer[SaveCurrentN]);
  162.   end
  163.   else begin
  164.  
  165.     Result := RecurseEstimate(Buffer[SaveCurrentN]);
  166.     for I := SaveCurrentN+1 to CurrentN - 1 do
  167.     begin
  168.       Temp := RecurseEstimate(Buffer[I]);
  169.       if (Board.Active = ActiveWhite) then
  170.       begin
  171.         if Temp > Result then
  172.           Result := Temp;
  173.       end
  174.       else begin
  175.         if Temp < Result then
  176.           Result := Temp;
  177.       end;
  178.     end;
  179.   end;
  180.  
  181.   Deep := Deep - 20;
  182.   CurrentN := SaveCurrentN;
  183. end;
  184.  
  185.  
  186.  
  187.  
  188. procedure WaitAnimation;
  189. begin
  190.   while SendMessage(MainForm.Handle, MM_IS_ANIMATION, 0, 0) = 1 do
  191.     Sleep(30);
  192. end;
  193.  
  194.  
  195. function SelectMove(var Board: TPosition; MaxBufLen: Integer; var CurrentEstimate: Integer): Integer;
  196. var
  197.   I: Integer;
  198.   CurrentIndex: Integer;
  199.   Temp: Integer;
  200. begin
  201.   try
  202.     MySide := Board.Active;
  203.     MaxBufferLen := MaxBufLen;
  204.     CurrentN := 0;
  205.     Deep := 0;
  206.  
  207.     if Board.Active = ActiveWhite
  208.       then CurrentN := Abs(GetMovesWhite(0, Board))
  209.       else CurrentN := Abs(GetMovesBlack(0, Board));
  210.  
  211.     if CurrentN = 0 then
  212.     begin
  213.       Result := NO_MOVES;
  214.       Exit;
  215.     end;
  216.  
  217.     if CurrentN = 1 then
  218.     begin
  219.       Board := Buffer[0];
  220.       Result := 0;
  221.       Exit;
  222.     end;
  223.  
  224.     SendMessage(MainForm.Handle, MM_DEBUG, 0, 0);
  225.     if not LibPosition(Buffer[0], CurrentEstimate) then
  226.       CurrentEstimate := RecurseEstimate(Buffer[0]);
  227.     SendMessage(MainForm.Handle, MM_DEBUG, Integer(@Buffer[0]), CurrentEstimate);
  228.     CurrentEstimate := CurrentEstimate + Random(101) - 50;
  229.     CurrentIndex := 0;
  230.     for I := 1 to CurrentN - 1 do
  231.     begin
  232.       if not LibPosition(Buffer[I], Temp) then
  233.         Temp := RecurseEstimate(Buffer[I]);
  234.       SendMessage(MainForm.Handle, MM_DEBUG, Integer(@Buffer[I]), Temp);
  235.       Temp := Temp + Random(21) - 10;
  236.       if MySide = ActiveWhite then
  237.       begin
  238.         if Temp > CurrentEstimate then
  239.         begin
  240.           CurrentEstimate := Temp;
  241.           CurrentIndex := I;
  242.         end;
  243.       end
  244.       else begin
  245.         if Temp < CurrentEstimate then
  246.         begin
  247.           CurrentEstimate := Temp;
  248.           CurrentIndex := I;
  249.         end;
  250.       end;
  251.     end;
  252.  
  253.     Board := Buffer[CurrentIndex];
  254.     Result := 0;
  255.  
  256.   finally
  257.     WaitAnimation;
  258.   end;  
  259. end;
  260.  
  261. function DefaultFileName: string;
  262. begin
  263.   Result := IncludeTrailingBackslash(ExtractFilePath(ParamStr(0))) +
  264.     'DamasHueLib.lib';
  265. end;
  266.  
  267. procedure LoadLib(FileName: string= '');
  268. var
  269.   Temp: TStringList;
  270.   I: Integer;
  271.   No: Integer;
  272. begin
  273.   if Trim(FileName) = '' then FileName := DefaultFileName;
  274.   try
  275.     Temp := TStringList.Create;
  276.     try
  277.       Temp.LoadFromFile(FileName);
  278.       for I := 0 to Temp.Count-1 do
  279.       begin
  280.         if Length(Trim(Temp[I])) <> 6 + 33 then Continue;
  281.         No := StrToIntDef(Copy(Trim(Temp[I]), 1, 6), -$7FFFFFFF);
  282.         if No = -$7FFFFFFF then Continue;
  283.         Lib.AddObject(Copy(Trim(Temp[I]), 7, 33), TObject(No));
  284.       end;
  285.       Lib.Sorted := True;
  286.     finally
  287.       Temp.Free;
  288.     end;
  289.   except
  290.     MessageDlg(Format('Erro ao carregar arquivo "%s"', [FileName]), mtWarning, [mbOk], 0);
  291.   end;
  292. end;
  293.  
  294. procedure SaveLib(FileName: string = '');
  295. var
  296.   I: Integer;
  297.   Estimate: Integer;
  298.   Temp: TStringList;
  299. begin
  300.   if Trim(FileName) = '' then FileName := DefaultFileName;
  301.   Temp := TStringList.Create;
  302.   try
  303.     for I := 0 to Lib.Count-1 do
  304.     begin
  305.       Estimate := Integer(Lib.Objects[I]);
  306.       if Estimate < 0
  307.         then Temp.Add('-' + FormatFloat('00000', -Estimate) + Lib[I])
  308.         else Temp.Add('+' + FormatFloat('00000', Estimate) + Lib[I]);
  309.     end;    
  310.     Temp.SaveToFile(FileName);
  311.   finally
  312.     Temp.Free;
  313.   end;
  314. end;
  315.  
  316. function FormatPosition(const Position: TPosition): string;
  317. var
  318.   I: Integer;
  319. begin
  320.   SetLength(Result, 33);
  321.   if Position.Active = ActiveWhite
  322.     then Result[1] := '+'
  323.     else Result[1] := '-';
  324.   for I := 0 to 31 do
  325.     case Position.Field[I] of
  326.       brWhiteSingle: Result[I+2] := 'w';
  327.       brBlackSingle: Result[I+2] := 'b';
  328.       brWhiteMam: Result[I+2] := 'W';
  329.       brBlackMam: Result[I+2] := 'B'
  330.       else Result[I+2] := '.'
  331.     end;  
  332. end;
  333.  
  334. initialization
  335.   Randomize;
  336.   Lib := TStringList.Create;
  337.  
  338. finalization
  339.   Lib.Free;
  340.  
  341. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement