javascript - 在此 JavaScript 代码中找不到错误所在。对“类”语法不太熟悉
问题描述
这是代码。这是一个随机的陌生人的任务,因为他在 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)'
是未定义的。
这个错误是什么,我需要更多的编写而不是代码?我应该发送一些垃圾邮件吗?
解决方案
这里的错误实际上只是在你的while
循环条件下:
while(this.players.length !== 0 || this.enemies.length !== 0)
您的条件是在至少有一名玩家或至少有一名敌人时循环。所以只要其中一个数组不为空,它就会继续循环。
但是当你第一次创建this.players
and时this.enemies
,它们以不同的大小开始。然后,当您从每个数组中删除一个条目时,最终其中一个数组在另一个数组之前为空。
然后,当数组为空时,您的代码var randomEnemyIndex = Math.floor(Math.random() * this.enemies.length);
将评估为 0。当你这样做时this.enemies[0]
,它返回未定义。当undefined
被传递给weapon.attack
likeweapon.attack(randomPlayer, undefined)
时,它会尝试调用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) {
或者您需要以相同的大小启动两个阵列。
推荐阅读
- java - Android-如何处理选择的图像?
- git - 什么是针对特定提交对单个未提交文件进行 git diff 的可靠方法?
- git - 模拟预提交挂钩 Bitbucket Cloud
- html - 引导表分页下拉菜单不起作用 - Rails
- windows - 为什么火花流的小批量在 Windows 上更持久?
- xml - 将 XML 数据从基于 Web 的源导入 Google 表格
- r - 检查函数中的字符条目是否在 R 中的向量集中
- sql-server - 购买商品并在 SQL Server 中保存供应商的名称
- python - DecisionTreeClassifier 中两片叶子之间的距离
- powershell - 在几个路径中递归地计算文件,在 Powershell 中排除和消除冗余文件