Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function NPC(name, hitpoints) {
- this.name = name;
- this.hitpoints = hitpoints;
- }
- function Hero(name) {
- NPC.call(this, name, 100);
- this.damage = 10;
- this.critical = false;
- }
- Hero.prototype.attack = function (defender) {
- if (defender instanceof Hero && defender.critical === true && this instanceof Hero) {
- defender.hitpoints -= 15;
- if (defender.hitpoints > 0) {
- defender.critical = true;
- this.critical = false;
- } else {
- this.critical = false;
- }
- } else {
- defender.hitpoints -= 10;
- }
- console.log(this.name + " attacked " + defender.name + ". Defender hitpoints = " + defender.hitpoints);
- };
- Hero.prototype.status = function () {
- return this.name + " " + this.hitpoints;
- };
- //Creating list of characters
- let N = Math.floor(Math.random() * 41) + 10;
- let characters = [];
- for (let i = 0; i < N; i++) {
- let random = Math.floor(Math.random() * 2) + 1;
- random === 1 ? characters.push(new Hero("Hero_" + i)) : characters.push(new NPC("NPC_" + i, 100));
- }
- //end of Creating list of characters
- //Choosing random Hero player for critical damage
- while (true) {
- let random = Math.floor(Math.random() * characters.length);
- if (characters[random] instanceof Hero) {
- characters[random].critical = true;
- break;
- }
- }
- console.log(characters);
- //end of Choosing random Hero player for critical damage
- //Battle of titans
- let go = true;
- function battle(characters) {
- while (go) {
- for (let i = 0; i < characters.length; i++) {
- while (true) {
- var random = Math.floor(Math.random() * characters.length);
- if (random !== i) {
- break;
- }
- }
- if (characters[i] instanceof Hero) {
- characters[i].attack(characters[random]);
- if (characters[random].hitpoints <= 0) {
- characters.splice(random, 1);
- }
- console.log(characters[i].status());
- }
- if (characters.length === 1) {
- console.log("Winner: " + characters[0].name + ", hitpoints: " + characters[0].hitpoints + "!");
- go = false;
- break;
- }
- }
- }
- }
- //end of Battle of titans
- battle(characters);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement