gpt4 book ai didi

javascript - 找不到此 JavaScript 代码中的错误所在。对 'class'语法不太熟悉

转载 作者:行者123 更新时间:2023-11-28 03:19:19 25 4
gpt4 key购买 nike

这是代码。这是一个随机陌生人的作业,他在 IG 上看到我的帖子后要求我帮他解决。

// Player class
class Player {
constructor(name, strength = 2, weapons) {
this.name = name;
this.health = 10;
this.strength = strength;
this.weapons = [...weapons];
}

applyDamage(int) {
this.health -= int;
}

isAlive() {
return this.health > 0;
}

attackWith() {
let randomNum = Math.floor(Math.random() * 8);
return this.weapons[randomNum];
}
}

// Weapon class

class Weapon {
constructor(name) {
this.name = name;
this.damage = Math.ceil(Math.random() * 5);
}

attack(player, enemy) {
if (player.isAlive() && player.isAlive()) {
let dmg = player.strength * this.damage;
enemy.applyDamage(dmg);
if (!enemy.isAlive()) {
return;
} else {
enemy.attack(player);
}
}
}
}

// Enemy class

class Enemy {
constructor(name = "Enemy", health = 5, strength = 2) {
this.name = name;
this.health = health;
this.strength = strength;
}

applyDamage(int) {
this.health -= int;
}

isAlive() {
return this.health > 0;
}

attack(player) {
player.applyDamage(this.strength);
}
}

// BattleSimulation class

class BattleSimulation {
constructor() {
this.players = [];
this.enemies = [];
}

createEnemies() {
for (let i = 0; i < 20; i += 1) {
this.enemies[i] = new Enemy();
}
}

createPlayers() {
// Weapons
let pencil = new Weapon("Pencil");
let book = new Weapon("Book");
let screwdriver = new Weapon("Screwdriver");
let theOneRing = new Weapon("Sauron's Ring");
let mustardGass = new Weapon("Mustard Gass");
let bigBoy = new Weapon("A Nuke");
let love = new Weapon("Power of Love");
let theForce = new Weapon("The Force");

let weaponsCache = [
pencil,
book,
screwdriver,
theOneRing,
mustardGass,
bigBoy,
love,
theForce
];

// Players
let luke = new Player("Luke", 5, weaponsCache);
let baldingCoder = new Player("DraciVik", 10, weaponsCache);
let trump = new Player("Trump", 1, weaponsCache);
let kikiMakarena = new Player("Kiki Makarena", 5, weaponsCache);
let johnWick = new Player("John Wick", 2, weaponsCache);

this.players = [luke, baldingCoder, trump, kikiMakarena, johnWick];
}

run() {
console.log("Simulating Battle");
this.createEnemies();
this.createPlayers();

while (this.players.length !== 0 || this.enemies.length !== 0) {
let randomPlayerIndex = Math.floor(Math.random() * this.players.length);
let randomPlayer = this.players[randomPlayerIndex];
let randomEnemyIndex = Math.floor(Math.random() * this.enemies.length);
let randomEnemy = this.enemies[randomEnemyIndex];
let weapon = randomPlayer.attackWith();
weapon.attack(randomPlayer, randomEnemy);
if (!randomPlayer.isAlive()) {
this.players.splice(randomPlayerIndex, 1);
}
if (!randomEnemy.isAlive()) {
this.enemies.splice(randomEnemyIndex, 1);
}
}
console.log(this.players);
if (this.players.length > 0) {
return "Congratulations, you have defeated Scarlet Byle";
}
return "Sorry, Scarlet Byle has defeated you and conquered the free world";
}
}

let battle = new BattleSimulation();
battle.run();

谁能看出错误在哪里吗?我收到返回错误 'enemy.applyDamage(dmg)' is undefined。

这个错误是什么,我需要更多的编写而不仅仅是代码?我应该发送一些垃圾邮件吗?

最佳答案

这里的错误实际上只是在您的 while 循环的条件下:

while(this.players.length !== 0 || this.enemies.length !== 0)

你的条件是在至少有一名玩家或至少有一名敌人时循环。所以只要其中一个数组不为空,就会继续循环。

但是当您第一次创建 this.playersthis.enemies 时,它们的初始大小不同。然后,当您从每个数组中删除一个条目时,最终其中一个数组先于另一个数组为空。

然后你的代码有 var randomEnemyIndex = Math.floor(Math.random() * this.enemies.length); 当数组为空时,它将计算为 0。当您执行 this.enemies[0] 时,它会返回 undefined。当undefinedweapon.attack(randomPlayer, undefined)一样传递到weapon.attack时,它会尝试调用applyDamage(dmg) )undefined 上,这会抛出异常。

如果您修改代码以具有以下控制台日志,您将看到此问题:


while (this.players.length !== 0 || this.enemies.length !== 0) {

console.log("PLAYERS: " + this.players.length.toString());
console.log("ENEMIES: " + this.enemies.length.toString());

...

你会看到:

'Simulating Battle'
'PLAYERS: 5'
'ENEMIES: 20'
'PLAYERS: 5'
'ENEMIES: 20'
'PLAYERS: 5'
[...]
'PLAYERS: 4'
'ENEMIES: 1'
'PLAYERS: 4'
'ENEMIES: 0'
error: Uncaught TypeError: Cannot read property 'applyDamage' of undefined

因此,要解决此问题,您需要将条件更改为:

while (this.players.length !== 0 && this.enemies.length !== 0) {

或者您需要以相同的大小开始两个数组。

关于javascript - 找不到此 JavaScript 代码中的错误所在。对 'class'语法不太熟悉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59314103/

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