forked from kgolobok/fight-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfight.js
56 lines (46 loc) · 1.35 KB
/
fight.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
51
52
53
54
55
56
'use strict';
// Character Class definition
class Character {
constructor(name, health, attack, defense) {
this.name = name;
this.health = health;
this.attack = attack;
this.defense = defense;
}
}
Character.prototype.attackCharacter = function(defender) {
// Implement me!
var baseDmg = this.attack - defender.defense;
var randDmg = Math.floor((Math.random() * 5) + 1);
var totalDmg = baseDmg + randDmg;
defender.health -= totalDmg;
console.log(this.name + " does " + totalDmg + " damage to " + defender.name );
}
// Main Fight Logic
var player = new Character('Edward Norton', 100, 25, 20);
var enemy = new Character('Tyler Durden', 100, 25, 20);
var round = 1;
while (player.health && enemy.health) {
runRound(round, player, enemy);
round++;
console.log('');
}
function runRound(round, p1, p2) {
// Implement me!
console.log("----- Round " + round + " -----");
p1.attackCharacter(p2);
if (p2.health <= 0) {
endGame(p1, p2);
}
p2.attackCharacter(p1);
if (p1.health <= 0) {
endGame(p2, p1);
}
console.log(p1.name + " health: " + p1.health);
console.log(p2.name + " health: " + p2.health);
}
function endGame(winner, loser) {
console.log('\n======== GAME OVER ========');
console.log(winner.name + " wins against " + loser.name + " with " + winner.health + " health remaining!");
process.exit();
}