def get_action(self, gamestate, who): valid = False my_team = gamestate.get_team(who) print "My moves:", [m for m in my_team.primary().moveset.moves] print "My switches:", [(m, i) for i, m in enumerate(my_team.poke_list) if m != my_team.primary() and m.alive] my_legal = gamestate.get_legal_actions(who, log=True) while not valid: action_string = raw_input('> ') my_action = Action.create(action_string) if my_action in my_legal: valid = True else: print "Illegal move", my_action, my_legal return my_action
def get_action(self, gamestate, who): valid = False my_team = gamestate.get_team(who) print("My moves:" + [m for m in my_team.primary().moveset.moves]) print("My switches:" + [(m, i) for i, m in enumerate(my_team.poke_list) if m != my_team.primary() and m.alive]) my_legal = gamestate.get_legal_actions(who, log=True) while not valid: action_string = raw_input('> ') try: my_action = Action.create(action_string) if my_action not in my_legal: print("Illegal move", my_action, my_legal) assert False valid = True except: pass return my_action
def get_legal_actions(self, who, log=False): my_team = self.get_team(who) my_poke = my_team.primary() opp_team= self.get_team(1 - who) opp_poke = opp_team.primary() pokemon = range(len(my_team.poke_list)) valid_switches = [i for i in pokemon if my_team.poke_list[i].alive and i != my_team.primary_poke] valid_backup_switches = valid_switches + [my_team.primary_poke] if len(valid_switches) == 0: valid_switches = [None] moves = [] switches = [] for move_index in range(len(my_poke.moveset.moves)): move_name = my_poke.moveset.moves[move_index] mega = my_poke.can_evolve() if my_poke.choiced: if move_name != my_poke.move_choice: continue if move_name == "U-turn" or move_name == "Volt Switch": for j in valid_switches: for k in valid_backup_switches: if j == None: moves.append( Action( "move", move_index=move_index, mega=mega, volt_turn=j, backup_switch=None ) ) elif j != None and k != None and j != k: moves.append( Action( "move", move_index=move_index, volt_turn=j, backup_switch=k, mega=mega ) ) else: moves.extend([ Action("move", move_index=move_index, mega=mega, backup_switch=j) for j in valid_switches ]) switches.extend([Action("switch", switch_index=i, backup_switch=j) for i in valid_switches for j in valid_backup_switches if j != i and i is not None]) if opp_poke.ability == "Magnet Pull" and "Steel" in my_poke.typing and "Ghost" not in my_poke.typing: switches = [] elif opp_poke.ability == "Shadow Tag" and "Ghost" not in my_poke.typing: switches = [] elif opp_poke.ability == "Arena Trap" and "Ghost" not in my_poke.typing and "Flying" not in my_poke.typing: switches = [] if my_poke.taunt: moves = [move for move in moves if MOVES[my_poke.moveset.moves[move.move_index]].category != "Non-Damaging"] if my_poke.disabled is not None: moves = [move for move in moves if my_poke.moveset.moves[move.move_index] != my_poke.disabled] if my_poke.encore: moves = [move for move in moves if my_poke.moveset.moves[move.move_index] == my_poke.last_move] return moves + switches
def get_legal_actions_probs(self, classifier, turn_num, player_num, log=False, probs=True): my_team = self.get_team(player_num) my_poke = my_team.primary() opp_team = self.get_team(1 - player_num) opp_poke = opp_team.primary() mega = my_poke.can_evolve() pokemon = range(len(my_team.poke_list)) valid_switches = [ i for i in pokemon if my_team.poke_list[i].alive and i != my_team.primary_poke ] valid_backup_switches = valid_switches + [my_team.primary_poke] if len(valid_switches) == 0: valid_switches = [None] move_names = [] move_probs = [] if probs: classifier_probs = classifier.predict( self.to_encoded_list(classifier.feature_label_encoders, classifier.cat_indices, turn_num, player_num))[0, :] else: classifier_probs = numpy.ones(681) if my_poke.choiced and my_poke.move_choice != None: move_names = [my_poke.move_choice] move_probs = numpy.ones(1) elif len(my_poke.moveset.known_moves) < 4: move_names = classifier.move_names move_probs = classifier_probs[classifier.move_indices] else: move_names = my_poke.moveset.known_moves my_move_indices = numpy.array([ classifier.move_dict[move] if move in classifier.move_dict else 0 for move in move_names ]) move_probs = classifier_probs[my_move_indices] move_probs = numpy.repeat(move_probs, len(valid_switches)) moves = [] for i in range(len(move_names)): move_name = move_names[i] if move_name in my_poke.moveset.known_moves: move_index = my_poke.moveset.known_moves.index(move_name) else: move_index = -1 if move_name == "U-turn" or move_name == "Volt Switch": if valid_switches[0] == None: moves.append( Action("move", move_index=move_index, mega=mega, volt_turn=None, backup_switch=None, move_name=move_name)) else: switches = valid_backup_switches[:] for a in xrange(1, len(switches)): b = random.choice(xrange(0, a)) switches[a], switches[b] = switches[b], switches[a] moves.extend([ Action("move", move_index=move_index, mega=mega, volt_turn=valid_switches[j], backup_switch=switches[j], move_name=move_name) for j in range(len(valid_switches)) ]) else: moves.extend([ Action("move", move_index=move_index, mega=mega, backup_switch=j, move_name=move_name) for j in valid_switches ]) switches = [ Action("switch", switch_index=i, backup_switch=j) for i in valid_switches for j in valid_backup_switches if j != i and i is not None ] if valid_switches == [None]: switch_probs = [] elif probs: switch_indices = numpy.array([ classifier.pokemon_dict[my_team.poke_list[i].name] if my_team.poke_list[i].name in classifier.pokemon_dict else 0 for i in valid_switches ]) switch_probs = numpy.repeat(classifier_probs[switch_indices], len(valid_backup_switches) - 1) else: switch_probs = numpy.ones(len(switches)) if opp_poke.ability == "Magnet Pull" and "Steel" in my_poke.typing and "Ghost" not in my_poke.typing: switches = [] switch_probs = [] elif opp_poke.ability == "Shadow Tag" and "Ghost" not in my_poke.typing: switches = [] switch_probs = [] elif opp_poke.ability == "Arena Trap" and "Ghost" not in my_poke.typing and "Flying" not in my_poke.typing: switches = [] if my_poke.taunt: moves = [ move for move in moves if MOVES[move.move_name].category != "Non-Damaging" ] indices = numpy.array([ i for i in range(len(moves)) if MOVES[move.move_name].category != "Non-Damaging" ]) move_probs = move_probs[indices] if my_poke.disabled is not None: moves = [ move for move in moves if move.move_name != my_poke.disabled ] indices = [ i for i in range(len(moves)) if move.move_name != my_poke.disabled ] move_probs = move_probs[indices] if my_poke.encore: moves = [ move for move in moves if move.move_name == my_poke.last_move ] indices = [ i for i in range(len(moves)) if move.move_name == my_poke.last_move ] total = moves + switches total_probs = numpy.hstack((move_probs, switch_probs)) total_probs /= numpy.sum(total_probs) return (total, total_probs)