Ignore:
Timestamp:
Jun 7, 2024, 2:36:34 PM (3 months ago)
Author:
chronos
Message:
  • Fixed: Fullscreen mode was not working on start.
  • Added: Step button in AI form to do single step.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/FormComputer.pas

    r86 r88  
    3434  TFormComputer = class(TFormEx)
    3535    ButtonStart: TButton;
    36     ButtonStop: TButton;
     36    ButtonEnd: TButton;
     37    ButtonStep: TButton;
    3738    Label1: TLabel;
    3839    TrackBar1: TTrackBar;
    3940    procedure ButtonStartClick(Sender: TObject);
    40     procedure ButtonStopClick(Sender: TObject);
     41    procedure ButtonEndClick(Sender: TObject);
     42    procedure ButtonStepClick(Sender: TObject);
    4143    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    4244    procedure FormCreate(Sender: TObject);
    4345    procedure FormDestroy(Sender: TObject);
    4446  private
    45     Stop: Boolean;
     47    Running: Boolean;
    4648    GameTries1: TGameTries;
    4749    GameTries2: TGameTries;
    4850    GameTries3: TGameTries;
    4951    GameTries4: TGameTries;
     52    procedure Step;
    5053    procedure TryAllDirections(GameTries: TGameTries; GameTry: TGameTry);
     54    procedure UpdateInterface;
    5155  end;
    5256
     
    151155  );
    152156begin
    153   ButtonStopClick(Self);
     157  Running := False;
    154158end;
    155159
     
    167171
    168172procedure TFormComputer.ButtonStartClick(Sender: TObject);
     173begin
     174  Running := True;
     175  while Core.Core.Game.CanMove and Running do begin
     176    Step;
     177  end;
     178  Running := False;
     179end;
     180
     181procedure TFormComputer.ButtonEndClick(Sender: TObject);
     182begin
     183  Running := False;
     184end;
     185
     186procedure TFormComputer.ButtonStepClick(Sender: TObject);
     187begin
     188  Step;
     189end;
     190
     191procedure TFormComputer.FormCreate(Sender: TObject);
     192begin
     193  GameTries1 := TGameTries.Create;
     194  GameTries2 := TGameTries.Create;
     195  GameTries3 := TGameTries.Create;
     196  GameTries4 := TGameTries.Create;
     197  UpdateInterface;
     198end;
     199
     200procedure TFormComputer.FormDestroy(Sender: TObject);
     201begin
     202  FreeAndNil(GameTries1);
     203  FreeAndNil(GameTries2);
     204  FreeAndNil(GameTries3);
     205  FreeAndNil(GameTries4);
     206end;
     207
     208procedure TFormComputer.Step;
    169209var
    170210  NewTry: TGameTry;
     
    176216  Delay: Integer;
    177217begin
    178   ButtonStart.Enabled := False;
    179   ButtonStop.Enabled := True;
    180   Stop := False;
    181218  with Core.Core.Game do begin
    182     while CanMove and not Stop do begin
    183       NewTry := TGameTry.Create;
    184       NewTry.Game.Assign(Core.Core.Game);
    185       GameTries1.Clear;
    186       TryAllDirections(GameTries1, NewTry);
    187       FreeAndNil(NewTry);
    188       GameTries2.Clear;
    189       for I := 0 to GameTries1.Count - 1 do
    190         TryAllDirections(GameTries2, GameTries1[I]);
    191       GameTries3.Clear;
    192       for I := 0 to GameTries2.Count - 1 do
    193         TryAllDirections(GameTries3, GameTries2[I]);
    194       GameTries4.Clear;
    195       for I := 0 to GameTries3.Count - 1 do
    196         TryAllDirections(GameTries4, GameTries3[I]);
    197 
    198       GameTries := GameTries4;
    199       GameTryComparer := TGameTryComparer.Create;
    200       GameTries.Sort(GameTryComparer);
    201       S := '';
    202       for I := 0 to GameTries.Count - 1 do begin
    203         S := S + FormatFloat('0.000', GameTries[I].GetFitness) + '(';
    204         for J := 0 to Length(GameTries[I].Moves) - 1 do
    205           S := S + DirectionText[GameTries[I].Moves[J]] + ' ';
    206         S := S + '), ';
    207       end;
    208       MoveAllAndUpdate(GameTries[0].Moves[0], False);
    209 {      if CanMergeDirection(drDown) then MoveAllAnimate(drDown)
    210       else if CanMergeDirection(drRight) then MoveAllAnimate(drRight)
    211       else if CanMergeDirection(drLeft) then MoveAllAnimate(drLeft)
    212       else if CanMoveDirection(drDown) then MoveAllAnimate(drDown)
    213       else if CanMoveDirection(drRight) then MoveAllAnimate(drRight)
    214       else if CanMoveDirection(drLeft) then MoveAllAnimate(drLeft)
    215       else if CanMoveDirection(drUp) then MoveAllAnimate(drUp)
    216       else Break;
    217       }
    218       Delay := TrackBar1.Position;
    219       while Delay > 0 do begin
    220         Application.ProcessMessages;
    221         Sleep(10);
    222         Delay := Delay - 10;
    223       end;
    224     end;
    225   end;
    226   ButtonStart.Enabled := True;
    227   ButtonStop.Enabled := False;
    228 end;
    229 
    230 procedure TFormComputer.ButtonStopClick(Sender: TObject);
    231 begin
    232   Stop := True;
    233 end;
    234 
    235 procedure TFormComputer.FormCreate(Sender: TObject);
    236 begin
    237   GameTries1 := TGameTries.Create;
    238   GameTries2 := TGameTries.Create;
    239   GameTries3 := TGameTries.Create;
    240   GameTries4 := TGameTries.Create;
    241 end;
    242 
    243 procedure TFormComputer.FormDestroy(Sender: TObject);
    244 begin
    245   FreeAndNil(GameTries1);
    246   FreeAndNil(GameTries2);
    247   FreeAndNil(GameTries3);
    248   FreeAndNil(GameTries4);
     219    NewTry := TGameTry.Create;
     220    NewTry.Game.Assign(Core.Core.Game);
     221    GameTries1.Clear;
     222    TryAllDirections(GameTries1, NewTry);
     223    FreeAndNil(NewTry);
     224    GameTries2.Clear;
     225    for I := 0 to GameTries1.Count - 1 do
     226      TryAllDirections(GameTries2, GameTries1[I]);
     227    GameTries3.Clear;
     228    for I := 0 to GameTries2.Count - 1 do
     229      TryAllDirections(GameTries3, GameTries2[I]);
     230    GameTries4.Clear;
     231    for I := 0 to GameTries3.Count - 1 do
     232      TryAllDirections(GameTries4, GameTries3[I]);
     233
     234    GameTries := GameTries4;
     235    GameTryComparer := TGameTryComparer.Create;
     236    GameTries.Sort(GameTryComparer);
     237    S := '';
     238    for I := 0 to GameTries.Count - 1 do begin
     239      S := S + FormatFloat('0.000', GameTries[I].GetFitness) + '(';
     240      for J := 0 to Length(GameTries[I].Moves) - 1 do
     241        S := S + DirectionText[GameTries[I].Moves[J]] + ' ';
     242      S := S + '), ';
     243    end;
     244    MoveAllAndUpdate(GameTries[0].Moves[0], False);
     245  {      if CanMergeDirection(drDown) then MoveAllAnimate(drDown)
     246    else if CanMergeDirection(drRight) then MoveAllAnimate(drRight)
     247    else if CanMergeDirection(drLeft) then MoveAllAnimate(drLeft)
     248    else if CanMoveDirection(drDown) then MoveAllAnimate(drDown)
     249    else if CanMoveDirection(drRight) then MoveAllAnimate(drRight)
     250    else if CanMoveDirection(drLeft) then MoveAllAnimate(drLeft)
     251    else if CanMoveDirection(drUp) then MoveAllAnimate(drUp)
     252    else Break;
     253    }
     254    Delay := TrackBar1.Position;
     255    while Delay > 0 do begin
     256      Application.ProcessMessages;
     257      Sleep(10);
     258      Delay := Delay - 10;
     259    end;
     260  end;
     261  UpdateInterface;
    249262end;
    250263
     
    268281end;
    269282
     283procedure TFormComputer.UpdateInterface;
     284begin
     285  ButtonStart.Enabled := not Running;
     286  ButtonStep.Enabled := not Running;
     287  ButtonEnd.Enabled := Running;
     288end;
     289
    270290end.
    271291
Note: See TracChangeset for help on using the changeset viewer.