Changeset 36 for trunk/UGame.pas


Ignore:
Timestamp:
Oct 13, 2019, 5:14:50 PM (5 years ago)
Author:
chronos
Message:
  • Fixed: Missing UFormHistory files.
  • Added: Allow to disable moves history recording.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r35 r36  
    6161    InitialTilesPos: array of TPoint;
    6262    procedure GetStep(GameStep: TGame; Step: Integer);
     63    procedure Clear;
    6364    constructor Create;
    6465    destructor Destroy; override;
     
    9697    FOnGameOver: TNotifyEvent;
    9798    FOnWin: TNotifyEvent;
     99    FRecordHistory: Boolean;
    98100    FRunning: Boolean;
    99101    FScore: Integer;
     
    101103    FBoardUndo: TBoard;
    102104    function GetTileColor(Value: Integer): TColor;
     105    procedure SetRecordHistory(AValue: Boolean);
    103106    procedure SetScore(AValue: Integer);
    104107    procedure DoChange;
     
    138141    property OnGameOver: TNotifyEvent read FOnGameOver write FOnGameOver;
    139142    property Moving: Boolean read FMoving;
     143    property RecordHistory: Boolean read FRecordHistory write SetRecordHistory;
    140144  end;
    141145
     
    173177var
    174178  I: Integer;
    175   C: Integer;
    176   HistoryMove: THistoryMove;
    177179begin
    178180  with Reg do begin
    179181    CurrentContext := RegContext;
    180     C := ReadIntegerWithDefault('Count', 0);
    181     for I := 0 to C - 1 do begin
    182       HistoryMove := THistoryMove.Create;
    183       Add(HistoryMove);
    184       HistoryMove.LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '/' + IntToStr(I)));
     182    Count := ReadIntegerWithDefault('Count', 0);
     183    for I := 0 to Count - 1 do begin
     184      Items[I] := THistoryMove.Create;
     185      THistoryMove(Items[I]).LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '/' + IntToStr(I)));
    185186    end;
    186187  end;
     
    229230      else raise Exception.Create('Tile should be empty');
    230231  end;
     232end;
     233
     234procedure THistory.Clear;
     235begin
     236  Moves.Clear;
     237  SetLength(InitialTilesPos, 0);
    231238end;
    232239
     
    475482    Result := EmptyTiles[Random(EmptyTiles.Count)];
    476483    Result.Value := NewValue;
     484    if RecordHistory then begin
     485      SetLength(History.InitialTilesPos, Length(History.InitialTilesPos) + 1);
     486      History.InitialTilesPos[Length(History.InitialTilesPos) - 1] := Result.Index;
     487    end;
    477488  end;
    478489  EmptyTiles.Free;
     
    512523var
    513524  I: Integer;
    514   NewTile: TTile;
    515525begin
    516526  FCanUndo := False;
     
    518528  Score := 0;
    519529  Running := True;
    520   with History do begin
    521     Moves.Clear;
    522 
    523     SetLength(InitialTilesPos, 0);
    524     for I := 0 to 1 do begin
    525       NewTile := FillRandomTile(0);
    526       SetLength(InitialTilesPos, Length(InitialTilesPos) + 1);
    527       InitialTilesPos[Length(InitialTilesPos) - 1] := NewTile.Index;
    528     end;
    529   end;
     530  History.Clear;
     531  for I := 0 to 1 do
     532    FillRandomTile(0);
    530533  DoChange;
    531534end;
     
    860863
    861864    NewTile := FillRandomTile;
    862     HistoryMove := THistoryMove.Create;
    863     HistoryMove.Direction := Direction;
    864     HistoryMove.NewItemPos := NewTile.Index;
    865     HistoryMove.NewItemValue := NewTile.Value;
    866     History.Moves.Add(HistoryMove);
     865    if RecordHistory then begin
     866      HistoryMove := THistoryMove.Create;
     867      HistoryMove.Direction := Direction;
     868      HistoryMove.NewItemPos := NewTile.Index;
     869      HistoryMove.NewItemValue := NewTile.Value;
     870      History.Moves.Add(HistoryMove);
     871    end;
    867872
    868873    if not CanMove and (Board.GetEmptyTilesCount = 0) then
     
    902907    WriteBool('CanUndo', FCanUndo);
    903908    WriteBool('UndoEnabled', UndoEnabled);
     909    WriteBool('RecordHistory', RecordHistory);
    904910    FBoardUndo.SaveToRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\BoardUndo'));
    905911    Board.SaveToRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Board'));
     
    924930    FCanUndo := ReadBoolWithDefault('CanUndo', False);
    925931    UndoEnabled := ReadBoolWithDefault('UndoEnabled', True);
     932    RecordHistory := ReadBoolWithDefault('RecordHistory', False);
    926933    FBoardUndo.LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\BoardUndo'));
    927934    Board.LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Board'));
     
    970977end;
    971978
     979procedure TGame.SetRecordHistory(AValue: Boolean);
     980begin
     981  if FRecordHistory = AValue then Exit;
     982  FRecordHistory := AValue;
     983  if not FRecordHistory then History.Clear;
     984end;
     985
    972986procedure TGame.SetScore(AValue: Integer);
    973987begin
Note: See TracChangeset for help on using the changeset viewer.