You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在kick方法中实现英雄的evasion(闪避)属性逻辑?

实现英雄闪避逻辑的解决方案

很容易搞定这个需求,咱们只需要在kick方法里加入随机闪避判断,再输出对应的结果信息就行。先理清楚核心逻辑:用0-1之间的随机数和目标的evasion属性对比,随机数小于等于闪避概率时,目标成功闪避,否则计算伤害并扣血。

另外先提个小细节:原代码里的currentHeaalth是拼写错误(少了个l),改成currentHealth能避免后续的bug,我在修改后的代码里已经修正了这个问题。

修改后的完整代码

function Unit(maxHealth, basicDamage, evasion, type) {
  this.maxHealth = maxHealth;
  this.currentHealth = maxHealth; // 修正拼写错误
  this.basicDamage = basicDamage;
  this.evasion = evasion;
  this.type = type;

  /* 判断是否存活 */
  this.isAlive = function () {
    return this.currentHealth > 0;
  };

  /* 格式化显示血量 */
  this.getFormattedHealth = function () {
    return `${this.currentHealth}/${this.maxHealth} HP`;
  };

  /* 获取总伤害(基础伤害+武器伤害) */
  this.getDamage = function () {
    return (this.weapon ? this.weapon.getDamage() : 0) + this.basicDamage;
  };

  /* 踢击方法,加入闪避逻辑 */
  this.kick = function (target) {
    if (!this.isAlive()) {
      console.log(`${this.type}已经阵亡,无法攻击!`);
      return this;
    }

    const dodgeChance = Math.random(); // 生成0-1的随机数
    const damage = this.getDamage();

    if (dodgeChance <= target.evasion) {
      // 闪避成功
      console.log(`✅ ${this.type}的攻击被${target.type}闪避了!`);
    } else {
      // 闪避失败,扣血
      target.currentHealth = Math.max(0, target.currentHealth - damage);
      console.log(`💥 ${this.type}对${target.type}造成了${damage}点伤害!${target.type}当前血量:${target.getFormattedHealth()}`);
    }

    return this;
  };

  /* 格式化显示单位信息 */
  this.toString = function () {
    return `Type - ${this.type}, is alive - ${this.isAlive()}, ${this.getFormattedHealth()}, hero current damage - ${this.getDamage()} points, hero evasion - ${this.evasion}`;
  };
}

function Archer(maxHealth, basicDamage, evasion) {
  Unit.apply(this, arguments);
  this.type = "archer";
}

var archer = new Archer(60, 5, 0.6);

function Swordsman(maxHealth, basicDamage, evasion) {
  Unit.apply(this, arguments);
  this.type = "swordsman";
}

var swordsman = new Swordsman(100, 10, 0.3);

// 战斗循环
while (archer.isAlive() && swordsman.isAlive()) {
  archer.kick(swordsman);
  swordsman.kick(archer);
}

console.log("\n=== 战斗结束 ===");
console.log(archer.toString());
console.log(swordsman.toString());

关键逻辑解释

  • 闪避判断:Math.random()会生成一个0(包含)到1(不包含)的随机数,当这个数小于等于目标的evasion值时,就判定为闪避成功。比如弓箭手的evasion=0.6,就有60%的概率触发闪避。
  • 结果输出:用控制台日志清晰展示每一次攻击的结果,无论是闪避还是造成伤害,都能直观看到过程。
  • 边界处理:扣血时用Math.max(0, ...)保证血量不会变成负数,符合游戏逻辑。

测试效果

运行代码后,你会看到类似这样的战斗过程:

💥 archer对swordsman造成了5点伤害!swordsman当前血量:95/100 HP
✅ swordsman的攻击被archer闪避了!
💥 archer对swordsman造成了5点伤害!swordsman当前血量:90/100 HP
💥 swordsman对archer造成了10点伤害!archer当前血量:50/60 HP
...
=== 战斗结束 ===
Type - archer, is alive - false, 0/60 HP, hero current damage - 5 points, hero evasion - 0.6
Type - swordsman, is alive - true, 40/100 HP, hero current damage - 10 points, hero evasion - 0.3

这样就完美实现了你想要的闪避逻辑和结果输出啦!

内容的提问来源于stack exchange,提问作者Igor Shvets

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:30:10