葡萄城的程序里面抠出来的- private static int Fight(INode mario, INode monster, ref string message)
- {
- int hp1 = mario.Health;
- int hp2 = monster.Health;
- int ap1 = mario.Attack;
- int ap2 = monster.Attack;
- int dp1 = mario.Defence;
- int dp2 = monster.Defence;
- if (ap1 <= dp2)
- {
- message = "由于攻击力太低,无法战胜怪物。";
- return 0;
- }
- while (hp1 > 0 && hp2 > 0)
- {
- if (ap1 > dp2)
- {
- hp2 -= (ap1 - dp2);
- }
- if (hp2 > 0)
- {
- if (ap2 > dp1)
- {
- hp1 -= (ap2 - dp1);
- }
- }
- }
- if (hp1 <= 0)
- {
- message = "被怪物杀死了。";
- return 0;
- }
- return hp1;
- }
复制代码 另外分享下我程序里的:-
- if (this.Attack <= monster.Defence)
- {
- ret.Clear();
- }
- else if (this.Defence >= monster.Attack)
- {
- this.CopyTo(ret);
- }
- else
- {
- // hurt1是打怪物一次掉的血,hurt2是被怪物打一次掉的血
- int hurt1 = this.Attack - monster.Defence;
- int hurt2 = monster.Attack - this.Defence;
- // time1是打死怪物的次数,time2是被怪物打死的次数
- int time1 = (monster.Health / hurt1) + (((monster.Health % hurt1) > 0) ? 1 : 0 );
- int time2 = (this.Health / hurt2) + (((this.Health % hurt2) > 0) ? 1 : 0);
- // 打怪物次数多于被打次数,自己先死
- if (time1 > time2)
- {
- ret.Clear();
- }
- else
- {
- this.CopyTo(ret);
- ret.Health -= (hurt2 * (time1 - 1));
- }
- }
复制代码 |