- Timestamp:
- Feb 20, 2014, 11:29:54 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UFormMain.pas
r16 r17 59 59 RedrawPending: Boolean; 60 60 procedure DoOnMove(CellFrom, CellTo: TCell; var CountOnce, CountRepeat: Integer); 61 procedure DoOnWin(Player: TPlayer); 61 62 public 62 63 Game: TGame; … … 111 112 end; 112 113 114 procedure TFormMain.DoOnWin(Player: TPlayer); 115 begin 116 ShowMessage('Player ' + Player.Name + ' wins'); 117 end; 118 113 119 procedure TFormMain.Redraw; 114 120 begin … … 120 126 Game := TGame.Create; 121 127 Game.OnMove := DoOnMove; 128 Game.OnWin := DoOnWin; 122 129 end; 123 130 … … 131 138 Game.NextTurn; 132 139 Redraw; 133 while Game. CurrentPlayer.Mode <> pmHumando begin140 while Game.Running and (Game.CurrentPlayer.Mode <> pmHuman) do begin 134 141 Game.NextTurn; 135 142 Redraw; -
trunk/UGame.pas
r16 r17 62 62 procedure Grow(APlayer: TPlayer); 63 63 procedure ClearCellMoves; 64 procedure ComputePlayerStats; 64 65 property Size: TPoint read FSize write SetSize; 65 66 end; … … 77 78 SelectedCell: TCell; 78 79 Mode: TPlayerMode; 80 TotalUnits: Integer; 81 TotalCells: Integer; 79 82 procedure ComputerTurn; 80 83 procedure SelectCell(Pos: TPoint); … … 93 96 94 97 TMoveEvent = procedure(CellFrom, CellTo: TCell; var CountOnce, CountRepeat: Integer) of object; 98 TWinEvent = procedure(Player: TPlayer) of object; 95 99 96 100 TGame = class 97 101 private 98 102 FOnMove: TMoveEvent; 103 FOnWin: TWinEvent; 99 104 procedure MoveAll(Player: TPlayer); 100 105 procedure SetMove(CellFrom, CellTo: TCell); … … 106 111 Moves: TObjectList; // TList<TMove> 107 112 TurnCounter: Integer; 113 Running: Boolean; 114 procedure ComputePlayerStats; 108 115 procedure NextTurn; 109 116 constructor Create; … … 112 119 published 113 120 property OnMove: TMoveEvent read FOnMove write FOnMove; 121 property OnWin: TWinEvent read FOnWin write FOnWin; 114 122 end; 115 123 … … 310 318 end; 311 319 320 procedure TGame.ComputePlayerStats; 321 var 322 I: Integer; 323 begin 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; 330 end; 331 312 332 procedure TGame.NextTurn; 333 var 334 AlivePlayers: TObjectList; 335 I: Integer; 336 PrevPlayer: TPlayer; 313 337 begin 314 338 MoveAll(CurrentPlayer); 315 339 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; 318 359 end; 319 360 … … 322 363 Player: TPlayer; 323 364 begin 365 Randomize; 366 324 367 Players := TObjectList.Create; 325 368 Player := TPlayer.Create; … … 339 382 Map := THexMap.Create; 340 383 Map.Game := Self; 341 Map.Size := Point(2 0, 20);384 Map.Size := Point(2, 2); 342 385 Moves := TObjectList.Create; 343 386 end; … … 379 422 end; 380 423 CurrentPlayer := TPlayer(Players[0]); 424 Running := True; 381 425 end; 382 426 … … 583 627 end; 584 628 629 procedure THexMap.ComputePlayerStats; 630 var 631 X, Y: Integer; 632 begin 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; 642 end; 643 585 644 end. 586 645
Note:
See TracChangeset
for help on using the changeset viewer.