コード例 #1
0
        public void Fight(Monster monster)
        {
            bool lastAttackerWasPlayer = false;

            Random r = new Random();

            while (monster.health > 0 && playerHealth > 0)
            {
                if (lastAttackerWasPlayer)
                {
                    int damage = (monster.baseDamage * r.Next(1,
                        monster.diceSides)) + monster.damageBonus;

                    if (r.Next(0, 100) < 70)
                    {
                        playerHealth -= damage;
                        Console.WriteLine(monster.Name + "hits you and gives " +
                            damage.ToString() + " damage.");
                    }
                    else //missed
                    {
                        Console.WriteLine(monster.Name + " missed to hit you.");
                    }

                    lastAttackerWasPlayer = false;
                }
                else
                {
                    int damage = (baseDamage * r.Next(1, diceSides)) + damageBonus;

                    if (r.Next(0, 100) < 70)
                    {
                        monster.health -= damage;
                        Console.WriteLine("You hit " + monster.Name + " and give "
                            + damage.ToString() + " damage.");
                    }
                    else //missed
                    {
                        Console.WriteLine("You missed to hit.");
                    }

                    lastAttackerWasPlayer = true;
                }
                if (playerHealth <= 0)
                {
                    Console.WriteLine("You died.");
                    Console.ReadKey();
                    return;
                }

                if (monster.health <= 0)
                {
                    Console.WriteLine("You killed " + monster.Name + "! (Experience + " + monster.givenExperience.ToString() + ")");
                        playerExperience += monster.givenExperience;
                    if (playerExperience >= experienceToNextLevel)
                    {
                        playerLevel++;
                        Console.WriteLine("You advanced to level " + playerLevel.ToString());
                    }
                }

                Console.ReadKey();
            }
        }
コード例 #2
0
        public Monster Clone()
        {
            Monster m = new Monster();
            m.Name = this.Name;
            m.baseDamage = this.baseDamage;
            m.diceSides = this.diceSides;
            m.damageBonus = this.damageBonus;
            m.health = this.health;
            m.givenExperience = this.givenExperience;

            return m;
        }