浏览器游戏中摧毁建筑物所需的时间计算错误


// Number of rockets launched per second.
const rocketSpeed = 1;
// The amount of damage dealt by rocket.
const attackDamage = 60;
// The amount of hit points of the building.
const buildingHitPoints = 1000;
// The number of rockets required to destroy the building.
const numberOfRockets = Math.ceil(buildingHitPoints / attackDamage);
// The time it takes to destroy the building.
const requiredTime = numberOfRockets * rocketSpeed;

如果rocketSpeed超过1秒,那么我得到了正确的值。

例如:const requiredTime = 17 * 1

但是,如果指定的rocketSpeed小于1秒,则该值不正确:

例如:const requiredTime = 17 * 0.625; // 10.625

0.625是每秒发射的火箭数量。也就是说,1枚火箭将在约1.2秒内发射。

我按类型尝试了不同的选项:const requiredTime = 17 + (17 - 37.5%);(37.5%是因为:1000-625=375。375是1000的37.5%(,但它不起作用。

每秒发射的火箭数量最好被认为是一个频率,而不是一个";速度";。

频率ff=1/T,其中时间是T,所以T=1/f

也许可以将rocketSpeed重命名为rocketFrequency,并使用
const timeBetweenRockets = 1 / rocketFrequency创建一个新变量timeBetweenRockets

那么最后一行代码将更加自我解释

const requiredTime = numberOfRockets * timeBetweenRockets

您的计算方式错误。如果你尝试的值大于1,那么你会发现它需要更长的时间:每秒1枚火箭(r/s(在17秒内产生。2转/秒在34秒内产生,0.5转/秒产生10.625秒。

const calculate = rocketSpeed => {
// The amount of damage dealt by rocket.
const attackDamage = 60;
// The amount of hit points of the building.
const buildingHitPoints = 1000;
// The number of rockets required to destroy the building.
const numberOfRockets = Math.ceil(buildingHitPoints / attackDamage);
// The time it takes to destroy the building.
const requiredTime = numberOfRockets * rocketSpeed;

return requiredTime;
}

console.log(`Time for 1 rocket/s: ${calculate(1)}`);
console.log(`Time for 2 rocket/s: ${calculate(2)}`);
console.log(`Time for 0.625 rocket/s: ${calculate(0.625)}`);

如果你把numberOfRockets除以rocketSpeed,你会得到正确的答案。

const calculate2 = rocketSpeed => {
// The amount of damage dealt by rocket.
const attackDamage = 60;
// The amount of hit points of the building.
const buildingHitPoints = 1000;
// The number of rockets required to destroy the building.
const numberOfRockets = Math.ceil(buildingHitPoints / attackDamage);
// The time it takes to destroy the building.
const requiredTime = numberOfRockets / rocketSpeed;

return requiredTime;
}
console.log(`Time for 1 rocket/s: ${calculate2(1)}`);
console.log(`Time for 2 rocket/s: ${calculate2(2)}`);
console.log(`Time for 0.625 rocket/s: ${calculate2(0.625)}`);

最新更新