using System; namespace Assignment10 { class Program { static void Main(string[] args) { const int SIVIR_RANDOM_SEED = 168; Champion sivir = new Champion("Sivir", 250, 40, SIVIR_RANDOM_SEED); //(name,hp,attackStrength) const int OLAF_RANDOM_SEED = 777; Champion olaf = new Champion("Olaf", 300, 55, OLAF_RANDOM_SEED); Random rand = new Random(); int k; Console.WriteLine("Start Fighting!!\n"); while (sivir.PositiveHP() && olaf.PositiveHP()) { //Random決定攻擊順序 k = rand.Next() % 2; if (k == 0) { Fight(sivir, olaf); } else { Fight(olaf, sivir); } } } static void Fight(Champion attacker, Champion victim) { attacker.Attack(victim); Console.WriteLine(attacker.Name + "\tattacks" + "\t\t\tHP = {0}", attacker.HP); Console.WriteLine(victim.Name + "\ttakes {0} HP loss" + "\tHP = {1}", victim.Damage, victim.HP); if (!victim.PositiveHP()) { Console.WriteLine(attacker.Name + " WIN !!"); } } } class Champion { private int hp; private int attackStrength; private int damage; private string name; Random rand; public string Name { get { return name; } } public int HP { get { return hp; } } public int Damage { get { return damage; } } public Champion(string name, int hp, int attackStrength, int randSeed) { this.name = name; this.hp = hp; this.attackStrength = attackStrength; damage = 0; rand = new Random(randSeed); } public Champion() // 建構式 { name = "NoName"; hp = 100; attackStrength = 20; damage = 0; rand = new Random(); } public void Attack(Champion victim) //英雄攻擊對方時呼叫此函式,函式的輸入是被攻擊者物件 { int damage = attackStrength*hp/150 + rand.Next() % 20 + 1; if (damage > victim.HP) damage = victim.HP; victim.BeingAttacked(damage); } public void BeingAttacked(int damage) //被攻擊者要呼叫此函式,減少血量 { this.damage = damage; hp -= damage; } public bool PositiveHP() //檢查血量是否為正 { return (hp > 0); } } }