Changeset 36 for trunk/UGame.pas
- Timestamp:
- Oct 13, 2019, 5:14:50 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r35 r36 61 61 InitialTilesPos: array of TPoint; 62 62 procedure GetStep(GameStep: TGame; Step: Integer); 63 procedure Clear; 63 64 constructor Create; 64 65 destructor Destroy; override; … … 96 97 FOnGameOver: TNotifyEvent; 97 98 FOnWin: TNotifyEvent; 99 FRecordHistory: Boolean; 98 100 FRunning: Boolean; 99 101 FScore: Integer; … … 101 103 FBoardUndo: TBoard; 102 104 function GetTileColor(Value: Integer): TColor; 105 procedure SetRecordHistory(AValue: Boolean); 103 106 procedure SetScore(AValue: Integer); 104 107 procedure DoChange; … … 138 141 property OnGameOver: TNotifyEvent read FOnGameOver write FOnGameOver; 139 142 property Moving: Boolean read FMoving; 143 property RecordHistory: Boolean read FRecordHistory write SetRecordHistory; 140 144 end; 141 145 … … 173 177 var 174 178 I: Integer; 175 C: Integer;176 HistoryMove: THistoryMove;177 179 begin 178 180 with Reg do begin 179 181 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))); 185 186 end; 186 187 end; … … 229 230 else raise Exception.Create('Tile should be empty'); 230 231 end; 232 end; 233 234 procedure THistory.Clear; 235 begin 236 Moves.Clear; 237 SetLength(InitialTilesPos, 0); 231 238 end; 232 239 … … 475 482 Result := EmptyTiles[Random(EmptyTiles.Count)]; 476 483 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; 477 488 end; 478 489 EmptyTiles.Free; … … 512 523 var 513 524 I: Integer; 514 NewTile: TTile;515 525 begin 516 526 FCanUndo := False; … … 518 528 Score := 0; 519 529 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); 530 533 DoChange; 531 534 end; … … 860 863 861 864 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; 867 872 868 873 if not CanMove and (Board.GetEmptyTilesCount = 0) then … … 902 907 WriteBool('CanUndo', FCanUndo); 903 908 WriteBool('UndoEnabled', UndoEnabled); 909 WriteBool('RecordHistory', RecordHistory); 904 910 FBoardUndo.SaveToRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\BoardUndo')); 905 911 Board.SaveToRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Board')); … … 924 930 FCanUndo := ReadBoolWithDefault('CanUndo', False); 925 931 UndoEnabled := ReadBoolWithDefault('UndoEnabled', True); 932 RecordHistory := ReadBoolWithDefault('RecordHistory', False); 926 933 FBoardUndo.LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\BoardUndo')); 927 934 Board.LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Board')); … … 970 977 end; 971 978 979 procedure TGame.SetRecordHistory(AValue: Boolean); 980 begin 981 if FRecordHistory = AValue then Exit; 982 FRecordHistory := AValue; 983 if not FRecordHistory then History.Clear; 984 end; 985 972 986 procedure TGame.SetScore(AValue: Integer); 973 987 begin
Note:
See TracChangeset
for help on using the changeset viewer.