-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.js
295 lines (224 loc) · 7.09 KB
/
simulator.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const spells = require('./spells');
const ENABLE_DEBUG = false;
const log = function () {
if (ENABLE_DEBUG) {
console.log(...arguments);
}
};
const copy = (state) => JSON.parse(JSON.stringify(state));
let lowest = Infinity;
const getManaSpent = (state) => {
return state.spellCasts.reduce((totalCost, spellId) => totalCost + spells[spellId].cost, 0);
};
const checkGame = (state) => {
if (state.player.hp <= 0) {
log('The player died, and the boss wins.');
state.isGameOver = true;
state.isFailure = true;
} else if (state.boss.hp <= 0) {
log('The boss died, and the player wins.');
const cost = getManaSpent(state);
if (cost < lowest) {
lowest = cost;
}
state.isGameOver = true;
state.isSuccess = true;
}
return state;
};
const toggleTurn = (state) => {
state.isPlayerTurn = !state.isPlayerTurn;
return state;
};
const stopGame = (state) => {
log('Could not afford spell or spell was already active.');
state.isGameOver = true;
state.isFailure = true;
return state;
};
const print = (state) => {
state.isPlayerTurn
? log('\n-- Player turn --')
: log('\n-- Boss turn --');
log(`- Player has ${state.player.hp} hit points, ${state.player.armor} armor, ${state.player.mana} mana`);
log(`- Boss has ${state.boss.hp} hit points`);
return state;
};
const applyShield = (state) => {
const effect = state.effects.shield;
if (effect) {
effect.timer--;
log(`Shield's timer is now ${effect.timer}.`);
if (effect.timer === 0) {
log(`Shield wears off, decreasing armor by ${effect.armorWhileActive}.`);
state.player.armor -= effect.armorWhileActive;
delete state.effects.shield;
}
}
return state;
};
const applyRecharge = (state) => {
const effect = state.effects.recharge;
if (effect) {
effect.timer--;
state.player.mana += effect.manaOverTime;
log(`Recharge provides ${effect.manaOverTime} mana; its timer is now ${effect.timer}.`);
if (effect.timer === 0) {
log('Recharge wears off.');
delete state.effects.recharge;
}
}
return state;
};
const applyPoison = (state) => {
const effect = state.effects.poison;
if (effect) {
effect.timer--;
state.boss.hp -= effect.damageOverTime;
log(`Poison deals ${effect.damageOverTime} damage; its timer is now ${effect.timer}.`);
if (effect.timer === 0) {
log('Poison wears off.');
delete state.effects.poison;
}
}
return state;
};
const applyEffects = (state) => applyPoison(applyRecharge(applyShield(print(copy(state)))));
const castMagicMissile = (state) => {
const nextState = checkGame(applyEffects(state));
if (nextState.isGameOver) {
return nextState;
}
const spell = spells.magicMissile;
if (nextState.player.mana >= spell.cost) {
log(`Player casts ${spell.name}, dealing ${spell.damage} damage.`);
nextState.player.mana -= spell.cost;
nextState.spellCasts.push('magicMissile');
nextState.boss.hp -= spell.damage;
}
return checkGame(toggleTurn(nextState));
};
const castDrain = (state) => {
const nextState = checkGame(applyEffects(state));
if (nextState.isGameOver) {
return nextState;
}
const spell = spells.drain;
if (nextState.player.mana >= spell.cost) {
log(`Player casts ${spell.name}, dealing ${spell.damage} damage, and healing ${spell.heals} hit points.`);
nextState.player.mana -= spell.cost;
nextState.spellCasts.push('drain');
nextState.boss.hp -= spell.damage;
nextState.player.hp += spell.heals;
}
return checkGame(toggleTurn(nextState));
};
const castShield = (state) => {
const nextState = checkGame(applyEffects(state));
if (nextState.isGameOver) {
return nextState;
}
const spell = spells.shield;
if (!nextState.effects.shield && nextState.player.mana >= spell.cost) {
log(`Player casts ${spell.name}, increasing armor by ${spell.armorWhileActive}.`);
nextState.player.mana -= spell.cost;
nextState.spellCasts.push('shield');
nextState.effects.shield = Object.assign({}, spell, { timer: spell.lasts });
nextState.player.armor = spell.armorWhileActive;
} else {
return checkGame(stopGame(nextState));
}
return checkGame(toggleTurn(nextState));
};
const castPoison = (state) => {
const nextState = checkGame(applyEffects(state));
if (nextState.isGameOver) {
return nextState;
}
const spell = spells.poison;
if (!nextState.effects.poison && nextState.player.mana >= spell.cost) {
log(`Player casts ${spell.name}.`);
nextState.player.mana -= spell.cost;
nextState.spellCasts.push('poison');
nextState.effects.poison = Object.assign({}, spell, { timer: spell.lasts });
} else {
return checkGame(stopGame(nextState));
}
return checkGame(toggleTurn(nextState));
};
const castRecharge = (state) => {
const nextState = checkGame(applyEffects(state));
if (nextState.isGameOver) {
return nextState;
}
const spell = spells.recharge;
if (!nextState.effects.recharge && nextState.player.mana >= spell.cost) {
log(`Player casts ${spell.name}.`);
nextState.player.mana -= spell.cost;
nextState.spellCasts.push('recharge');
nextState.effects.recharge = Object.assign({}, spell, { timer: spell.lasts });
} else {
return checkGame(stopGame(nextState));
}
return checkGame(toggleTurn(nextState));
};
const bossAttack = (state) => {
const nextState = checkGame(applyEffects(state));
if (nextState.isGameOver) {
return nextState;
}
const damage = Math.max(nextState.boss.damage - nextState.player.armor, 1);
nextState.player.armor > 0
? log(`Boss attacks for ${nextState.boss.damage} - ${nextState.player.armor} = ${damage} damage!`)
: log(`Boss attacks for ${damage} damage!`);
nextState.player.hp -= damage;
return checkGame(toggleTurn(nextState));
};
const permute = (state, winningGames = []) => {
if (getManaSpent(state) > lowest) {
return winningGames;
}
const nextStates = [
bossAttack(castMagicMissile(state)),
bossAttack(castDrain(state)),
bossAttack(castShield(state)),
bossAttack(castPoison(state)),
bossAttack(castRecharge(state)),
];
const activeGames = nextStates.filter((game) => !game.isGameOver && !game.isFailure);
winningGames = nextStates.filter((game) => game.isGameOver && game.isSuccess);
for (let i = 0; i < activeGames.length; i++) {
winningGames = [
...winningGames,
...permute(activeGames[i], winningGames),
];
}
return winningGames;
};
module.exports = (input, player = { hp: 50, mana: 500 }) => {
const boss = {
hp: parseInt(input.match(/Hit Points: (\d+)/m)[1]),
damage: parseInt(input.match(/Damage: (\d+)/m)[1]),
};
const initialState = {
isGameOver: false,
isPlayerTurn: true,
isFailure: false,
isSuccess: false,
boss: {
hp: boss.hp,
damage: boss.damage,
},
player: {
hp: player.hp,
mana: player.mana,
armor: 0,
},
spellCasts: [],
effects: {},
};
const states = permute(initialState);
return states
.map((state) => state.spellCasts.reduce((totalCost, spellId) => totalCost + spells[spellId].cost, 0))
.sort((a, b) => a - b)[0];
};