Question
Javascript -- I'm working on an assignment to make a short game, battle.js The code I have is : function Player(name, weapons) { this.Name =
Javascript -- I'm working on an assignment to make a short game, battle.js
The code I have is :
function Player(name, weapons) { this.Name = name; this.Health = 10; this.Strength = 2; this.Weapons = weapons; }
Player.prototype.applyDamage = function(damage) { this.Health = this.Health - damage; console.log(" " + this.Name + " has sustained " + damage + " amount of damage."); }
Player.prototype.isAlive = function() { if (this.Health > 0) { return true; } else { return false; } }
Player.prototype.attackWith = function() { var index = Math.floor(Math.random() * 8); return this.Weapons[index]; }
function Weapon(name) { this.Name = name; this.Damage = Math.floor(Math.random() * 5) + 1; }
Weapon.prototype.attack = function(player, enemy) { while (player.isAlive() && enemy.isAlive()) { var damage = player.Strength * this.Damage; enemy.applyDamage(damage); if (enemy.isAlive()) { enemy.attack(player); } } } function Enemy() { this.Name = "Enemy"; this.Health = 5; this.Strength = 2; }
Enemy.prototype.applyDamage = function(damage) { this.Health = this.Health - damage;
console.log(" " + this.Name + " has sustained " + damage + " amount of damage " ); }
Enemy.prototype.isAlive = function() { if (this.Health > 0) { return true; } else { return false; } }
Enemy.prototype.attack = function(player) { player.applyDamage(this.Strength); } function Game() { this.Players = []; this.Enemys = [];
}
Game.prototype.createEnemys = function() { while (this.Enemys.length < 20) { this.Enemys.push(new Enemy()); } }
Game.prototype.createPlayers = function() { var weaponsCache = []; weaponsCache.push(new Weapon("Tiriosh")); weaponsCache.push(new Weapon("Dragonwrath")); weaponsCache.push(new Weapon("Shadowmourne")); weaponsCache.push(new Weapon("Infinity Blade")); weaponsCache.push(new Weapon("Netherstrand Long Bow")); weaponsCache.push(new Weapon("Atiesh")); weaponsCache.push(new Weapon("Sulfuras")); weaponsCache.push(new Weapon("ThunderFury")); this.Players.push(new Player("Korgull", weaponsCache)); this.Players.push(new Player("Zexidamus", weaponsCache)); this.Players.push(new Player("Sylania", weaponsCache)); this.Players.push(new Player("Zhadur", weaponsCache)); this.Players.push(new Player("Brailli", weaponsCache)); }
Game.prototype.play = function() { this.createEnemys(); this.createPlayers(); do { var playerIndex = Math.floor(Math.random() * 5); var player = this.Players[playerIndex]; var enemyIndex = Math.floor(Math.random() * 20); var enemy = this.Enemys[enemyIndex]; var weapon = player.attackWith(); weapon.attack(player, enemy); var enemysStillAlive = false; for (i = 0; i < this.Enemys.length; i++) { if (this.Enemys[i].isAlive()) { enemysStillAlive = true; break; } } if (!enemysStillAlive) { break; } var playersStillAlive = false; for (i = 0; i < this.Players.length; i++) { if (this.Players[i].isAlive()) { playersStillAlive = true; break; } } if (!playersStillAlive) { break; } } while (true); var playersStillAlive = 0; var enemysStillAlive = 0; for (i = 0; i < this.Players.length; i++) { if (this.Players[i].isAlive()) { playersStillAlive++; console.log(" " + this.Players[i].Name + " is still alive!"); } }
for (i = 0; i < this.Enemys.length; i++) { if (this.Enemys[i].isAlive()) { enemysStillAlive++; console.log("Sorry, the Scarlet Byte has defeated you and conquered the free world."); } }
if (playersStillAlive > enemysStillAlive) { console.log(" Congratulations, you have defeated Scarlet Byte."); } }
var game = new Game(); game.play();
----------------------------------------
the code works, but I want to be able to add to the output
Enemy has sustained X amount of damage from weapon
where weapon is one of the weapons in the weaponsCache
Everything I've tried I get errors, and now im at a loss as to what to try next.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started