Changeset 105 for trunk


Ignore:
Timestamp:
Dec 9, 2024, 4:40:34 PM (5 weeks ago)
Author:
chronos
Message:
  • Added: Set number of randomly generated disabled tiles in new game form.
Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Board.pas

    r104 r105  
    6969  for Y := 0 to Size.Y - 1 do
    7070    for X := 0 to Size.X - 1 do
    71       if Tiles[Y, X].Value = 0 then
     71      if (Tiles[Y, X].Value = 0) and not Tiles[Y, X].Disabled then
    7272        EmptyTiles.Add(Tiles[Y, X]);
    7373end;
     
    7676var
    7777  X, Y: Integer;
    78   Value: string;
    7978begin
    8079  with Reg do begin
     
    8382    WriteInteger('SizeX', Size.X);
    8483    WriteInteger('SizeY', Size.Y);
    85     Value := '';
    8684    for Y := 0 to Size.Y - 1 do begin
    8785      for X := 0 to Size.X - 1 do begin
    88         Value := Value + IntToStr(Tiles[Y, X].Value);
    89         if X < Size.X - 1 then Value := Value + ',';
     86        Tiles[Y, X].SaveToRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Tile' + IntToStr(X) + 'x' + IntToStr(Y)));
    9087      end;
    91       if Y < Size.Y - 1 then Value := Value + ';'
    9288    end;
    93     WriteString('TileValues', Value);
    9489  end;
    9590end;
     
    9893var
    9994  X, Y: Integer;
    100   Items: TStringList;
    101   Lines: TStringList;
    102   Number: Integer;
    10395begin
    10496  with Reg do begin
    10597    CurrentContext := RegContext;
    10698
    107     Size := Point(ReadIntegerWithDefault('SizeX', 4), ReadIntegerWithDefault('SizeY', 4));
    108     Items := TStringList.Create;
    109     Items.Delimiter := ',';
    110     Lines := TStringList.Create;
    111     Lines.Delimiter := ';';
    112     Lines.DelimitedText := ReadStringWithDefault('TileValues', '');
    113     for Y := 0 to Lines.Count - 1 do begin
    114       Items.DelimitedText := Lines[Y];
    115       for X := 0 to Items.Count - 1 do begin
    116         if TryStrToInt(Items[X], Number) and (X < Size.X) and (Y < Size.Y) then
    117           Tiles[Y, X].Value := Number;
     99    Size := Point(ReadIntegerWithDefault('SizeX', 4),
     100      ReadIntegerWithDefault('SizeY', 4));
     101    for Y := 0 to Size.Y - 1 do begin
     102      for X := 0 to Size.X - 1 do begin
     103        Tiles[Y, X].LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Tile' + IntToStr(X) + 'x' + IntToStr(Y)));
    118104      end;
    119105    end;
    120     Lines.Free;
    121     Items.Free;
    122106  end;
    123107end;
     
    155139  for Y := 0 to Size.Y - 1 do
    156140    for X := 0 to Size.X - 1 do
    157       if Tiles[Y, X].Value = 0 then
     141      if (Tiles[Y, X].Value = 0) and not Tiles[Y, X].Disabled then
    158142        Inc(Result);
    159143end;
     
    175159  for Y := 0 to Size.Y - 1 do
    176160    for X := 0 to Size.X - 1 do
    177       Tiles[Y, X].Value := 0;
     161      Tiles[Y, X].Clear;
    178162end;
    179163
  • trunk/Forms/FormNew.lfm

    r103 r105  
    3535    Width = 497
    3636    HorzScrollBar.Page = 435
    37     VertScrollBar.Page = 118
     37    VertScrollBar.Page = 160
    3838    Anchors = [akTop, akLeft, akRight, akBottom]
    3939    ClientHeight = 245
     
    6969      Style = csDropDownList
    7070      TabOrder = 0
     71      OnChange = ComboBoxSizeChange
    7172    end
    7273    object CheckBoxUndoEnabled: TCheckBox
     
    8687      TabOrder = 2
    8788    end
     89    object Label2: TLabel
     90      Left = 16
     91      Height = 26
     92      Top = 122
     93      Width = 119
     94      Caption = 'Disabled tiles:'
     95    end
     96    object SpinEditDisabledTiles: TSpinEdit
     97      Left = 320
     98      Height = 43
     99      Top = 117
     100      Width = 115
     101      MaxValue = 10000
     102      TabOrder = 3
     103    end
    88104  end
    89105end
  • trunk/Forms/FormNew.lrj

    r103 r105  
    55{"hash":103901194,"name":"tformnew.label1.caption","sourcebytes":[66,111,97,114,100,32,115,105,122,101,58],"value":"Board size:"},
    66{"hash":260260820,"name":"tformnew.checkboxundoenabled.caption","sourcebytes":[85,110,100,111,32,101,110,97,98,108,101,100],"value":"Undo enabled"},
    7 {"hash":146862089,"name":"tformnew.checkboxrecordhistory.caption","sourcebytes":[82,101,99,111,114,100,32,109,111,118,101,115,32,104,105,115,116,111,114,121],"value":"Record moves history"}
     7{"hash":146862089,"name":"tformnew.checkboxrecordhistory.caption","sourcebytes":[82,101,99,111,114,100,32,109,111,118,101,115,32,104,105,115,116,111,114,121],"value":"Record moves history"},
     8{"hash":248990730,"name":"tformnew.label2.caption","sourcebytes":[68,105,115,97,98,108,101,100,32,116,105,108,101,115,58],"value":"Disabled tiles:"}
    89]}
  • trunk/Forms/FormNew.pas

    r103 r105  
    55uses
    66  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    7   Game, FormEx;
     7  Spin, Game, FormEx, Math;
    88
    99type
     
    1818    ComboBoxSize: TComboBox;
    1919    Label1: TLabel;
     20    Label2: TLabel;
    2021    ScrollBox1: TScrollBox;
     22    SpinEditDisabledTiles: TSpinEdit;
     23    procedure ComboBoxSizeChange(Sender: TObject);
    2124  public
     25    procedure UpdateInterface;
    2226    procedure Load(Game: TGame);
    2327    procedure Save(Game: TGame);
     
    3034
    3135{ TFormNew }
     36
     37procedure TFormNew.ComboBoxSizeChange(Sender: TObject);
     38begin
     39  UpdateInterface;
     40end;
     41
     42procedure TFormNew.UpdateInterface;
     43begin
     44  SpinEditDisabledTiles.MaxValue := ComboBoxSize.ItemIndex + 2;
     45end;
     46
    3247procedure TFormNew.Load(Game: TGame);
    3348begin
     
    3550  CheckBoxUndoEnabled.Checked := Game.UndoEnabled;
    3651  CheckBoxRecordHistory.Checked := Game.RecordHistory;
     52  SpinEditDisabledTiles.Value := Game.DisabledTilesCount;
     53  UpdateInterface;
    3754end;
    3855
     
    4259  Game.UndoEnabled := CheckBoxUndoEnabled.Checked;
    4360  Game.RecordHistory := CheckBoxRecordHistory.Checked;
     61  Game.DisabledTilesCount := SpinEditDisabledTiles.Value;
    4462end;
    4563
  • trunk/Game.pas

    r104 r105  
    3939    FColorPalette: TColorPalette;
    4040    FSkin: TTileSkin;
     41    FDisabledTilesCount: Integer;
    4142    function GetTileColor(Value: Integer): TColor;
    4243    procedure SetRecordHistory(AValue: Boolean);
     
    5253    procedure Win;
    5354    function FillRandomTile: TTile;
     55    function DisableRandomTile: TTile;
    5456    function GetMoveArea(Direction: TMoveDirection): TArea;
    5557    procedure MoveAllAnimate(Direction: TMoveDirection);
     
    9698    property Skin: TTileSkin read FSkin write SetSkin;
    9799    property ColorPalette: TColorPalette read FColorPalette write SetColorPalette;
     100    property DisabledTilesCount: Integer read FDisabledTilesCount write FDisabledTilesCount;
    98101  end;
    99102
     
    225228    Result.Value := NewValue;
    226229    Result.Action := taAppear;
     230  end;
     231  EmptyTiles.Free;
     232end;
     233
     234function TGame.DisableRandomTile: TTile;
     235var
     236  EmptyTiles: TTiles;
     237begin
     238  Result := nil;
     239  EmptyTiles := TTiles.Create(False);
     240  Board.GetEmptyTiles(EmptyTiles);
     241  if EmptyTiles.Count > 0 then begin
     242    Result := EmptyTiles[Random(EmptyTiles.Count)];
     243    Result.Disabled := True;
    227244  end;
    228245  EmptyTiles.Free;
     
    276293  Running := True;
    277294  History.Clear;
     295  for I := 0 to DisabledTilesCount - 1 do
     296    DisableRandomTile;
     297
    278298  if RecordHistory then begin
    279299    for I := 0 to InitialTileCount - 1 do begin
     
    365385      if (Board.Tiles[Y, X].Action <> taNone) then MetaCanvas.Brush.Color := GetTileColor(0)
    366386        else MetaCanvas.Brush.Color := GetTileColor(Board.Tiles[Y, X].Value);
    367       MetaCanvas.Brush.Style := bsSolid;
     387      if Board.Tiles[Y, X].Disabled then MetaCanvas.Brush.Style := bsClear
     388        else MetaCanvas.Brush.Style := bsSolid;
    368389      TileRect := Bounds(
    369390        Frame.Left + X * TileSize.X + TileMargin,
     
    459480      while P.X <> Area.P2.X + Area.Increment.X do begin
    460481        PNew := P + DirectionDiff[Direction];
    461         if IsValidPos(PNew) then begin
     482        if IsValidPos(PNew) and not Board.Tiles[PNew.Y, PNew.X].Disabled then begin
    462483          SrcTile := Board.Tiles[P.Y, P.X];
    463484          DstTile := Board.Tiles[PNew.Y, PNew.X];
     
    496517  TextSize: TSize;
    497518begin
     519  if Canvas.Brush.Style = bsClear then Exit;
     520
    498521  Canvas.Pen.Style := psClear;
    499522  Canvas.RoundRect(TileRect, ScaleX(TileRect.Width div 20, 96), ScaleY(TileRect.Height div 20, 96));
    500   if WithText and (Tile.Value <> 0) then begin
     523  if (WithText and (Tile.Value <> 0)) then begin
    501524    ValueStr := GetTileSkinValue(Tile.Value);
    502525    Canvas.Brush.Style := bsClear;
     
    626649      if IsValidPos(PNew) then begin
    627650        if (Board.Tiles[P.Y, P.X].Value <> 0) then begin
    628           if (Board.Tiles[PNew.Y, PNew.X].Value = 0) or
    629           CanMergeTile(Board.Tiles[PNew.Y, PNew.X].Value, Board.Tiles[P.Y, P.X].Value) then begin
     651          if ((Board.Tiles[PNew.Y, PNew.X].Value = 0) or
     652          CanMergeTile(Board.Tiles[PNew.Y, PNew.X].Value, Board.Tiles[P.Y, P.X].Value)) and
     653          not Board.Tiles[PNew.Y, PNew.X].Disabled then begin
    630654            Result := True;
    631655            Break;
     
    673697      while P.X <> Area.P2.X + Area.Increment.X do begin
    674698        PNew := P + DirectionDiff[Direction];
    675         if IsValidPos(PNew) then begin
     699        if IsValidPos(PNew) and not Board.Tiles[PNew.Y, PNew.X].Disabled then begin
    676700          if (Board.Tiles[P.Y, P.X].NewValue <> 0) then begin
    677701            if (Board.Tiles[PNew.Y, PNew.X].NewValue = 0) then begin
     
    934958    WriteInteger('Skin', Integer(Skin));
    935959    WriteInteger('ColorPalette', Integer(ColorPalette));
     960    WriteInteger('DisabledTilesCount', DisabledTilesCount);
    936961    FBoardUndo.SaveToRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\BoardUndo'));
    937962    Board.SaveToRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Board'));
     
    959984    Skin := TTileSkin(ReadIntegerWithDefault('Skin', Integer(tsPowerOfTwo)));
    960985    ColorPalette := TColorPalette(ReadIntegerWithDefault('ColorPalette', Integer(cpOrangeYellow)));
     986    DisabledTilesCount := ReadIntegerWithDefault('DisabledTilesCount', DisabledTilesCount);
    961987    FBoardUndo.LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\BoardUndo'));
    962988    Board.LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Board'));
  • trunk/Languages/Game2048.cs.po

    r103 r105  
    280280msgstr "Velikost desky:"
    281281
     282#: tformnew.label2.caption
     283msgid "Disabled tiles:"
     284msgstr "Blokované dlaždice:"
     285
    282286#: tformsettings.buttoncancel.caption
    283287msgctxt "tformsettings.buttoncancel.caption"
  • trunk/Tile.pas

    r104 r105  
    44
    55uses
    6   Classes, SysUtils, Generics.Collections;
     6  Classes, SysUtils, Generics.Collections, RegistryEx;
    77
    88type
     
    1818    Action: TTileAction;
    1919    Shift: TPoint;
     20    Disabled: Boolean;
     21    procedure Clear;
    2022    procedure Assign(Source: TTile);
     23    procedure SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
     24    procedure LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
    2125  end;
    2226
     
    2933{ TTile }
    3034
     35procedure TTile.Clear;
     36begin
     37  Value := 0;
     38  Action := taNone;;
     39  Disabled := False;
     40end;
     41
    3142procedure TTile.Assign(Source: TTile);
    3243begin
     44  Index := Source.Index;
    3345  Value := Source.Value;
    3446  Merged := Source.Merged;
     47  Disabled := Source.Disabled;
    3548end;
    3649
     50procedure TTile.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
     51begin
     52  with Reg do begin
     53    CurrentContext := RegContext;
     54
     55    WriteInteger('Value', Value);
     56    WriteBool('Disabled', Disabled);
     57  end;
     58end;
     59
     60procedure TTile.LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext
     61  );
     62begin
     63  with Reg do begin
     64    CurrentContext := RegContext;
     65
     66    Value := ReadIntegerWithDefault('Value', Value);
     67    Disabled := ReadBoolWithDefault('Disabled', Disabled);
     68  end;
     69end;
    3770
    3871end.
Note: See TracChangeset for help on using the changeset viewer.