public void RollDice() { var attackingTerritory = game.Board.GetTerritory(new Location(1, 1)); var defendingTerritory = game.Board.GetTerritory(new Location(0, 0)); var attackResult = game.TryAttack("bazookaJoe", attackingTerritory, defendingTerritory, 2); Assert.IsFalse(attackResult.AttackInvalid); Assert.IsTrue(attackResult.CanContinue); //Assert.IsTrue(game.GetPlayerRemainingArmies("Macaco") == 2); //Assert.IsTrue(game.GetPlayerRemainingArmies("bazookaJoe") == 2); //Assert.IsTrue(gameStatus.Players.Count() == 1); }
private async Task doBattle() { game.StartTime = DateTime.Now; while (game.Players.Count() > 1 && game.GameState == GameState.Attacking && game.Players.Any(p => game.PlayerCanAttack(p))) { var consecutivePacifistTurns = 0; for (int i = 0; i < game.Players.Count() && game.Players.Count() > 1; i++) { var currentPlayer = game.Players.Skip(i).First() as ApiPlayer; if (game.PlayerCanAttack(currentPlayer)) { var failedTries = 0; TryAttackResult attackResult = new TryAttackResult { AttackInvalid = false }; Territory attackingTerritory = null; Territory defendingTerritory = null; logger.LogInformation($"Asking {currentPlayer.Name} what action they want to perform..."); var actionResponse = await askForActionAsync(currentPlayer, ActionStatus.PreviousActionRequestFailed); if (actionResponse.userAction == UserAction.Attack) { consecutivePacifistTurns = 0; do { logger.LogInformation($"Asking {currentPlayer.Name} where they want to attack..."); var beginAttackResponse = await askForAttackLocationAsync(currentPlayer, BeginAttackStatus.PreviousAttackRequestFailed); try { attackingTerritory = game.Board.GetTerritory(beginAttackResponse.From); defendingTerritory = game.Board.GetTerritory(beginAttackResponse.To); logger.LogInformation($"{currentPlayer.Name} wants to attack from {attackingTerritory} to {defendingTerritory}"); attackResult = game.TryAttack(currentPlayer.Token, attackingTerritory, defendingTerritory); } catch (Exception ex) { attackResult = new TryAttackResult { AttackInvalid = true, Message = ex.Message }; } if (attackResult.AttackInvalid) { logger.LogError($"Invalid attack request! {currentPlayer.Name} from {attackingTerritory} to {defendingTerritory} "); failedTries++; if (failedTries == MaxFailedTries) { BootPlayerFromGame(currentPlayer); i--; break; } } } while (attackResult.AttackInvalid); while (attackResult.CanContinue) { var continueResponse = await askContinueAttackingAsync(currentPlayer, attackingTerritory, defendingTerritory); if (continueResponse.ContinueAttacking) { logger.LogInformation("Keep attacking!"); attackResult = game.TryAttack(currentPlayer.Token, attackingTerritory, defendingTerritory); } else { logger.LogInformation("run away!"); break; } } } else if (actionResponse.userAction == UserAction.Pacifism) { consecutivePacifistTurns++; if (consecutivePacifistTurns == game.Players.Count()) { logger.LogInformation("Game Over"); game.SetGameOver(); return; } } } else { logger.LogWarning($"{currentPlayer.Name} cannot attack."); } } } logger.LogInformation("Game Over"); game.SetGameOver(); }
public async Task DoPlayerBattle(ApiPlayer player) { if (game.PlayerCanAttack(player)) { var failedTries = 0; bool hasCard = false; ContinueAttackResponse anotherAttackResponse = new ContinueAttackResponse { ContinueAttacking = false }; do { TryAttackResult attackResult = new TryAttackResult { AttackInvalid = false }; Territory attackingTerritory = null; Territory defendingTerritory = null; do { logger.LogInformation($"Asking {player.Name} where they want to attack..."); var beginAttackResponse = await askForAttackLocationAsync(player, BeginAttackStatus.PreviousAttackRequestFailed); try { attackingTerritory = game.Board.GetTerritory(beginAttackResponse.From); defendingTerritory = game.Board.GetTerritory(beginAttackResponse.To); logger.LogInformation($"{player.Name} wants to attack from {attackingTerritory} to {defendingTerritory}"); attackResult = game.TryAttack(player.Token, attackingTerritory, defendingTerritory); if (attackResult.BattleWasWon && !hasCard) { player.PlayerCards.Add(new Card()); hasCard = true; } } catch (Exception ex) { attackResult = new TryAttackResult { AttackInvalid = true, Message = ex.Message }; } if (attackResult.AttackInvalid) { logger.LogError($"Invalid attack request! {player.Name} from {attackingTerritory} to {defendingTerritory} "); failedTries++; if (failedTries == MaxFailedTries) { BootPlayerFromGame(player); logger.LogError($"Player {player.Name} Booted! Due to attack error"); return; } } } while (attackResult.AttackInvalid); while (attackResult.CanContinue) { var continueResponse = await askContinueAttackingAsync(player, attackingTerritory, defendingTerritory); if (continueResponse.ContinueAttacking) { logger.LogInformation("Keep attacking!"); attackResult = game.TryAttack(player.Token, attackingTerritory, defendingTerritory); if (attackResult.BattleWasWon && !hasCard) { player.PlayerCards.Add(new Card()); hasCard = true; } } else { logger.LogInformation("run away!"); break; } } if (game.PlayerCanAttack(player)) { anotherAttackResponse = await AskMakeAnotherAttackAsync(player, attackingTerritory, defendingTerritory); } else { anotherAttackResponse.ContinueAttacking = false; } } while (anotherAttackResponse.ContinueAttacking); } else { logger.LogWarning($"{player.Name} cannot attack."); } }