-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22.py
executable file
·235 lines (188 loc) · 7.38 KB
/
22.py
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
def inc_timer(cur_spells):
spells = {}
for t, sp in cur_spells.items():
if 'timer' in sp:
tmr = sp['timer'] - 1
if tmr >= 0:
spells[t] = dict(sp)
spells[t]['timer'] = tmr
return spells
def copy_cur_spells(d):
{x:dict(y) for x,y in d.items()}
def get_spells_res(cur_spells, indent):
global output
ret = {'damage': 0, 'hp': 0, 'armor': 0, 'mana': 0}
for sp in cur_spells.values():
if 'damage' in sp:
ret['damage'] += sp['damage']
if output:
print(indent, '{} provides {} damage, timer now {}'.format(sp['t'], sp['damage'], sp['timer']))
if 'hp' in sp:
ret['hp'] += sp['hp']
if output:
print(indent, '{} provides {} hp, timer now {}'.format(sp['t'], sp['hp'], sp['timer']))
if 'armor' in sp:
ret['armor'] += sp['armor']
if output:
print(indent, '{} provides {} armor, timer now {}'.format(sp['t'], sp['armor'], sp['timer']))
if 'mana' in sp:
ret['mana'] += sp['mana']
if output:
print(indent, '{} provides {} mana, timer now {}'.format(sp['t'], sp['mana'], sp['timer']))
return ret
def get_spell_res(sp, indent):
ret = {'damage': 0, 'hp': 0, 'armor': 0, 'mana': 0}
if 'timer' not in sp:
if 'damage' in sp:
ret['damage'] += sp['damage']
if output:
print(indent, '{} provides {} damage'.format(sp['t'], sp['damage']))
if 'hp' in sp:
ret['hp'] += sp['hp']
if output:
print(indent, '{} provides {} hp'.format(sp['t'], sp['hp']))
if 'armor' in sp:
ret['armor'] += sp['armor']
if output:
print(indent, '{} provides {} armor'.format(sp['t'], sp['armor']))
if 'mana' in sp:
ret['mana'] += sp['mana']
if output:
print(indent, '{} provides {} mana'.format(sp['t'], sp['mana']))
return ret
def apply_spells(sp, me, boss, apply_hp_cost):
global hp_turn_cost
new_me = dict(me)
new_boss = dict(boss)
new_me['hp'] += sp['hp']
new_me['mana'] += sp['mana']
new_me['armor'] = sp['armor']
new_boss['hp'] -= sp['damage']
if apply_hp_cost and hp_turn_cost > 0:
new_me['hp'] -= hp_turn_cost
return (new_me, new_boss)
def run_game(me, boss, next_spell, cur_spells, cur_spend, depth):
global metawins
global min_mana_win
global outputLoss
global hp_turn_cost
indent = ' ' * depth
if cur_spend > min_mana_win:
return
# start player turn
if output:
print(indent, '-- Player turn --')
print(indent, '- Player has {} hit points, {} armor, {} mana'.format(me['hp'], me['armor'], me['mana']))
print(indent, '- Boss has {} hit points'.format(boss['hp']))
if hp_turn_cost > 0:
if me['hp'] <= 1:
if output:
print(indent, ' @ player loses', 'boss =', boss['hp'])
return
new_cur_spells = inc_timer(cur_spells)
if next_spell['t'] in new_cur_spells and new_cur_spells[next_spell['t']]['timer'] > 0:
return
spell_res = get_spells_res(new_cur_spells, indent)
(new_me, new_boss) = apply_spells(spell_res, me, boss, True)
# check for boss death
if new_boss['hp'] <= 0:
if output or outputLoss:
print(indent, ' ! boss loses', 'player =', me['hp'], 'spend =', cur_spend)
manawins.append(cur_spend)
if cur_spend < min_mana_win:
min_mana_win = cur_spend
#print(indent, ' ! boss loses', 'player =', me['hp'], 'spend =', cur_spend)
print('new min', cur_spend)
return
if output:
print(indent, 'Player casts', next_spell['t'])
new_me['mana'] -= next_spell['cost']
cur_spend += next_spell['cost']
if new_me['mana'] < 0:
if output:
print(indent, ' @ player loses, no mana', 'boss =', new_boss['hp'], 'player =', new_me['hp'])
return
if 'timer' in next_spell:
new_cur_spells[next_spell['t']] = dict(next_spell)
spell_res = get_spell_res(next_spell, indent)
(new_me, new_boss) = apply_spells(spell_res, new_me, new_boss, False)
# check for boss death
if new_boss['hp'] <= 0:
if output or outputLoss:
print(indent, ' ! boss loses', 'player =', new_me['hp'], 'spend =', cur_spend)
manawins.append(cur_spend)
if cur_spend < min_mana_win:
min_mana_win = cur_spend
#print(indent, ' ! boss loses', 'player =', me['hp'], 'spend =', cur_spend)
print('new min', cur_spend)
return
# start boss turn
if output:
print(indent, '-- Boss turn --')
print(indent, '- Player has {} hit points, {} armor, {} mana'.format(new_me['hp'], new_me['armor'], new_me['mana']))
print(indent, '- Boss has {} hit points'.format(new_boss['hp']))
new_cur_spells = inc_timer(new_cur_spells)
spell_res = get_spells_res(new_cur_spells, indent)
(new_me, new_boss) = apply_spells(spell_res, new_me, new_boss, False)
# check for boss death
if new_boss['hp'] <= 0:
if output or outputLoss:
print(indent, ' ! boss loses', 'player =', new_me['hp'], 'spend =', cur_spend)
manawins.append(cur_spend)
if cur_spend < min_mana_win:
min_mana_win = cur_spend
#print(indent, ' ! boss loses', 'player =', me['hp'], 'spend =', cur_spend)
print('new min', cur_spend)
return
if output:
print(indent, 'Boss attacked for {} damage!'.format(new_boss['damage'] - new_me['armor']))
new_me['hp'] -= max(1, new_boss['damage'] - new_me['armor'])
# check for player death
if new_me['hp'] <= 0:
if output:
print(indent, ' @ player loses', 'boss =', new_boss['hp'])
return
# loop through spells not in new_cur_spells, call run_game for each
for next_sp in spells:
run_game(new_me, new_boss, next_sp, new_cur_spells, cur_spend, depth + 1)
spells = []
spells.append({'t': 'Poison', 'cost': 173, 'damage': 3, 'timer': 6})
spells.append({'t': 'Recharge', 'cost': 229, 'mana': 101, 'timer': 5})
spells.append({'t': 'Sheild', 'cost': 113, 'armor': 7, 'timer': 6})
spells.append({'t': 'Magic Missle', 'cost': 53, 'damage': 4})
spells.append({'t': 'Drain', 'cost': 73, 'damage': 2, 'hp': 2})
# boss
# Hit Points: 55
# Damage: 8
# my hp = 50
# mana = 500
# test 1
# me = {'hp': 10, 'mana': 250, 'armor': 0, 'damage': 0}
# boss = {'hp': 13, 'mana': 0, 'armor': 0, 'damage': 8}
# test 2
# me = {'hp': 10, 'mana': 250, 'armor': 0, 'damage': 0}
# boss = {'hp': 14, 'mana': 0, 'armor': 0, 'damage': 8}
output = False
outputLoss = False
# part 1
me = {'hp': 50, 'mana': 500, 'armor': 0, 'damage': 0}
boss = {'hp': 55, 'mana': 0, 'armor': 0, 'damage': 8}
hp_turn_cost = 0
manawins = []
min_mana_win = 999999999
for next_sp in spells:
print('starting with', next_sp)
run_game(me, boss, next_sp, {}, 0, 0)
#print('manawins', manawins)
print('part1, manawins', min(manawins))
# part 2
me = {'hp': 50, 'mana': 500, 'armor': 0, 'damage': 0}
boss = {'hp': 55, 'mana': 0, 'armor': 0, 'damage': 8}
hp_turn_cost = 1
manawins = []
min_mana_win = 999999999
for next_sp in spells:
print('starting with', next_sp)
run_game(me, boss, next_sp, {}, 0, 0)
#print('manawins', manawins)
print('part2, manawins', min(manawins))