Exemplo n.º 1
0
        protected void okButton_Click(object sender, EventArgs e)
        {
            string result = "";

            try
            {
                // Calculate percentage of wins:
                decimal wins = decimal.Parse(gamesWonTextBox.Text);
                decimal total = decimal.Parse(totalGamesTextBox.Text);
                decimal winningPercentage = wins / total;

                result = string.Format("Winning Percentage: {0:P}",
                    winningPercentage);

                var monster = new Character() { Name = "Zerg", HitPoints = 0 };
                var hero = new Character() { Name = "Buzz", HitPoints = 5 };
                GameActions.Battle(hero, monster);
                result += string.Format("{0} attacked {1} leaving him with {2} hit points.",
                    hero.Name,
                    monster.Name,
                    monster.HitPoints);

            }
            catch (FormatException ex)
            {
                //Log the exception!

                result = "Please make sure to enter numeric values.";
            }
            catch (DivideByZeroException ex)
            {
                result = "Please make sure the number of games is greater than 0.";
            }
            catch (ArgumentOutOfRangeException ex)
            {
                result = "There was a problem: " + ex.Message;
            }
            catch (CharacterAlreadyDeadException ex)
            {
                result = "Problem: " + ex.CharacterName + " was dead.  Could not attack.";
            }
            catch (Exception ex)
            {
                result = "There was a problem: " + ex.Message;
            }
            finally
            {
                // Perform cleanup here.  You probably
                // won't need this until we start working
                // with external resources like
                // database connections, network connections,
                // file system manipulation, etc.
            }

            resultLabel.Text = result;
        }
Exemplo n.º 2
0
        // In the past, some have tried return codes.
        // That is generally frowned upon because
        // return codes are like magic numbers ...
        // they are hard to check at compile time.
        // DO NOT DO THIS:
        // public int Battle(Character attacker, Character defender)
        // {
        //   ...
        //   if (successful) return 0;
        //   else return 66;
        //   ...
        // }
        public static void Battle(Character attacker, Character defender)
        {
            if (attacker.HitPoints <= 0)
                throw new CharacterAlreadyDeadException("Attacker");

            if (defender.HitPoints <= 0)
                throw new CharacterAlreadyDeadException("Defender");

            Random random = new Random();
            int attackValue = random.Next(100);

            defender.HitPoints -= attackValue;
        }