-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtwo-fighters-one-winner.js
73 lines (59 loc) · 2.12 KB
/
two-fighters-one-winner.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// class Fighter {
// constructor(name, health, damagePerAttack) {
// this.name = name;
// this.health = health;
// this.damagePerAttack = damagePerAttack;
// }
// attack(fighter) {
// fighter.takeDamage(this.damagePerAttack);
// }
// takeDamage(damage) {
// this.health -= damage;
// }
// isAlive() {
// return this.health > 0;
// }
// }
function Fighter(name, health, damagePerAttack) {
this.name = name;
this.health = health;
this.damagePerAttack = damagePerAttack;
this.toString = function() { return this.name; };
// this.attack = (fighter) => {
// fighter.takeDamage(this.damagePerAttack);
// };
// this.takeDamage = (damage) => {
// this.health -= damage;
// };
// this.isAlive = () => {
// return this.health > 0;
// };
}
Fighter.prototype.attack = function(fighter) {
fighter.takeDamage(this.damagePerAttack);
};
Fighter.prototype.takeDamage = function(damage) {
this.health -= damage;
};
Fighter.prototype.isAlive = function() {
return this.health > 0;
};
function declareWinner(fighter1, fighter2, firstAttackerName) {
let currentAttacker = fighter1;
let currentDefender = fighter2;
if (fighter1.name !== firstAttackerName) {
currentAttacker = fighter2;
currentDefender = fighter1;
}
while(fighter1.health > 0 && fighter2.health > 0) {
currentAttacker.attack(currentDefender);
if (!currentDefender.isAlive()) return currentAttacker.name;
[currentDefender, currentAttacker] = [currentAttacker, currentDefender];
}
}
console.log(declareWinner(new Fighter('Lew', 10, 2), new Fighter('Harry', 5, 4), 'Lew'), 'Lew');
console.log(declareWinner(new Fighter('Lew', 10, 2), new Fighter('Harry', 5, 4), 'Harry'), 'Harry');
console.log(declareWinner(new Fighter('Harald', 20, 5), new Fighter('Harry', 5, 4), 'Harry'), 'Harald');
console.log(declareWinner(new Fighter('Harald', 20, 5), new Fighter('Harry', 5, 4), 'Harald'), 'Harald');
console.log(declareWinner(new Fighter('Jerry', 30, 3), new Fighter('Harald', 20, 5), 'Jerry'), 'Harald');
console.log(declareWinner(new Fighter('Jerry', 30, 3), new Fighter('Harald', 20, 5), 'Harald'), 'Harald');