Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit a7d2beb

Browse files
committed
RPG refactored
1 parent 7f240a7 commit a7d2beb

File tree

1 file changed

+56
-40
lines changed

1 file changed

+56
-40
lines changed

src/lesson5/task.js

+56-40
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,62 @@
1-
1+
// Hero classes
22
const heroClasses = {
33
warrior: { charClass: "Warrior", life: 30, damage: 4 },
44
rogue: { charClass: "Rogue", life: 25, damage: 3 },
55
sorcerer: { charClass: "Sorcerer", life: 20, damage: 5 }
66
};
77

8-
// Monsters classes and randomizer
8+
// Monsters classes
99
const monsterClasses = {
1010
zombie: { charClass: "Zombie", life: 8, damage: 4 },
1111
skeleton: { charClass: "Skeleton", life: 10, damage: 6 },
1212
holem: { charClass: "Holem", life: 15, damage: 6 }
1313
};
1414

15-
// Hero and Monsters constructors
15+
// Char
16+
const Char = function() {};
17+
18+
Char.prototype.getName = function(){
19+
return this.heroName || `I am ${this.charClass} I don\`t have name`;
20+
};
21+
Char.prototype.getCharClass = function(){ return this.charClass; };
22+
23+
Char.prototype.attack = function(enemy) {
24+
let attacker, attacked;
25+
if (this instanceof Hero) {
26+
if (enemy instanceof Hero) return "I will attack only monsters";
27+
attacker = 'Hero';
28+
attacked = enemy.charClass;
29+
}
30+
if (this instanceof Monster) {
31+
if (enemy instanceof Monster) return "I will attack only Hero";
32+
attacker = this.charClass;
33+
attacked = 'Hero';
34+
}
35+
enemy.life = Math.max(enemy.life - this.damage, 0);
36+
if (!enemy.life) return (`${attacker} attacked, ${attacked} killed`);
37+
return (`${attacker} attacked, done ${this.damage} damage to ${attacked}`);
38+
};
39+
40+
41+
// Hero and Monster constructors
1642
const Hero = function(name, charClass) {
1743
if (heroClasses.hasOwnProperty(charClass)) {
1844
this.heroName = name;
1945
Object.assign(this, heroClasses[charClass]);
20-
this.getName = function(){ return this.heroName; };
21-
this.getCharClass = function(){ return this.charClass; };
2246
}
2347
else throw new Error("Incorrect character class provided");
2448
};
2549

2650
const Monster = function(charClass) {
2751
if (monsterClasses.hasOwnProperty(charClass)) {
2852
Object.assign(this, monsterClasses[charClass]);
29-
this.getName = function(){ return (`I am ${this.charClass} I don\`t have name`); };
30-
this.getCharClass = function(){ return this.charClass; };
3153
}
3254
else throw new Error("Incorrect character class provided");
3355
};
3456

35-
36-
Hero.prototype.attack = function(enemy) {
37-
if (enemy instanceof Hero) return "I will attack only monsters";
38-
else {
39-
enemy.life = Math.max(enemy.life - this.damage, 0);
40-
if (!enemy.life) return (`Hero attacked, ${enemy.charClass} killed`);
41-
return (`Hero attacked, done ${this.damage} damage to ${enemy.charClass}`);
42-
}
43-
};
44-
45-
Monster.prototype.attack = function(enemy) {
46-
if (enemy instanceof Monster) return "I will attack only Hero";
47-
else {
48-
enemy.life = Math.max(enemy.life - this.damage, 0);
49-
if (!enemy.life) return (`${this.charClass} attacked, Hero killed`);
50-
return (`${this.charClass} attacked, done ${this.damage} damage to ${enemy.charClass}`);
51-
}
52-
};
53-
54-
// GENERAL_ATTACK_MESSAGE
55-
// "CHARACTER_CLASS killed" - this action will kill target
56-
// "done AMOUNT_OF_DAMAGE damage to CHARACTER_CLASS";
57+
Hero.prototype = new Char();
58+
Monster.prototype = new Char();
59+
//--------------------------------
5760

