Changeset 342 for trunk


Ignore:
Timestamp:
Dec 22, 2024, 10:52:26 AM (10 hours ago)
Author:
chronos
Message:
  • Added: More tests.
Location:
trunk
Files:
7 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/Game.pas

    r339 r342  
    66  Classes, SysUtils, ExtCtrls, Graphics, XMLConf, XMLRead, XMLWrite, Forms,
    77  DOM, Math, LazFileUtils, XML, Dialogs, LCLType, LCLIntf, Building, Geometry,
    8   Player, Map, MapType, &Unit, GameSystem;
     8  Player, Map, MapType, Units, GameSystem;
    99
    1010const
     
    4949    procedure SetRunning(AValue: Boolean);
    5050    procedure BuildTerrain;
     51    procedure PlaceUnits;
    5152    procedure PlaceCities;
    5253    procedure InitPlayers;
     
    9192    procedure LoadFromFile(FileName: string);
    9293    procedure SaveToFile(FileName: string);
     94    function ToString: string; override;
    9395    procedure ComputePlayerStats;
    9496    procedure NextPlayer;
     
    301303    (Map.IsOutsideShape(PosPx)) then Terrain := ttVoid
    302304      else Terrain := ttNormal;
     305  end;
     306end;
     307
     308procedure TGame.PlaceUnits;
     309var
     310  Cell: TCell;
     311  NewPower: Integer;
     312begin
     313  if GameSystem.UnitKinds.Count = 0 then Exit;
     314
     315  for Cell in Map.Cells do
     316  with Cell do begin
    303317    NewPower := Random(MaxNeutralUnits + 1);
    304318    if (NewPower > 0) and not Assigned(OneUnit) then begin
     
    348362        end;
    349363        StartCell.Player := Player;
    350         if not Assigned(StartCell.OneUnit) then
    351           StartCell.OneUnit := Self.Units.AddNew(TUnitKind(GameSystem.UnitKinds[0]), Player.StartUnits);
    352         StartCell.OneUnit.Power := Player.StartUnits;
    353         StartCell.OneUnit.Kind := TUnitKind(GameSystem.UnitKinds[0]);
    354         StartCell.OneUnit.Player := Player;
     364        if GameSystem.UnitKinds.Count > 0 then begin
     365          if not Assigned(StartCell.OneUnit) then
     366            StartCell.OneUnit := Self.Units.AddNew(TUnitKind(GameSystem.UnitKinds[0]), Player.StartUnits);
     367          StartCell.OneUnit.Power := Player.StartUnits;
     368          StartCell.OneUnit.Kind := TUnitKind(GameSystem.UnitKinds[0]);
     369          StartCell.OneUnit.Player := Player;
     370        end;
    355371      end;
    356372    end;
     
    694710end;
    695711
     712function TGame.ToString: string;
     713begin
     714  Result := 'StoredRandSeed: ' + IntToStr(StoredRandSeed) + LineEnding;
     715  Result := Result + 'MapType: ' + IntToStr(Integer(MapType)) + LineEnding;
     716  Result := Result + 'SymetricMap: ' + BoolToStr(SymetricMap) + LineEnding;
     717  Result := Result + 'CyclicMap: ' + BoolToStr(CyclicMap) + LineEnding;
     718  Result := Result + 'FogOfWar: ' + BoolToStr(FogOfWar) + LineEnding;
     719  Result := Result + 'VoidEnabled: ' + BoolToStr(VoidEnabled) + LineEnding;
     720  Result := Result + 'VoidPercentage: ' + IntToStr(VoidPercentage) + LineEnding;
     721  Result := Result + 'MaxNeutralUnits: ' + IntToStr(MaxNeutralUnits) + LineEnding;
     722  Result := Result + 'MaxPower: ' + IntToStr(MaxPower) + LineEnding;
     723  Result := Result + 'GrowCells: ' + IntToStr(Integer(GrowCells)) + LineEnding;
     724  Result := Result + 'GrowAmount: ' + IntToStr(Integer(GrowAmount)) + LineEnding;
     725  Result := Result + 'CityEnabled: ' + BoolToStr(CityEnabled) + LineEnding;
     726  Result := Result + 'CityPercentage: ' + IntToStr(CityPercentage) + LineEnding;
     727  Result := Result + 'BridgeEnabled: ' + BoolToStr(BridgeEnabled) + LineEnding;
     728  Result := Result + 'TurnCounter: ' + IntToStr(TurnCounter) + LineEnding;
     729  Result := Result + 'WinObjective: ' + IntToStr(Integer(WinObjective)) + LineEnding;
     730  Result := Result + 'StayAliveForDefinedTurns: ' + IntToStr(StayAliveForDefinedTurns) + LineEnding;
     731  Result := Result + 'Running: ' + BoolToStr(Running) + LineEnding;
     732  Result := Result + 'GameSystem: ' + LineEnding + GameSystem.ToString + LineEnding;
     733  Result := Result + 'Map: ' + LineEnding + Map.ToString + LineEnding;
     734  Result := Result + 'Players: ' + LineEnding + Players.ToString + LineEnding;
     735  Result := Result + 'Units: ' + LineEnding + Units.ToString + LineEnding;
     736end;
     737
    696738procedure TGame.ComputePlayerStats;
    697739var
     
    905947  Map.MaxPower := MaxPower;
    906948  BuildTerrain;
     949  PlaceUnits;
    907950  PlaceCities;
    908951  WinObjectiveMapPrepare;
     
    931974function TGame.Compare(Game: TGame): Boolean;
    932975begin
    933   Result := BridgeEnabled = Game.BridgeEnabled;
     976  Result := (StoredRandSeed = Game.StoredRandSeed) and
     977    (DevelMode = Game.DevelMode) and
     978    Players.Compare(Game.Players) and
     979    (MapType = Game.MapType) and
     980    Map.Compare(Game.Map) and
     981    (MapImageFileName = Game.MapImageFileName) and
     982    (VoidEnabled = Game.VoidEnabled) and
     983    (VoidPercentage = Game.VoidPercentage) and
     984    (SymetricMap = Game.SymetricMap) and
     985    (CyclicMap = Game.CyclicMap) and
     986    (GrowCells = Game.GrowCells) and
     987    (GrowAmount = Game.GrowAmount) and
     988    (CityEnabled = Game.CityEnabled) and
     989    (CityPercentage = Game.CityPercentage) and
     990    (TurnCounter = Game.TurnCounter) and
     991    (WinObjective = Game.WinObjective) and
     992    (SpecialCaptureCellCount = Game.SpecialCaptureCellCount) and
     993    (StayAliveForDefinedTurns = Game.StayAliveForDefinedTurns) and
     994    (MaxNeutralUnits = Game.MaxNeutralUnits) and
     995    (FileName = Game.FileName) and
     996    (FogOfWar = Game.FogOfWar) and
     997    (BridgeEnabled = Game.BridgeEnabled) and
     998    (MaxPower = Game.MaxPower) and
     999    GameSystem.Compare(Game.GameSystem);
    9341000end;
    9351001
  • trunk/GameSystem.pas

    r339 r342  
    44
    55uses
    6   Classes, SysUtils, Generics.Collections, &Unit, DOM, XMLRead, XMLWrite, XML,
     6  Classes, SysUtils, Generics.Collections, Units, DOM, XMLRead, XMLWrite, XML,
    77  XMLConf, FileUtil, LazFileUtils, MapType, Nation, Building, ItemList, UnitKind;
    88
     
    2727    function GetName: string;
    2828    procedure Assign(Source: TGameSystem);
     29    function Compare(GameSystem: TGameSystem): Boolean;
    2930    procedure LoadFromNode(Node: TDOMNode);
    3031    procedure SaveToNode(Node: TDOMNode);
     
    133134  BuildingKinds.Assign(Source.BuildingKinds);
    134135  Nations.Assign(Source.Nations);
     136end;
     137
     138function TGameSystem.Compare(GameSystem: TGameSystem): Boolean;
     139begin
     140  Result := (FileName = GameSystem.FileName) and
     141    (UnitsMoveImmediately = GameSystem.UnitsMoveImmediately) and
     142    (MaxPlayerCount = GameSystem.MaxPlayerCount) and
     143    (UnitsSplitMerge = GameSystem.UnitsSplitMerge) and
     144    (EmptyCellsNeutral = GameSystem.EmptyCellsNeutral) and
     145    (PreferedMapType = GameSystem.PreferedMapType); // and
     146    //(UnitKinds.Assign(GameSystem.UnitKinds) and
     147    //(BuildingKinds.Assign(GameSystem.BuildingKinds) and
     148    //(Nations.Assign(GameSystem.Nations);
    135149end;
    136150
  • trunk/ItemList.pas

    r336 r342  
    4141  private
    4242    procedure AssignValue(Source: TItem; Field: TItemField);
     43    function CompareValue(Item: TItem; Field: TItemField): Boolean;
    4344    procedure LoadValueFromNode(Node: TDOMNode; Field: TItemField); virtual;
    4445    procedure SaveValueToNode(Node: TDOMNode; Field: TItemField); virtual;
     
    6465    procedure SetValueReference(Index: Integer; Value: TItem);
    6566    procedure Assign(Source: TItem); virtual;
     67    function Compare(Item: TItem): Boolean; virtual;
     68    function ToString: string; override;
    6669    procedure LoadFromNode(Node: TDOMNode); virtual;
    6770    procedure SaveToNode(Node: TDOMNode); virtual;
     
    8891    function FindByName(Name: string): TItem;
    8992    function GetNewId: Integer;
     93    function ToString: string; override;
    9094    procedure Assign(Source: TItemList); virtual;
     95    function Compare(ItemList: TItemList): Boolean; virtual;
    9196    function AddItem(Name: string = ''): TItem; virtual;
    9297    function CreateItem(Name: string = ''): TItem; virtual;
     
    150155end;
    151156
     157function TItemList.Compare(ItemList: TItemList): Boolean;
     158var
     159  I: Integer;
     160begin
     161  Result := Count = ItemList.Count;
     162  if not Result then Exit;
     163  for I := 0 to Count - 1 do begin
     164    Result := Result and TItem(Items[I]).Compare(ItemList.Items[I]);
     165    if not Result then Break;
     166  end;
     167end;
     168
    152169function TItemList.AddItem(Name: string): TItem;
    153170begin
     
    269286end;
    270287
     288function TItemList.ToString: string;
     289var
     290  I: Integer;
     291begin
     292  Result := '';
     293  for I := 0 to Count - 1 do
     294  with TItem(Items[I]) do begin
     295    Result := Result + ToString + LineEnding;
     296  end;
     297end;
     298
    271299{ TItemFields }
    272300
     
    312340  if Field.DataType = dtReference then begin
    313341    SetValueReference(Field.Index, Source.GetValueReference(Field.Index));
     342  end else
     343    raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
     344end;
     345
     346function TItem.CompareValue(Item: TItem; Field: TItemField): Boolean;
     347begin
     348  if Field.DataType = dtString then begin
     349    Result := GetValueString(Field.Index) = Item.GetValueString(Field.Index);
     350  end else
     351  if Field.DataType = dtColor then begin
     352    Result := GetValueColor(Field.Index) = Item.GetValueColor(Field.Index);
     353  end else
     354  if Field.DataType = dtInteger then begin
     355    Result := GetValueInteger(Field.Index) = Item.GetValueInteger(Field.Index);
     356  end else
     357  if Field.DataType = dtBoolean then begin
     358    Result := GetValueBoolean(Field.Index) = Item.GetValueBoolean(Field.Index);
     359  end else
     360  if Field.DataType = dtEnumeration then begin
     361    Result := GetValueEnumeration(Field.Index) = Item.GetValueEnumeration(Field.Index);
     362  end else
     363  if Field.DataType = dtReference then begin
     364    Result := GetValueReference(Field.Index) = Item.GetValueReference(Field.Index);
    314365  end else
    315366    raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
     
    503554end;
    504555
     556function TItem.Compare(Item: TItem): Boolean;
     557var
     558  I: Integer;
     559  Fields: TItemFields;
     560begin
     561  Result := True;
     562  Result := Result and (Id = Item.Id);
     563  if Item is ClassType then begin
     564    Fields := GetFields;
     565    try
     566      for I := 0 to Fields.Count - 1 do begin
     567        Result := Result and CompareValue(Item, Fields[I]);
     568        if not Result then Break;
     569      end;
     570    finally
     571      Fields.Free;
     572    end;
     573  end;
     574end;
     575
     576function TItem.ToString: string;
     577var
     578  Fields: TItemFields;
     579  I: Integer;
     580begin
     581  Result := 'Id: ' + IntToStr(Id) + LineEnding;
     582  Fields := GetFields;
     583  try
     584    for I := 0 to Fields.Count - 1 do begin
     585      Result := Result + Fields[I].SysName + ': ' + GetValueAsText(Fields[I].Index) + LineEnding;
     586    end;
     587  finally
     588    Fields.Free;
     589  end;
     590end;
     591
    505592procedure TItem.LoadFromNode(Node: TDOMNode);
    506593var
  • trunk/Map.pas

    r338 r342  
    55uses
    66  Classes, SysUtils, Graphics, ExtCtrls, Geometry, DOM, Generics.Collections,
    7   Generics.Defaults, Building, XML, &Unit;
     7  Generics.Defaults, Building, XML, Units;
    88
    99const
     
    162162    function IsValidIndex(Index: TPoint): Boolean; virtual;
    163163    procedure Assign(Source: TMap); virtual;
     164    function Compare(Map: TMap): Boolean;
    164165    procedure LoadFromFile(FileName: string); virtual;
    165166    procedure SaveToFile(FileName: string); virtual;
     
    539540end;
    540541
     542function TMap.Compare(Map: TMap): Boolean;
     543begin
     544  Result := True;
     545end;
     546
    541547procedure TMap.LoadFromFile(FileName: string);
    542548begin
  • trunk/Player.pas

    r339 r342  
    55uses
    66  Classes, SysUtils, Graphics, Map, DOM, Generics.Collections, Generics.Defaults,
    7   XMLConf, XML, Math, Geometry, &Unit, Nation, ItemList, TurnStats;
     7  XMLConf, XML, Math, Geometry, Units, Nation, ItemList, TurnStats;
    88
    99type
  • trunk/Tests.pas

    r336 r342  
    1717  end;
    1818
     19  { TTestCaseMap }
     20
     21  TTestCaseMap = class(TTestCase)
     22    Game: TGame;
     23    procedure Run; override;
     24    constructor Create; override;
     25    destructor Destroy; override;
     26  end;
     27
    1928function GetTestCases: TTestCases;
    2029
    2130
    2231implementation
     32
     33uses
     34  Geometry, MapType, Map;
    2335
    2436function GetTestCases: TTestCases;
     
    2739  with Result do begin
    2840    with TTestCaseGame(AddNew('Load and save', TTestCaseGame)) do begin
    29 
     41    end;
     42    with TTestCaseMap(AddNew('Map cells connection', TTestCaseMap)) do begin
    3043    end;
    3144  end;
     
    4457  Game2.LoadFromFile(FileName);
    4558  Evaluate(Game2.Compare(Game));
     59  Log := 'Game1: ' + LineEnding + Game.ToString + LineEnding +
     60    'Game2: ' + LineEnding + Game2.ToString + LineEnding;
    4661  Game2.Free;
    4762end;
     
    5974end;
    6075
     76{ TTestCaseMap }
     77
     78procedure TTestCaseMap.Run;
     79var
     80  X, Y: Integer;
     81  Connected: Boolean;
     82  Cell: TCell;
     83  Expected: Integer;
     84const
     85  Size = 10;
     86begin
     87  Connected := True;
     88  Game.Map.Size := TPoint.Create(Size, Size);
     89  Game.MapType := mtSquare;
     90  Game.New;
     91  for Y := 0 to Size - 1 do
     92  for X := 0 to Size - 1 do begin
     93    Cell := Game.Map.Cells[X + Y * Size];
     94    Expected := 4;
     95    if (X = 0) or (X = Size - 1) then Dec(Expected);
     96    if (Y = 0) or (Y = Size - 1) then Dec(Expected);
     97    if Cell.Neighbors.Count <> Expected then begin
     98      Log := Log + 'Cell ' + IntToStr(X + Y * Size) + ' expected neighbors count ' +
     99        IntToStr(Expected) + ' but is ' + IntToStr(Cell.Neighbors.Count) + LineEnding;
     100      if not Connected then Break;
     101    end;
     102  end;
     103  Evaluate(Connected);
     104end;
     105
     106constructor TTestCaseMap.Create;
     107begin
     108  inherited;
     109  Game := TGame.Create;
     110end;
     111
     112destructor TTestCaseMap.Destroy;
     113begin
     114  FreeAndNil(Game);
     115  inherited;
     116end;
     117
    61118end.
    62119
  • trunk/Units.pas

    r341 r342  
    1 unit &Unit;
     1unit Units;
    22
    33interface
  • trunk/xtactics.lpi

    r340 r342  
    260260      </Unit26>
    261261      <Unit27>
    262         <Filename Value="Unit.pas"/>
    263         <IsPartOfProject Value="True"/>
    264         <UnitName Value="&amp;Unit"/>
     262        <Filename Value="Units.pas"/>
     263        <IsPartOfProject Value="True"/>
    265264      </Unit27>
    266265      <Unit28>
Note: See TracChangeset for help on using the changeset viewer.