-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrpg.js
50 lines (40 loc) · 1.21 KB
/
rpg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Game {
constructor(player, boss) {
this.player_hero_set = player.hero_set;
this.player = {
hp: player.hp,
damage: this.player_hero_set.damage,
defense: this.player_hero_set.defense,
cost: this.player_hero_set.cost,
};
this.boss = {
hp: boss.hp,
damage: boss.damage,
defense: boss.armor,
};
}
/**
* @returns True if the player wins, false if the boss wins
*/
run() {
let turn = 0;
while (true) {
// Player goes first
let player_damage = this.player.damage - this.boss.defense;
player_damage = player_damage < 1 ? 1 : player_damage;
this.boss.hp -= player_damage;
if (this.boss.hp <= 0) {
return true;
}
// Boss is still alive, boss's turn
let boss_damage = this.boss.damage - this.player.defense;
boss_damage = boss_damage < 1 ? 1 : boss_damage;
this.player.hp -= boss_damage;
if (this.player.hp <= 0) {
return false;
}
turn++;
}
}
}
module.exports = Game;