5861
const statuses = {
5962
idle : "Idle",
@@ -66,9 +69,9 @@ const Game = function() {
6669
this.status = statuses.idle;
6770
};
6871

69-
Object.defineProperty(Game.prototype, 'lives', {
72+
Object.defineProperty(Game.prototype, 'monstersAlive', {
7073
get: function() {
71-
return this.monsters[0].life + this.monsters[1].life;
74+
return this.monsters.some(monster => monster.life > 0);
7275
}
7376
});
7477

@@ -78,7 +81,7 @@ Game.prototype.addHero = function(hero) {
7881
else if (this.hero) throw new Error ("Only one hero can exist");
7982
else {
8083
this.hero = hero;
81-
return ("Hero created, welcome %s", this.hero.heroName);
84+
return (`Hero created, welcome ${this.hero.heroName}`);
8285
}
8386
};
8487

@@ -110,7 +113,7 @@ Game.prototype.finishJourney = function() {
110113
this.status = statuses.finished;
111114
return "The Game is finished. Hero is dead :(";
112115
}
113-
else if (this.monsters[0].life + this.monsters[1].life === 0) {
116+
else if (!this.monstersAlive) {
114117
return "The Game is finished. Monsters are dead. Congratulations";
115118
}
116119
else return "Don`t stop. Some monsters are still alive. Kill`em all";
@@ -121,32 +124,45 @@ Game.prototype.fight = function() {
121124
throw new Error("Begin your journey to start fighting monsters");
122125
}
123126

127+
if (!this.hero.life || !this.monstersAlive) return this.finishJourney();
128+
124129
let currentMonster;
125-
if (this.monsters[0].life) currentMonster = this.monsters[0];
126-
else if (this.monsters[1].life) currentMonster = this.monsters[1];
130+
for (let m of this.monsters) {
131+
if (m.life) {
132+
currentMonster = m;
133+
break;
134+
}
135+
}
127136

128137
while (this.hero.life && currentMonster.life) {
129138
this.hero.attack(currentMonster);
130139
if (currentMonster.life) currentMonster.attack(this.hero);
131140
}
132-
if (!this.hero.life) return "Monster win";
133-
else if (!currentMonster.life) return "Hero win";
141+
if (!this.hero.life) {
142+
this.status = statuses.finished;
143+
return "Monster win";
144+
}
145+
else if (!this.monstersAlive) {
146+
this.status = statuses.finished;
147+
return "Hero win";
148+
}
149+
return "Hero win";
134150
};
135151

136152

137153
/* Game Population mechanism should go below */
138154

139155
Game.prototype.populate = function() {
140156
const heroName = prompt('Name your Hero:', 'Chuck Norris');
141-
const heroClass = prompt('What is your Hero?', 'warrior | rogue | sorcerer | random').toLowerCase().trim();
157+
const heroClass = prompt('What is your Hero?', 'warrior | rogue | sorcerer | random').trim();
142158
if (heroClass === 'random') this.addRandomHero(heroName);
143159
else {
144160
const theHero = new Hero(heroName, heroClass);
145161
this.addHero(theHero);
146162
}
147163

148-
while (this.monsters.length < 2) {
149-
const monsterClass = prompt(`What is your Monster ${this.monsters.length+1}?`, 'zombie | skeleton | holem | random').toLowerCase().trim();
164+
while (this.monsters.length < Game.maxMonsters) {
165+
const monsterClass = prompt(`What is your Monster ${this.monsters.length+1}?`, 'zombie | skeleton | holem | random').trim();
150166
if (monsterClass === 'random') this.addRandomMonster();
151167
else {
152168
const theMonster = new Monster(monsterClass);

0 commit comments

Comments
 (0)