Changeset 66 for trunk/UGame.pas


Ignore:
Timestamp:
Sep 28, 2014, 11:51:26 AM (10 years ago)
Author:
chronos
Message:
  • Modified: Computer strategy divided to Attack and InnerMoves.
  • Modified: Computer attack lower power cells first.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r65 r66  
    5959
    6060  TCellArray = array of TCell;
     61  TCells = class(TObjectList)
     62
     63  end;
    6164
    6265  { TView }
     
    201204    Game: TGame;
    202205    Targets: TObjectList;
     206    procedure Attack;
     207    procedure InnerMoves;
    203208    procedure Process;
    204209  end;
     
    11131118end;
    11141119
    1115 procedure TComputer.Process;
     1120function CellCompare(Item1, Item2: Pointer): Integer;
     1121begin
     1122  if TCell(Item1).Power > TCell(Item2).Power then Result := 1
     1123  else if TCell(Item1).Power < TCell(Item2).Power then Result := -1
     1124  else Result := 0;
     1125end;
     1126
     1127procedure TComputer.Attack;
    11161128var
    11171129  AllCells: TObjectList;
    11181130  Cells, Cells2: TCellArray;
    1119   X, Y: Integer;
    11201131  TotalPower: Integer;
    11211132  AttackPower: Integer;
     
    11241135  C: Integer;
    11251136  CanAttack, CanAttack2: Integer;
     1137  TargetCells: TCells;
     1138  S: string;
    11261139const
    11271140  AttackDiff = 2;
    11281141begin
    11291142  AllCells := Game.Map.Cells;
     1143  TargetCells := TCells.Create;
     1144  TargetCells.OwnsObjects := False;
     1145
     1146  // Get list of all attack target cells
    11301147  for C := 0 to AllCells.Count - 1 do
    11311148  with TCell(AllCells[C]) do begin
    11321149    if (Terrain <> ttVoid) and (Player <> Game.CurrentPlayer) then begin
     1150      Cells := Game.Map.GetCellNeighbors(TCell(AllCells[C]));
     1151      CanAttack := 0;
     1152      for I := 0 to Length(Cells) - 1 do
     1153      if (Cells[I].Player = Game.CurrentPlayer) then begin
     1154        Inc(CanAttack);
     1155      end;
     1156      if CanAttack > 0 then TargetCells.Add(AllCells[C]);
     1157    end;
     1158  end;
     1159
     1160  // Sort ascending to attack cells with lower power first
     1161  TargetCells.Sort(CellCompare);
     1162
     1163  for C := 0 to TargetCells.Count - 1 do
     1164  with TCell(TargetCells[C]) do begin
    11331165      // Attack to not owned cell yet
    11341166      // Count own possible power
    1135       Cells := Game.Map.GetCellNeighbors(TCell(AllCells[C]));
     1167      Cells := Game.Map.GetCellNeighbors(TCell(TargetCells[C]));
    11361168      TotalPower := 0;
    11371169      for I := 0 to Length(Cells) - 1 do
     
    11481180          if Cells[I].GetAvialPower < AttackPower then
    11491181            AttackPower := Cells[I].GetAvialPower;
    1150           Game.SetMove(Cells[I], TCell(AllCells[C]), AttackPower);
     1182          Game.SetMove(Cells[I], TCell(TargetCells[C]), AttackPower);
    11511183          TotalAttackPower := TotalAttackPower + AttackPower;
    11521184        end;
    11531185      end;
    1154     end else
    1155     if (Terrain <> ttVoid) and (Player = Game.CurrentPlayer) then begin
     1186  end;
     1187
     1188  TargetCells.Free;
     1189end;
     1190
     1191procedure TComputer.InnerMoves;
     1192var
     1193  AllCells: TObjectList;
     1194  Cells, Cells2: TCellArray;
     1195  I, J: Integer;
     1196  C: Integer;
     1197  CanAttack, CanAttack2: Integer;
     1198begin
     1199  AllCells := Game.Map.Cells;
     1200  for C := 0 to AllCells.Count - 1 do
     1201  with TCell(AllCells[C]) do begin
     1202      if (Terrain <> ttVoid) and (Player = Game.CurrentPlayer) then begin
    11561203      // Inner moves
    11571204      // We need to move available power to borders to be available for attacks
     
    11881235end;
    11891236
     1237procedure TComputer.Process;
     1238begin
     1239  Attack;
     1240  InnerMoves;
     1241end;
     1242
    11901243procedure TView.SelectCell(Pos: TPoint; Player: TPlayer);
    11911244var
Note: See TracChangeset for help on using the changeset viewer.