コード例 #1
0
    private static BattleResult performBattle(
      Hero hero, 
      Robot robot, 
      DiceService diceService)
    {
      var battleResult = new BattleResult();
      var round = 0;
      while (hero.Health > 0 && robot.Health > 0)
      {
        var battleRound = new BattleRound();

        round++;
        battleRound.RoundNumber = round;

        battleRound.RobotHealthBeginning = robot.Health;
        battleRound.HeroDamageInflicted = robot.Defend(hero.Attack(diceService));
        battleRound.RobotHealth = robot.Health;

        battleRound.HeroHealthBeginning = hero.Health;
        battleRound.RobotDamageInflicted = hero.Defend(robot.Attack(hero, diceService));
        battleRound.HeroHealth = hero.Health;

        battleResult.BattleRounds.Add(battleRound);
      }
      if (hero.Health > 0)
      {
        hero.MovesRemaining--;
        hero.Wins++;
        hero.Health = hero.Health;
        battleResult.CreditsEarned =
          hero.CollectCredits(diceService, robot.Difficulty);

        battleResult.BonusMovesAwarded =
          hero.AwardBonusMoves(diceService, robot.Difficulty);
      }
      else
      {
        hero.MovesRemaining = 0;
        hero.Losses--;
        hero.Health = 0;
      }
      battleResult.Hero = hero;
      battleResult.Robot = robot;
      return battleResult;
    }
コード例 #2
0
ファイル: Default.aspx.cs プロジェクト: rafaelmtz/HeroVsRobot
    private static string formatBattleResult(BattleResult battleResult)
    {
      string result;

      var robotName = battleResult.Robot.Name;
      var heroName = battleResult.Hero.Name;

      var progressMessage = "<h4>Battle Progress:</h4>";
      foreach (var battleRound in battleResult.BattleRounds)
      {
        if (battleRound.HeroHealth <= 20)
        {
          progressMessage += (battleRound.HeroHealth <= 0
            ? string.Format("<div class='bs-callout bs-callout-danger'><h4>Round {0} ...</h4>",
                battleRound.RoundNumber)
            : string.Format("<div class='bs-callout bs-callout-warning'><h4>Round {0} ...</h4>",
                battleRound.RoundNumber));
        }
        else
        {
          progressMessage += string.Format("<div class='bs-callout bs-callout-info'><h4>Round {0} ...</h4>",
            battleRound.RoundNumber);
        }

        var robotHealth = (battleRound.RobotHealth > 0 ? battleRound.RobotHealth : 0);
        var heroHealth = (battleRound.HeroHealth > 0 ? battleRound.HeroHealth : 0);

        var heroDamageInflicted = battleRound.HeroDamageInflicted;

        progressMessage += string.Format("<p>{0} inflicted {1} damage on {2} reducing {2}'s health to {3}.</p>",
          robotName,
          heroDamageInflicted,
          robotName,
          robotHealth);

        var robotDamageInflicted = battleRound.RobotDamageInflicted;

        progressMessage += string.Format("<p>{0} inflicted {1} damage on {2} reducing {2}'s health to {3}.</p>",
          heroName,
          robotDamageInflicted,
          heroName,
          heroHealth);
        progressMessage += "</div>";
      }


      if (battleResult.Hero.Health > 0)
      {
        result = string.Format("<p class='text-success'>Awesome! {0} defeats {1}!</p>",
          heroName,
          robotName);

        result += string.Format("<p class='text-success'>Earned {0} credits!</p>",
          battleResult.CreditsEarned);

        if (battleResult.BonusMovesAwarded > 0)
        {
          result += string.Format("<p class='text-success'>w00t! You earned {0} bonus moves!</p>",
            battleResult.BonusMovesAwarded);
        }
        if (battleResult.Hero.Health <= 20)
        {
          result += string.Format("<p class='text-warning'>Be careful! Your hero only has {0} health left!</p>",
            battleResult.Hero.Health);
        }
      }
      else
      {
        result = (battleResult.Robot.Health > 0
          ? string.Format("<p class='text-danger'>Oh no! {0} defeats {1}!</p>",
            robotName,
            heroName)
          : string.Format("<p class='text-danger'>Oh no! Both {0} and {1} were knocked out.",
            heroName,
            robotName));
      }
      return result + progressMessage;
    }