Changeset 25


Ignore:
Timestamp:
Mar 3, 2014, 12:42:16 AM (11 years ago)
Author:
chronos
Message:
  • Added: Implement basic computer strategy to attack to weaker neighbours.
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/UCore.pas

    r23 r25  
    8989  FormMain.Redraw;
    9090  while Game.Running and (Game.CurrentPlayer.Mode <> pmHuman) do begin
     91    if Game.CurrentPlayer.Mode = pmComputer then
     92      Game.CurrentPlayer.ComputerTurn;
    9193    Game.NextTurn;
    9294    FormMain.Redraw;
  • trunk/UFormMain.pas

    r23 r25  
    7373procedure TFormMain.PaintBox1Paint(Sender: TObject);
    7474begin
    75   if Core.Game.Running then
     75  //if Core.Game.Running then
     76  if Assigned(Core.Game.CurrentPlayer) then
    7677  with Core.Game.CurrentPlayer do begin
    7778    View.DestRect := Bounds(0, 0, PaintBox1.Width, PaintBox1.Height);
  • trunk/UGame.pas

    r24 r25  
    3939    destructor Destroy; override;
    4040  end;
     41
     42  TCellArray = array of TCell;
    4143
    4244  { TMap }
     
    7779    DefaultCellSize: TPoint;
    7880    Cells: array of array of TCell;
     81    function IsValidIndex(Index: TPoint): Boolean;
     82    function GetCellNeighbours(Cell: TCell): TCellArray;
    7983    function PosToCell(Pos: TPoint; View: TView): TCell;
    8084    function CellToPos(Cell: TCell; View: TView): TPoint;
     
    309313
    310314procedure TPlayer.ComputerTurn;
    311 begin
    312 
     315var
     316  Cells: TCellArray;
     317  X, Y: Integer;
     318  Target: TCell;
     319  MinPower: Integer;
     320  I: Integer;
     321begin
     322  for Y := 0 to Game.Map.Size.Y - 1 do
     323  for X := 0 to Game.Map.Size.Y - 1 do
     324  with TCell(Game.Map.Cells[Y, X]) do begin
     325    if Player = Self then begin
     326      // Search not own neighbour with lowes power
     327      Cells := Game.Map.GetCellNeighbours(Game.Map.Cells[Y, X]);
     328      Target := nil;
     329      for I := 0 to Length(Cells) - 1 do
     330      if (Cells[I].Terrain <> ttVoid) and (Cells[I].Player <> Self) then begin
     331        if I = 0 then begin
     332          MinPower := Cells[I].Power;
     333          Target := Cells[I];
     334        end else if Cells[I].Power < MinPower then begin
     335          MinPower := Cells[I].Power;
     336          Target := Cells[I];
     337        end;
     338      end;
     339      // Attack if target is weaker
     340      if Assigned(Target) and (Target.Power < Power) then
     341        Game.SetMove(Game.Map.Cells[Y, X], Target);
     342    end;
     343  end;
    313344end;
    314345
     
    415446    CountOnce := TMove(Moves[I]).CountOnce;
    416447    CountRepeat := TMove(Moves[I]).CountRepeat;
    417     if Assigned(FOnMove) then FOnMove(CellFrom, CellTo, CountOnce, CountRepeat, True);
     448    if Assigned(CurrentPlayer) and (CurrentPlayer.Mode = pmHuman) and
     449      Assigned(FOnMove) then FOnMove(CellFrom, CellTo, CountOnce, CountRepeat, True);
    418450  end else begin
    419451    CountOnce := CellFrom.Power;
    420452    CountRepeat := 0;
    421     if Assigned(FOnMove) then FOnMove(CellFrom, CellTo, CountOnce, CountRepeat, False);
     453    if Assigned(CurrentPlayer) and (CurrentPlayer.Mode = pmHuman) and
     454      Assigned(FOnMove) then FOnMove(CellFrom, CellTo, CountOnce, CountRepeat, False);
    422455  end;
    423456  if I < Moves.Count then begin
     
    599632      Cells[Y, X] := NewCell;
    600633    end;
     634  end;
     635end;
     636
     637function THexMap.IsValidIndex(Index: TPoint): Boolean;
     638begin
     639  Result := (Index.X >= 0) and (Index.X < Size.X) and
     640    (Index.Y >= 0) and (Index.Y < Size.Y);
     641end;
     642
     643function THexMap.GetCellNeighbours(Cell: TCell): TCellArray;
     644var
     645  X, Y: Integer;
     646begin
     647  SetLength(Result, 0);
     648  for Y := -1 to 1 do
     649  for X := -1 to 1 do
     650  if IsValidIndex(Point(Cell.Pos.X + X, Cell.Pos.Y + Y)) and
     651  IsCellsNeighbor(Cell, Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]) then begin
     652    SetLength(Result, Length(Result) + 1);
     653    Result[Length(Result) - 1] := Cells[Cell.Pos.Y + Y, Cell.Pos.X + X];
    601654  end;
    602655end;
Note: See TracChangeset for help on using the changeset viewer.