Changeset 65 for trunk


Ignore:
Timestamp:
Sep 28, 2014, 12:07:12 AM (10 years ago)
Author:
chronos
Message:
  • Modified: Computer strategy as separate class TComputer.
  • Modified: Computer move more units to borders.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/UCore.pas

    r64 r65  
    133133
    134134procedure TCore.AGameEndTurnExecute(Sender: TObject);
     135var
     136  I: Integer;
     137  Computer: TComputer;
    135138begin
    136139  Game.NextTurn;
    137140  FormMain.Redraw;
    138141  while Game.Running and (Game.CurrentPlayer.Mode <> pmHuman) do begin
    139     if Game.CurrentPlayer.Mode = pmComputer then
    140       Game.CurrentPlayer.ComputerTurn;
     142    if Game.CurrentPlayer.Mode = pmComputer then begin
     143      Computer := TComputer.Create;
     144      Computer.Game := Game;
     145      Computer.Process;
     146      Computer.Free;
     147      FormMain.Redraw;
     148      for I := 0 to 10 do begin
     149        Application.ProcessMessages;
     150        Sleep(100);
     151      end;
     152    end;
    141153    Game.NextTurn;
    142154    FormMain.Redraw;
  • trunk/UGame.pas

    r64 r65  
    188188    procedure LoadFromNode(Node: TDOMNode);
    189189    procedure SaveToNode(Node: TDOMNode);
    190     procedure ComputerTurn;
    191190    procedure Paint(PaintBox: TPaintBox);
    192191    constructor Create;
     
    194193    procedure Assign(Source: TPlayer);
    195194    property Game: TGame read FGame write SetGame;
     195  end;
     196
     197
     198  { TComputer }
     199
     200  TComputer = class
     201    Game: TGame;
     202    Targets: TObjectList;
     203    procedure Process;
    196204  end;
    197205
     
    381389end;
    382390
     391{ TComputer }
     392
    383393{ TPlayers }
    384394
     
    11031113end;
    11041114
    1105 procedure TPlayer.ComputerTurn;
     1115procedure TComputer.Process;
    11061116var
    11071117  AllCells: TObjectList;
    1108   Cells: TCellArray;
     1118  Cells, Cells2: TCellArray;
    11091119  X, Y: Integer;
    11101120  TotalPower: Integer;
    11111121  AttackPower: Integer;
    11121122  TotalAttackPower: Integer;
    1113   I: Integer;
     1123  I, J: Integer;
    11141124  C: Integer;
    1115   CanAttack: Integer;
     1125  CanAttack, CanAttack2: Integer;
     1126const
     1127  AttackDiff = 2;
    11161128begin
    11171129  AllCells := Game.Map.Cells;
    11181130  for C := 0 to AllCells.Count - 1 do
    11191131  with TCell(AllCells[C]) do begin
    1120     if (Terrain <> ttVoid) and (Player <> Self) then begin
     1132    if (Terrain <> ttVoid) and (Player <> Game.CurrentPlayer) then begin
    11211133      // Attack to not owned cell yet
    11221134      // Count own possible power
     
    11241136      TotalPower := 0;
    11251137      for I := 0 to Length(Cells) - 1 do
    1126       if (Cells[I].Player = Self) then begin
     1138      if (Cells[I].Player = Game.CurrentPlayer) then begin
    11271139        TotalPower := TotalPower + Cells[I].GetAvialPower;
    11281140      end;
    11291141      // Attack if target is weaker
    1130       if TotalPower >= Power then begin
     1142      if TotalPower >= (Power + AttackDiff) then begin
    11311143        TotalAttackPower := 0;
    11321144        for I := 0 to Length(Cells) - 1 do
    1133         if (Cells[I].Player = Self) then begin
     1145        if (Cells[I].Player = Game.CurrentPlayer) then begin
    11341146          // Use only necessary power
    1135           AttackPower := Power - TotalAttackPower + 1;
     1147          AttackPower := Power - TotalAttackPower + AttackDiff;
    11361148          if Cells[I].GetAvialPower < AttackPower then
    11371149            AttackPower := Cells[I].GetAvialPower;
     
    11411153      end;
    11421154    end else
    1143     if (Terrain <> ttVoid) and (Player = Self) then begin
     1155    if (Terrain <> ttVoid) and (Player = Game.CurrentPlayer) then begin
    11441156      // Inner moves
    11451157      // We need to move available power to borders to be available for attacks
     
    11481160      CanAttack := 0;
    11491161      for I := 0 to Length(Cells) - 1 do
    1150       if (Cells[I].Player <> Self) and (Cells[I].Terrain <> ttVoid) then begin
     1162      if (Cells[I].Player <> Game.CurrentPlayer) and (Cells[I].Terrain <> ttVoid) then begin
    11511163        Inc(CanAttack);
    11521164      end;
     
    11551167        // For simplicty just try to balance inner area cells power
    11561168        for I := 0 to Length(Cells) - 1 do
    1157         if (Cells[I].Player = Self) and (Cells[I].Power < TCell(AllCells[C]).GetAvialPower) then begin
     1169        if (Cells[I].Player = Game.CurrentPlayer) and (Cells[I].Power < TCell(AllCells[C]).GetAvialPower) then begin
    11581170          Game.SetMove(TCell(AllCells[C]), Cells[I], (TCell(AllCells[C]).GetAvialPower - Cells[I].Power) div 2);
     1171        end;
     1172      end else begin
     1173        // Is enemy border cell, try to absorb all units from inner neighbours
     1174        for I := 0 to Length(Cells) - 1 do
     1175        if (Cells[I].Player = Game.CurrentPlayer) and (Cells[I].GetAvialPower > 0) then begin
     1176          Cells2 := Game.Map.GetCellNeighbors(TCell(Cells[I]));
     1177          CanAttack2 := 0;
     1178          for J := 0 to Length(Cells2) - 1 do
     1179          if (Cells2[J].Player <> Game.CurrentPlayer) and (Cells2[J].Terrain <> ttVoid) then begin
     1180            Inc(CanAttack2);
     1181          end;
     1182          if CanAttack2 = 0 then
     1183            Game.SetMove(Cells[I], TCell(AllCells[C]), Cells[I].GetAvialPower);
    11591184        end;
    11601185      end;
Note: See TracChangeset for help on using the changeset viewer.