gpt4 book ai didi

javascript - 如何在 "evasion"方法中实现 "kick"属性?

转载 作者:行者123 更新时间:2023-12-03 01:50:04 25 4
gpt4 key购买 nike

我想在“kick”方法中实现英雄躲避的逻辑。对于英雄,设置了一定数量的“躲避”属性,在此基础上设置了其躲避的百分比,示例如下“躲避”弓箭手的几率是 0.6,即 60% 的几率躲避“踢”。如何正确实现此逻辑以及消息的结果,显示伤害或躲避

function Unit(maxHealth, basicDamage, evasion, type) {

this.maxHealth = maxHealth;
this.currentHeaalth = maxHealth;
this.basicDamage = basicDamage;
this.evasion = evasion;
this.type = type;
/*method for showing the status of life, true if the "health" is greater
than 0 and false if equal to or lower */
this.isAlive = function () {
return this.currentHeaalth > 0
};
/* a method that
shows the level of health*/
this.getFormattedHealth = function () {
return this.currentHeaalth + '/' + this.maxHealth + ' HP';
};

/*a method that returns the base damage of the heroand damage to the
weapon (if it is set)*/
this.getDamage = function () {
return (this.weapon ? this.weapon.getDamage() : 0) + this.basicDamage;
};
/* The method of hitting
the hero for the chosen purpose*/
this.kick = function (target) {
if (this.isAlive()) {
target.currentHeaalth = Math.max(0, target.currentHeaalth - this.getDamage());
}
return this;
};
/*method for showing all the characteristics of the hero and changes
with them*/
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(archer.toString());
console.log(swordsman.toString());

最佳答案

编写一个函数,根据逃避方式用 10 位 01 填充数组,并返回随机 1 或 <强>0:

let evasion = 0.4,
probability = function() {
var notRandomNumbers = [],
maxEvasion = 0;
if ((evasion + '').split('.')[0] == 1 && (evasion + '').split('.')[1] == 0) {
maxEvasion = 10;
} else {
maxEvasion = (evasion + '').split('.')[1];
}
for (var i = 0; i < maxEvasion; i++) {
notRandomNumbers.push(1);
}
for (var i = 0; i < 10 - maxEvasion; i++) {
notRandomNumbers.push(0);
}
var idx = Math.floor(Math.random() * notRandomNumbers.length);
return notRandomNumbers[idx];
}

console.log('1 )', probability());
console.log('2 )', probability());
console.log('3 )', probability());
console.log('4 )', probability());

kick函数中使用它。

this.kick = function(target) {
if (this.isAlive()) {
target.currentHeaalth = Math.max(
0,
target.currentHeaalth - this.probability() * this.getDamage()
);
}
};

完整代码如下:

function Unit(maxHealth, basicDamage, evasion, type) {
this.maxHealth = maxHealth;
this.currentHeaalth = maxHealth;
this.basicDamage = basicDamage;
this.evasion = evasion;
this.type = type;

/*method for showing the status of life, true if the "health" is greater
than 0 and false if equal to or lower */
this.isAlive = function() {
return this.currentHeaalth > 0;
};
/* a method that
shows the level of health*/
this.getFormattedHealth = function() {
return this.currentHeaalth + "/" + this.maxHealth + " HP";
};

this.probability = function() {
var notRandomNumbers = [],
maxEvasion = 0;
if (
(this.evasion + "").split(".")[0] == 1 &&
(this.evasion + "").split(".")[1] == 0
) {
maxEvasion = 10;
} else {
maxEvasion = (this.evasion + "").split(".")[1];
}
for (var i = 0; i < maxEvasion; i++) {
notRandomNumbers.push(1);
}
for (var i = 0; i < 10 - maxEvasion; i++) {
notRandomNumbers.push(0);
}
var idx = Math.floor(Math.random() * notRandomNumbers.length);
return notRandomNumbers[idx];
};

/*a method that returns the base damage of the heroand damage to the
weapon (if it is set)*/
this.getDamage = function() {
return (this.weapon ? this.weapon.getDamage() : 0) + this.basicDamage;
};
/* The method of hitting
the hero for the chosen purpose*/
this.kick = function(target) {
if (this.isAlive()) {
target.currentHeaalth = Math.max(
0,
target.currentHeaalth - this.probability() * this.getDamage()
);
}
};
/*method for showing all the characteristics of the hero and changes
with them*/
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(archer.toString());
console.log(swordsman.toString());

希望这对您有帮助。

关于javascript - 如何在 "evasion"方法中实现 "kick"属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50454504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com