Changeset 19 for trunk/UGame.pas


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

Legend:

Unmodified
Added
Removed
  • 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.