-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
351 lines (292 loc) · 11.6 KB
/
player.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from pokedex import *
from pokemon import *
from pkmn_types import get_effectiveness
import random
ATTACK = 0
SWITCH = 1
class Action:
def __init__(self, action, user, target = None):
self.action = action
self.user = user
self.target = target # Pokemon() if action is switch, Move() if action is Attack
# general Trainer superclass
# (for player and agent)
class Trainer:
def __init__(self):
self.team = [None, None, None, None, None, None] # Trainer Pokemon team
self.token = None # token used to assingnate actual turn
self.is_ai = False
for i in range(len(self.team)):
tmp = random.choice(list(pokedex_list.items()))[1]
self.team[i] = Pokemon(tmp.num, tmp.species, tmp.elements, 100, tmp.base_stats)
self.in_battle = self.team[0]
self.team[0].on_field = True
def get_team_with_stats(self):
for pkmn in self.team:
pkmn.get_stats()
pkmn.get_moves()
def get_team(self):
if not self.is_ai:
print('Player Team:')
else: print('AI Team:')
for pkmn in self.team:
print('- {mon} \t{types}'.format(mon = pkmn.name, types = pkmn.typing))
def get_possible_choices(self):
possible_choices = [ ]
for move in self.in_battle.moves:
if move != None:
if move.pp > 0:
possible_choices.append(Action(ATTACK, self.in_battle, move))
return possible_choices
def print_choices(self, choices):
print('\n{pkmn}\'s possible choices:'.format(pkmn = self.in_battle.name))
for i in range(len(choices)):
print('- {index}) name: {move_name},\
\tpower: {move_power},\
\ttype: {move_type},\
\tkind: {move_kind}'.format(index = i+1,\
move_name = choices[i].target.name,\
move_power = choices[i].target.power,\
move_type = choices[i].target.typing,\
move_kind = choices[i].target.physical))
def game_over_lose(self):
faint_cnt = 0
for pkmn in self.team:
if pkmn.fainted:
faint_cnt += 1
if faint_cnt == 6:
return True
else:
return False
def is_turn(self):
return self.token
def set_turn(self, _token):
self.token = _token
# general AI class
# (for all kinds of agent)
class TrainerAI(Trainer):
def __init__(self):
super(TrainerAI, self).__init__()
self.is_ai = True
def verify_fainted_switch(self):
if self.game_over_lose() == False:
if self.in_battle.fainted == True:
self.in_battle.on_field = False
i = 0
while True:
if self.team[i].fainted == True:
i += 1
else:
self.in_battle = self.team[i]
self.team[i].on_field = True
break
# RandomAI: does random actions
# generally used for testing
class RandomAI(TrainerAI):
def __init__(self):
super(RandomAI, self).__init__()
self.choices = [ ]
def get_choice(self, rival):
target = rival
if self.is_turn():
self.verify_fainted_switch()
move = None
while move == None:
move = random.choice(self.in_battle.moves)
print(move.name)
self.choices.append(move.name)
self.in_battle.try_atk_status(move, target)
# base MiniMax: ai tries to maximize the value function,
# while the player tries to minimize it
class MinimaxAI(TrainerAI):
# depth to edit
def __init__(self, rival, max_play_depth = 7):
super(MinimaxAI, self).__init__()
self.choices = [ ]
self.win_val = 1000000
self.max_play_depth = max_play_depth
self.last_move = None
self.rival = rival
# computes evaluation function; it's based on:
# - total actual hp;
# - total max hp;
# - total stats;
# - number of pkmn with status;
# - number of fainted pkmn
def evaluate(self, action):
# self vars
s_hp = 0
s_hp_full = 0
s_stats = 0
s_status = 0
s_fainted = 0
for pkmn in self.team:
s_hp += pkmn.hp
s_hp_full += pkmn.max_hp
s_stats += pkmn.atk_mult + pkmn.def_mult + pkmn.sp_atk_mult + pkmn.sp_def_mult + pkmn.speed_mult + pkmn.acc_mult + pkmn.ev_mult
if pkmn.status != None and pkmn.fainted == False:
s_status += 1
if pkmn.fainted != False:
s_fainted += 1
# target vars
t_hp = 0
t_hp_full = 0
t_stats = 0
t_status = 0
t_fainted = 0
for pkmn in self.rival.team:
t_hp += pkmn.hp
t_hp_full += pkmn.max_hp
t_stats += pkmn.atk_mult + pkmn.def_mult + pkmn.sp_atk_mult + pkmn.sp_def_mult + pkmn.speed_mult + pkmn.acc_mult + pkmn.ev_mult
if pkmn.status != None and pkmn.fainted == False:
t_status += 1
if pkmn.fainted != False:
t_fainted += 1
hp_diff = (s_hp_full - t_hp_full) - (s_hp - t_hp)
status_diff = t_status - s_status
stats_diff = s_stats - t_stats
fainted_diff = t_fainted - s_fainted
move = action.target
user = action.user
move_damage = self.in_battle.calculate_damage(move, self.rival.in_battle)
print('hp_diff:', hp_diff)
print('status_diff:', status_diff)
print('stats_diff:', stats_diff)
print('fainted_diff:', fainted_diff)
print('user:', user.name)
print('possible move:', move.name)
print('possible damage:', move_damage)
value = hp_diff * .35 + move_damage * .35 + status_diff * 100 * .25 + stats_diff * 100 * .05 + fainted_diff * 100
# malus if move was used last turn
if move == self.last_move:
value -= 100
type1 = pkmn_types.get_effectiveness(move.typing, self.rival.in_battle.typing[0]) # effectiveness vs enemy's type1
type2 = 1
if len(self.rival.in_battle.typing) == 2:
type2 = pkmn_types.get_effectiveness(move.typing, self.rival.in_battle.typing[1]) # effectiveness vs enemy's type1
# effectiveness bonus/malus
if type1 * type2 == 4:
value += 100
elif type1 * type2 == 2:
value += 50
elif type1 * type2 == 0.5:
value -= 50
elif type1 * type2 == 0:
value -= 100
print('value: {value}\n'.format(value = value))
return value
def get_choice(self, target):
if self.is_turn():
self.verify_fainted_switch()
choosen_action = None
while choosen_action == None:
possible_choices = self.get_possible_choices()
self.print_choices(possible_choices)
if len(possible_choices) >= 1:
best_action = possible_choices[0]
best_val = -float('inf')
for action in possible_choices:
val = self.minimax(self.max_play_depth, action, True)
if val >= best_val:
best_action = action
best_val = val
choosen_action = best_action
self.last_move = choosen_action
else:
choosen_action = 'no_pp'
if choosen_action != 'no_pp':
if choosen_action.action == ATTACK:
move = choosen_action.target
print('Choosen move:', move.name)
self.choices.append(move.name)
self.in_battle.try_atk_status(move, target)
else:
# simply trigger struggle
self.in_battle.struggle_no_pp(target)
def minimax(self, depth, action, is_maximizing):
print('\n--- NODE DEPTH: {depth} ---'.format(depth = depth))
if self.game_over_lose() or self.rival.game_over_lose():
if self.game_over_lose():
return -self.win_val
else:
return self.win_val
elif depth == 0:
return self.evaluate(action)
if is_maximizing:
best_val = -float('inf')
for move in self.get_possible_choices():
val = self.minimax(depth - 1, move, False)
best_val = max(best_val, val)
return best_val
else:
best_val = float('inf')
for move in self.rival.get_possible_choices():
val = self.minimax(depth - 1, move, True)
best_val = min(best_val, val)
return best_val
# AlphaBeta Pruning Minimax
# the algorithm does a cut-off of all those edges that
# it doesn't need to explore, through the the update of
# the alpha and beta values
class MMAlphaBetaAI(MinimaxAI):
def __init__(self, rival, max_play_depth = 20):
super(MMAlphaBetaAI, self).__init__(rival)
self.alpha = -float('inf')
self.beta = -self.alpha
def minimax(self, depth, action, is_maximizing):
print('\n--- NODE DEPTH: {depth} ---'.format(depth = depth))
if self.game_over_lose() or self.rival.game_over_lose():
if self.game_over_lose():
return -self.win_val
else:
return self.win_val
elif depth == 0:
return self.evaluate(action)
if is_maximizing:
best_val = -float('inf')
for move in self.get_possible_choices():
val = self.minimax(depth - 1, move, False)
best_val = max(best_val, val)
self.alpha = max(self.alpha, best_val)
if self.beta <= self.alpha:
break
return best_val
else:
best_val = float('inf')
for move in self.rival.get_possible_choices():
val = self.minimax(depth - 1, move, True)
best_val = min(best_val, val)
self.beta = min(self.beta, best_val)
if self.beta <= self.alpha:
break
return best_val
# ExpectiMax
# as the agent doesn't know what the player will do,
# he tries to calculate the weighted average between
# its possible choices, guessing what he could do,
# rather than searching the minimum value
class ExpectiMaxAI(MinimaxAI):
def __init__(self, rival, max_play_depth = 7):
super(ExpectiMaxAI, self).__init__(rival)
def minimax(self, depth, action, is_maximizing):
print('\n--- NODE DEPTH: {depth} ---'.format(depth = depth))
if self.game_over_lose() or self.rival.game_over_lose():
if self.game_over_lose():
return -self.win_val
else:
return self.win_val
elif depth == 0:
return self.evaluate(action)
if is_maximizing:
best_val = -float('inf')
for move in self.get_possible_choices():
val = self.minimax(depth - 1, move, False)
best_val = max(best_val, val)
return best_val
else:
avg_val = 0
n_moves = len(self.rival.get_possible_choices())
for move in self.rival.get_possible_choices():
val = self.minimax(depth - 1, move, True)
avg_val += val/n_moves
return avg_val