-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpg.js
97 lines (78 loc) · 2.34 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const shopInventory = require('./shop');
const wouldPlayerSurvive = (bossStats, playerStats) => {
let bossHp = bossStats.hp;
let playerHp = playerStats.hp;
let isPlayersTurn = true;
while (bossHp > 0 && playerHp > 0) {
if (isPlayersTurn) {
const damage = Math.max(playerStats.damage - bossStats.armor, 1);
bossHp -= damage;
// console.log(`The player deals ${damage} damage; the boss goes down to ${bossHp} hit points.`);
} else {
const damage = Math.max(bossStats.damage - playerStats.armor, 1);
playerHp -= damage;
// console.log(`The boss deals ${damage} damage; the player goes down to ${playerHp} hit points.`);
}
isPlayersTurn = !isPlayersTurn;
}
return playerHp > 0;
};
const getRingCombinations = () => {
const combinations = [
{
cost: 0,
damage: 0,
armor: 0,
},
];
shopInventory.rings.forEach((item) => {
shopInventory.rings
.filter((otherItem) => otherItem.name !== item.name)
.forEach((otherItem) => {
const combination = {
cost: item.cost + otherItem.cost,
damage: item.damage + otherItem.damage,
armor: item.armor + otherItem.armor,
};
const alreadyExists = combinations.find(({ cost, damage, armor }) => {
return combination.cost === cost &&
combination.damage === damage &&
combination.armor === armor;
});
if (!alreadyExists) {
combinations.push(combination);
}
});
});
return combinations;
};
const rpg = (bossStats, { hp }) => {
const combinations = getRingCombinations()
.reduce((combos, rings) => {
shopInventory.weapons.forEach((weapon) => {
shopInventory.armor.forEach((armor) => {
const stats = {
cost: rings.cost + weapon.cost + armor.cost,
damage: rings.damage + weapon.damage,
armor: rings.armor + armor.armor,
};
combos.push(stats);
});
});
return combos;
}, [])
.sort((a, b) => a.cost - b.cost);
for (let i = 0; i < combinations.length; i++) {
const { cost, damage, armor } = combinations[i];
const playerStats = {
hp,
damage,
armor,
};
if (wouldPlayerSurvive(bossStats, playerStats)) {
return cost;
}
}
return 0;
};
module.exports = rpg;