Changeset 17 for trunk


Ignore:
Timestamp:
Feb 20, 2014, 11:29:54 PM (11 years ago)
Author:
chronos
Message:
  • Added: Compute total units and total cell count for each player and evaluate how many players are still alive. If only one player remains then end game and show winner.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/UFormMain.pas

    r16 r17  
    5959    RedrawPending: Boolean;
    6060    procedure DoOnMove(CellFrom, CellTo: TCell; var CountOnce, CountRepeat: Integer);
     61    procedure DoOnWin(Player: TPlayer);
    6162  public
    6263    Game: TGame;
     
    111112end;
    112113
     114procedure TFormMain.DoOnWin(Player: TPlayer);
     115begin
     116  ShowMessage('Player ' + Player.Name + ' wins');
     117end;
     118
    113119procedure TFormMain.Redraw;
    114120begin
     
    120126  Game := TGame.Create;
    121127  Game.OnMove := DoOnMove;
     128  Game.OnWin := DoOnWin;
    122129end;
    123130
     
    131138  Game.NextTurn;
    132139  Redraw;
    133   while Game.CurrentPlayer.Mode <> pmHuman do begin
     140  while Game.Running and (Game.CurrentPlayer.Mode <> pmHuman) do begin
    134141    Game.NextTurn;
    135142    Redraw;
  • trunk/UGame.pas

    r16 r17  
    6262    procedure Grow(APlayer: TPlayer);
    6363    procedure ClearCellMoves;
     64    procedure ComputePlayerStats;
    6465    property Size: TPoint read FSize write SetSize;
    6566  end;
     
    7778    SelectedCell: TCell;
    7879    Mode: TPlayerMode;
     80    TotalUnits: Integer;
     81    TotalCells: Integer;
    7982    procedure ComputerTurn;
    8083    procedure SelectCell(Pos: TPoint);
     
    9396
    9497  TMoveEvent = procedure(CellFrom, CellTo: TCell; var CountOnce, CountRepeat: Integer) of object;
     98  TWinEvent = procedure(Player: TPlayer) of object;
    9599
    96100  TGame = class
    97101  private
    98102    FOnMove: TMoveEvent;
     103    FOnWin: TWinEvent;
    99104    procedure MoveAll(Player: TPlayer);
    100105    procedure SetMove(CellFrom, CellTo: TCell);
     
    106111    Moves: TObjectList; // TList<TMove>
    107112    TurnCounter: Integer;
     113    Running: Boolean;
     114    procedure ComputePlayerStats;
    108115    procedure NextTurn;
    109116    constructor Create;
     
    112119  published
    113120    property OnMove: TMoveEvent read FOnMove write FOnMove;
     121    property OnWin: TWinEvent read FOnWin write FOnWin;
    114122  end;
    115123
     
    310318end;
    311319
     320procedure TGame.ComputePlayerStats;
     321var
     322  I: Integer;
     323begin
     324  for I := 0 to Players.Count - 1 do
     325  with TPlayer(Players[I]) do begin
     326    TotalUnits := 0;
     327    TotalCells := 0;
     328  end;
     329  Map.ComputePlayerStats;
     330end;
     331
    312332procedure TGame.NextTurn;
     333var
     334  AlivePlayers: TObjectList;
     335  I: Integer;
     336  PrevPlayer: TPlayer;
    313337begin
    314338  MoveAll(CurrentPlayer);
    315339  Map.Grow(CurrentPlayer);
    316   CurrentPlayer := TPlayer(Players[(Players.IndexOf(CurrentPlayer) + 1) mod Players.Count]);
    317   if Players.IndexOf(CurrentPlayer) = 0 then Inc(TurnCounter);
     340  ComputePlayerStats;
     341  PrevPlayer := CurrentPlayer;
     342  // Skip dead players
     343  repeat
     344    CurrentPlayer := TPlayer(Players[(Players.IndexOf(CurrentPlayer) + 1) mod Players.Count]);
     345  until CurrentPlayer.TotalCells > 0;
     346  if Players.IndexOf(CurrentPlayer) < Players.IndexOf(PrevPlayer) then Inc(TurnCounter);
     347  try
     348    AlivePlayers := TObjectList.Create;
     349    AlivePlayers.OwnsObjects := False;
     350    for I := 0 to Players.Count - 1 do
     351      if TPlayer(Players[I]).TotalCells > 0 then AlivePlayers.Add(TPlayer(Players[I]));
     352    if AlivePlayers.Count <= 1 then begin
     353      Running := False;
     354      if (AlivePlayers.Count = 1) and Assigned(OnWin) then OnWin(TPlayer(AlivePlayers.First));
     355    end;
     356  finally
     357    AlivePlayers.Free;
     358  end;
    318359end;
    319360
     
    322363  Player: TPlayer;
    323364begin
     365  Randomize;
     366
    324367  Players := TObjectList.Create;
    325368  Player := TPlayer.Create;
     
    339382  Map := THexMap.Create;
    340383  Map.Game := Self;
    341   Map.Size := Point(20, 20);
     384  Map.Size := Point(2, 2);
    342385  Moves := TObjectList.Create;
    343386end;
     
    379422  end;
    380423  CurrentPlayer := TPlayer(Players[0]);
     424  Running := True;
    381425end;
    382426
     
    583627end;
    584628
     629procedure THexMap.ComputePlayerStats;
     630var
     631  X, Y: Integer;
     632begin
     633  for Y := 0 to Size.Y - 1 do
     634  for X := 0 to Size.X - 1 do
     635  with Cells[Y, X] do begin
     636    if Assigned(Player) then begin
     637      Player.TotalCells := Player.TotalCells + 1;
     638      Player.TotalUnits := Player.TotalUnits + Power;
     639
     640    end;
     641  end;
     642end;
     643
    585644end.
    586645
Note: See TracChangeset for help on using the changeset viewer.