JSFiddle - React, Tailwind, and code Playground
JavaScript
"use strict";
//class to create weapons for units
class Weapon {
constructor(name, damage) {
this.name = name;
this.damage = damage;
}
getDamage() {
return this.damage;
}
toString() {
return `${this.name} damage: (${this.getDamage()}) points`;
}
}
//constructor for creating a unit
class Unit {
constructor(maxHealth, basicDamage, evasion, type) {
this.maxHealth = maxHealth;
this.currentHealth = maxHealth;
this.basicDamage = basicDamage;
this.evasion = evasion;
this.type = type;
this.dead = false;
}
/*method for showing the status of life, true if the "health" is greater
than 0 and false if equal to or lower */
isAlive() {
if (this.dead) {
return false;
}
if (this.currentHealth <= 0) {
console.log(`${this.name} die!`);
this.dead = true;
return false;
}
return true;
}
/* a method that
shows the level of health*/
getFormattedHealth() {
return `[${this.currentHealth}/${this.maxHealth}]`;
}
/*method which fills an array with 10 digit of 0s and 1s according
to evasion and return a random 1 or 0:*/
probability() {
let notRandomNumbers = [];
let maxEvasion = this.evasion * 10;
let i;
let idx;
for (i = 0; i < maxEvasion; i++) {
notRandomNumbers.push(1);
}
for (i = 0; i < 10 - maxEvasion; i++) {
notRandomNumbers.push(0);
}
idx = Math.floor(Math.random() * notRandomNumbers.length);
if (!notRandomNumbers[idx]) {
console.log(`${this.name} miss`);
}
return notRandomNumbers[idx];
}
/* The method that defines the weapon created in the constructor "Weapon"*/
setWeapon(weapon) {
try {
if (!(weapon instanceof Weapon)) {
throw new...