Changeset 19


Ignore:
Timestamp:
Oct 5, 2019, 1:00:29 PM (5 years ago)
Author:
chronos
Message:
  • Added: Remember board state between application restarts.
Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormMain.pas

    r17 r19  
    8989  Core.PersistentForm1.RegistryContext := Core.ApplicationInfo1.GetRegistryContext;
    9090  Core.PersistentForm1.Load(Self);
    91   Core.Game.New;
     91  if not Core.Game.Running then
     92    Core.Game.New;
    9293end;
    9394
  • trunk/Forms/UFormSettings.lfm

    r13 r19  
    1616    Height = 26
    1717    Top = 24
    18     Width = 150
    19     Caption = 'Animation speed:'
     18    Width = 172
     19    Caption = 'Animation duration:'
    2020    ParentColor = False
    2121  end
     
    5252  object ComboBoxLanguage: TComboBox
    5353    Left = 208
    54     Height = 43
     54    Height = 42
    5555    Top = 86
    5656    Width = 230
  • trunk/Forms/UFormSettings.lrj

    r13 r19  
    11{"version":1,"strings":[
    22{"hash":213582195,"name":"tformsettings.caption","sourcebytes":[83,101,116,116,105,110,103,115],"value":"Settings"},
    3 {"hash":69745274,"name":"tformsettings.label1.caption","sourcebytes":[65,110,105,109,97,116,105,111,110,32,115,112,101,101,100,58],"value":"Animation speed:"},
     3{"hash":10139450,"name":"tformsettings.label1.caption","sourcebytes":[65,110,105,109,97,116,105,111,110,32,100,117,114,97,116,105,111,110,58],"value":"Animation duration:"},
    44{"hash":1339,"name":"tformsettings.buttonok.caption","sourcebytes":[79,75],"value":"OK"},
    55{"hash":77089212,"name":"tformsettings.buttoncancel.caption","sourcebytes":[67,97,110,99,101,108],"value":"Cancel"},
  • trunk/Install/win/Game2048.iss

    r6 r19  
    5555Source: "{#MyAppSubDir}\lib\x86_64-win64-Release\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion; Check: Is64BitInstallMode
    5656Source: "{#MyAppSubDir}\lib\i386-win32-Release\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion; Check: not Is64BitInstallMode
    57 ;Source: "{#MyAppSubDir}\Languages\*.po"; DestDir: "{app}\Languages"; Flags: ignoreversion
     57Source: "{#MyAppSubDir}\Languages\*.po"; DestDir: "{app}\Languages"; Flags: ignoreversion
    5858
    5959
  • trunk/Languages/Game2048.cs.po

    r14 r19  
    9999
    100100#: tformsettings.label1.caption
    101 msgid "Animation speed:"
    102 msgstr "Rychlost animace:"
     101msgid "Animation duration:"
     102msgstr "Trvání animace:"
    103103
    104104#: tformsettings.label2.caption
  • trunk/Languages/Game2048.po

    r14 r19  
    8989
    9090#: tformsettings.label1.caption
    91 msgid "Animation speed:"
     91msgid "Animation duration:"
    9292msgstr ""
    9393
  • trunk/UCore.pas

    r17 r19  
    115115    CurrentContext := ApplicationInfo1.GetRegistryContext;
    116116
    117     Game.TopScore := ReadIntegerWithDefault('TopScore', 0);
    118     Game.Board.Size := Point(ReadIntegerWithDefault('SizeX', 4), ReadIntegerWithDefault('SizeY', 4));
    119     Game.AnimationDuration := ReadIntegerWithDefault('AnimationDuration', 30);
    120117    if ValueExists('LanguageCode') then
    121118      Translator1.Language := Translator1.Languages.SearchByCode(ReadStringWithDefault('LanguageCode', ''))
     
    124121    Free;
    125122  end;
     123  Game.LoadFromRegistry(ApplicationInfo1.GetRegistryContext);
    126124end;
    127125
     
    132130    CurrentContext := ApplicationInfo1.GetRegistryContext;
    133131
    134     WriteInteger('TopScore', Game.TopScore);
    135     WriteInteger('SizeX', Game.Board.Size.X);
    136     WriteInteger('SizeY', Game.Board.Size.Y);
    137     WriteInteger('AnimationDuration', Game.AnimationDuration);
    138132    if Assigned(Translator1.Language) and (Translator1.Language.Code <> '') then
    139133      WriteString('LanguageCode', Translator1.Language.Code)
     
    142136    Free;
    143137  end;
     138  Game.SaveToRegistry(ApplicationInfo1.GetRegistryContext);
    144139end;
    145140
  • trunk/UGame.pas

    r17 r19  
    77uses
    88  Classes, SysUtils, Dialogs, fgl, Graphics, Types, Forms, Math, DateUtils,
    9   Controls;
     9  Controls, URegistry;
    1010
    1111type
     
    3939    function GetEmptyTilesCount: Integer;
    4040    procedure GetEmptyTiles(EmptyTiles: TTiles);
     41    procedure SaveToRegistry(RegContext: TRegistryContext);
     42    procedure LoadFromRegistry(RegContext: TRegistryContext);
    4143    destructor Destroy; override;
    4244    property Size: TPoint read FSize write SetSize;
     
    7274    procedure MoveTile(SourceTile, TargetTile: TTile);
    7375    function IsValidPos(Pos: TPoint): Boolean;
     76    procedure SaveToRegistry(RegContext: TRegistryContext);
     77    procedure LoadFromRegistry(RegContext: TRegistryContext);
    7478    constructor Create;
    7579    destructor Destroy; override;
     
    131135      if Tiles[Y, X].Value = 0 then
    132136        EmptyTiles.Add(Tiles[Y, X]);
     137end;
     138
     139procedure TBoard.SaveToRegistry(RegContext: TRegistryContext);
     140var
     141  X, Y: Integer;
     142  Value: string;
     143begin
     144  with TRegistryEx.Create do
     145  try
     146    CurrentContext := RegContext;
     147
     148    Value := '';
     149    for Y := 0 to Size.Y - 1 do begin
     150      for X := 0 to Size.X - 1 do begin
     151        Value := Value + IntToStr(Tiles[Y, X].Value);
     152        if X < Size.X - 1 then Value := Value + ',';
     153      end;
     154      if Y < Size.Y - 1 then Value := Value + ';'
     155    end;
     156    WriteString('TileValues', Value);
     157  finally
     158    Free;
     159  end;
     160end;
     161
     162procedure TBoard.LoadFromRegistry(RegContext: TRegistryContext);
     163var
     164  X, Y: Integer;
     165  Items: TStringList;
     166  Lines: TStringList;
     167  Number: Integer;
     168begin
     169  with TRegistryEx.Create do
     170  try
     171    CurrentContext := RegContext;
     172
     173    Items := TStringList.Create;
     174    Items.Delimiter := ',';
     175    Lines := TStringList.Create;
     176    Lines.Delimiter := ';';
     177    Lines.DelimitedText := ReadStringWithDefault('TileValues', '');
     178    for Y := 0 to Lines.Count - 1 do begin
     179      Items.DelimitedText := Lines[Y];
     180      for X := 0 to Items.Count - 1 do begin
     181        if TryStrToInt(Items[X], Number) and (X < Size.X) and (Y < Size.Y) then
     182          Tiles[Y, X].Value := Number;
     183      end;
     184    end;
     185  finally
     186    Free;
     187  end;
    133188end;
    134189
     
    497552end;
    498553
     554procedure TGame.SaveToRegistry(RegContext: TRegistryContext);
     555begin
     556  with TRegistryEx.Create do
     557  try
     558    CurrentContext := RegContext;
     559
     560    WriteInteger('TopScore', TopScore);
     561    WriteInteger('AnimationDuration', AnimationDuration);
     562    WriteInteger('SizeX', Board.Size.X);
     563    WriteInteger('SizeY', Board.Size.Y);
     564    WriteInteger('Score', Score);
     565    WriteBool('GameRunning', FRunning);
     566    WriteBool('Won', Won);
     567  finally
     568    Free;
     569  end;
     570  Board.SaveToRegistry(RegContext);
     571end;
     572
     573procedure TGame.LoadFromRegistry(RegContext: TRegistryContext);
     574begin
     575  with TRegistryEx.Create do
     576  try
     577    CurrentContext := RegContext;
     578    Board.Size := Point(ReadIntegerWithDefault('SizeX', 4), ReadIntegerWithDefault('SizeY', 4));
     579    AnimationDuration := ReadIntegerWithDefault('AnimationDuration', 30);
     580    TopScore := ReadIntegerWithDefault('TopScore', 0);
     581    Score := ReadIntegerWithDefault('Score', 0);
     582    FRunning := ReadBoolWithDefault('GameRunning', False);
     583    Won := ReadBoolWithDefault('Won', False);
     584  finally
     585    Free;
     586  end;
     587  Board.LoadFromRegistry(RegContext);
     588end;
     589
    499590constructor TGame.Create;
    500591begin
Note: See TracChangeset for help on using the changeset viewer.