async def on_message(message): pre = get_prefix print (pre) if message.author == client.user: return # handle help and all of the playbook interactions response = plain_command_handler(message) if response: await message.channel.send(response) return response = embed_command_handler(message) if response: await message.channel.send(embed=response) return #list moves move_list = get_moves(message) if move_list: await message.channel.send(move_list) return #remember generic ! should always be last in the tree elif message.content.startswith("!"): log_line = message.guild.name + "|" + message.channel.name + "|" + message.author.name + "|" + message.content logger.info(log_line) response = mad_parse(message) if response: await message.channel.send(embed=response) await client.process_commands(message)
def play(): ''' play the game ''' orientation = {'N': 0, 'S': 180, 'E': 90, 'W': 270} glide = 12 mm = 0 for move in moves.get_moves(): # who's moving actor, direction = move.split(SEP) f = F[actor] if f.static : continue if not f.alive: continue has_weapon = False if f.weapon is None else True print('-'*40) print(mm:=mm+1, f, '->', direction) # set angle angle = orientation[direction] f.setheading(angle) if has_weapon: f.weapon.setheading(angle) # move f_pos_old = tuptoint(f.position()) step = span/glide for _ in range(glide): f.forward(step) if has_weapon: f.weapon.forward(step) f_pos_new = tuptoint(f.position()) # update board tracking occupied = f_pos_new in F_Pos update_position('old', f_pos_old, f, F_Pos) update_position('new', f_pos_new, f, F_Pos) # fight if occupied if occupied: print('someone here already -> ', F_Pos[f_pos_new][0]) normal_speed = f.speed() ring, at, df = warmup(f_pos_new) winner, loser = bell(angle, at, df, normal_speed) final_rites(ring, loser) if loser == f: continue # update weapons tracking dict if has_weapon: update_position('old', f_pos_old, f.weapon, W_Pos) update_position('old', f_pos_new, f.weapon, W_Pos) else: # pick any weapons if f_pos_new in W_Pos: f.pick_weapon(W_Pos[f_pos_new]) f.weapon.forward(0) # so weapon shows atop
async def on_message(message): if message.author == client.user: return # handle help and all of the playbook interactions if message.content.startswith("!"): response = plain_command_handler(message) if response: log_line = msg_log_line(message) logger.info(log_line) await message.channel.send(response) return response = embed_command_handler(message) if response: log_line = msg_log_line(message) logger.info(log_line) await message.channel.send(embed=response) return #answer a call for help if message.content.startswith("!help"): log_line = msg_log_line(message) logger.info(log_line) help_file = open("help", "r") response = help_file.read() await message.author.send(response) await message.channel.send("I have sent help to your PMs.") #list moves# if message.content.startswith("!"): move_list = get_moves(message) if move_list: await message.channel.send(move_list) #remember generic ! should always be last in the tree# else: log_line = msg_log_line(message) logger.info(log_line) response = mad_parse(message) if response: logger.info(response) (response, addendum) = response # if addendum is not None: ##testing if going first made a difference # await message.channel.send(content = addendum) await message.channel.send(embed=response) if addendum is not None: await message.channel.send(content=addendum) else: logger.info('no match found for ' + message.content)
async def on_message(message): if message.author == client.user: return lang = get_raw_lang(message) # handle help and all of the playbook interactions response = plain_command_handler(message, lang) if response: log_line = message.guild.name + "|" + message.channel.name + "|" + message.author.name + "|" + message.content logger.info(log_line) await message.channel.send(response) return response = embed_command_handler(message, lang) if response: log_line = message.guild.name + "|" + message.channel.name + "|" + message.author.name + "|" + message.content logger.info(log_line) await message.channel.send(embed=response) return #answer a call for help if message.content.startswith("!help"): log_line = message.guild.name + "|" + message.channel.name + "|" + message.author.name + "|" + message.content logger.info(log_line) help_file = open("help", "r") response = help_file.read() await message.author.send(response) await message.channel.send("I have sent help to your PMs.") #list moves# move_list = get_moves(message, lang) if move_list: await message.channel.send(move_list) #remember generic ! should always be last in the tree# elif message.content.startswith("!"): log_line = message.guild.name + "|" + message.channel.name + "|" + message.author.name + "|" + message.content logger.info(log_line) response = mad_parse(message) if response: logger.info(response) await message.channel.send(embed=response) else: logger.info('no match found for ' + message.content)
def human_turn(playing_board, my_pieces, computer_pieces): print_board(playing_board) # Get a list of all the pieces that can be moved edible = can_eat(playing_board, my_pieces, contents.WHITE, contents.BLACK) # Print the list of pieces print_pieces(edible[0]) # Get which piece the user wants to move index = get_piece(edible[0]) # Get where the user would like to move that piece print 'Please select where to move this piece to' moves = get_moves(playing_board, index, contents.WHITE, contents.BLACK) print_dct(moves) move = get_move(moves) # Make the move make_move(playing_board, index, move, edible[1])
def __init__(self): Character.__init__(self, "The Maker", 5, {"element": "???", "quirk": "Flogna", "form": "Ghost", "gender": 0, "orientation": 0}, {"HP": sys.maxint, "MP": sys.maxint, "Attack": sys.maxint, "Defense": sys.maxint, "Special": sys.maxint, "Resistance": sys.maxint, "Speed": sys.maxint, "Luck": sys.maxint, "Accuracy": sys.maxint, "Evasion": sys.maxint}, dict([(x, 0) for x in get_moves().keys()]))
def __init__(self): unsigned_moves = dict([(x, 0) for x in get_moves().keys()]) for signature in get_signatures().keys(): del unsigned_moves[signature] Character.__init__(self, "Mew", 5, {"element": "Psychic", "color": "magenta", "quirk": "Synchronize", "form": "Ghost", "gender": 0, "bio": "Because it can use all kinds of moves, many scientists believe Mew to be the ancestor of all Pokemon. Apparently, it appears only to those who are pure of heart and have a strong desire to see it."}, {"HP": 100, "MP": 100, "Attack": 100, "Defense": 100, "Special": 100, "Resistance": 100, "Speed": 100, "Luck": 100}, unsigned_moves)
def a_star_sandbar(screen=[], old_loc=[], loop=False, frame_num=0, *args): # Performs A* on the frog in order to get the player to the half-way point # (the sandbar). Takes the screen and previous player's location, and # returns the move or set of moves to make as well as the player's new position. loc = old_loc guide = set_guide(screen, loc) loc = guide.pop() dist = sandbar_distance(loc) node = [] if frame_num: # Dynamic nodes are made up of location, move that got there, # the cost of the node (traveled + heuristic), the frame it # came from, and the parent of the current node (implicit list). node = [loc, None, dist, frame_num, None] else: # Nodes are made up of location, move that got there, # the cost of the node (traveled + heuristic) and the # parent node of the current one(they're implicitly linked). node = [loc, None, dist, None] frontier = priority_queue() frontier.push(node, dist) explored = [] move = 0 move_list = [] if frame_num: while True: if frontier.is_empty(): return None parent_node = frontier.pop() loc, move, this_cost, frame, grand_parent_node = parent_node if loc in explored: continue if not sandbar_distance(loc): while parent_node: _, action, _, _, parent_node = parent_node if not action == None: if not loop: move = action else: move_list.append(action) if loop: move_list.append(loc) return move_list else: move = [move, loc] return move explored.append(loc) for move in get_moves(guide, loc, frame): action, new_loc = move h_cost = sandbar_distance(new_loc) new_cost = 1 + h_cost next_frame = 5 + frame child_node = [ new_loc, action, new_cost, next_frame, parent_node ] if not ((new_loc in explored) or (new_loc in frontier)): frontier.push(child_node, new_cost) elif (new_loc in frontier): frontier.push(child_node, new_cost) else: while True: if frontier.is_empty(): return None parent_node = frontier.pop() loc, move, this_cost, grand_parent_node = parent_node if loc in explored: continue if not sandbar_distance(loc): while parent_node: _, action, _, parent_node = parent_node if not action == None: if not loop: move = action else: move_list.append(action) if loop: move_list.append(loc) return move_list else: move = [move, loc] return move explored.append(loc) for move in get_moves(guide, loc): action, new_loc = move h_cost = sandbar_distance(new_loc) new_cost = 1 + h_cost child_node = [new_loc, action, new_cost, parent_node] if not ((new_loc in explored) or (new_loc in frontier)): frontier.push(child_node, new_cost) elif (new_loc in frontier): frontier.push(child_node, new_cost)
def a_sure(screen=[], old_loc=[], loop=False, frame_num=0, *args): # Takes the screen and the previous player's position as input # and returns either the next move to make or the set of moves # to make as well as the new position of the player. loc = old_loc guide = set_guide(screen, loc) loc = guide.pop() closest = [] # a list, with first element being h_cost, then the node dist = goal_distance(loc, guide) # Nodes are made up of location, move that got there, # the cost of the node (traveled + heuristic), and the # parent node of the current one (they're implicitly linked). node = [loc, None, dist, None] closest = [dist, node] frontier = priority_queue() frontier.push(node, dist) explored = [] move = 0 move_list = [] while True: if frontier.is_empty(): parent_node = closest[1] while parent_node: loc, action, _, parent_node = parent_node if not action == None: if not loop: move = action else: move_list.append(action) #print("a_sure: incomplete move: " + str(move) + " from distance " + str(goal_distance(loc, guide))) if loop: move_list.append(loc) return move_list else: move = [move, loc] return move parent_node = frontier.pop() loc, move, this_cost, grand_parent_node = parent_node if loc in explored: continue if not goal_distance(loc, guide): while parent_node: loc, action, _, parent_node = parent_node if not action == None: if not loop: move = action else: move_list.append(action) #print("a_sure: complete move: " + str(move) + " from location: " + str(loc)) if loop: move_list.append(loc) return move_list else: move = [move, loc] return move explored.append(loc) for new_move in get_moves(guide, loc, frame_num): new_action, new_loc = new_move # print("a_sure: new_action = " + str(new_action) + " new_loc = " + str(new_loc)) new_h_cost = goal_distance(new_loc, guide) new_cost = 1 + new_h_cost this_child_node = [new_loc, new_action, new_cost, parent_node] if not ((new_loc in explored) or (new_loc in frontier)) or (new_action == 0): #print("a_sure: checked location: " + str(new_loc)) frontier.push(this_child_node, new_cost) elif (new_loc in frontier): #print("a_sure: re-entered location: " + str(new_loc)) frontier.push(this_child_node, new_cost) if (new_h_cost < closest[0]): closest = [new_h_cost, this_child_node] elif (new_h_cost == closest[0]): if (new_cost < closest[1][2]): closest = [new_h_cost, this_child_node]
def alpha_beta_max(playing_board, comp_pieces, human_pieces, iteration, alpha, beta): # Get globals global NODES_MADE global MAX_PRUNES # Find if this node is a terminal node if is_terminal(playing_board, contents.BLACK): return [comp_pieces, 1000, iteration] if is_terminal(playing_board, contents.WHITE): return [human_pieces, -1000, iteration] # If this iteration is as far down as the tree should go if iteration is 0: # Get a utility of this node utility = get_utility(comp_pieces, human_pieces, contents.BLACK) return [comp_pieces, utility, iteration] # Start the search! smallest_value = -1000 potential_moves = list(comp_pieces) # Find all the pieces that can be moved edible = can_eat(playing_board, comp_pieces, contents.BLACK, contents.WHITE) # For each piece that can be moved for i, piece in enumerate(edible[0]): # Get the moves for that piece all_moves_for_piece = get_moves(playing_board, piece, contents.BLACK, contents.WHITE) # If a piece has to be eaten if edible[1] is True: current_max = -1001 potential_eating_moves = None # For all the moves that can be taken for move in all_moves_for_piece: # Accounts for the return of get_moves if type(all_moves_for_piece[move]) is bool: continue # A new node was created NODES_MADE += 1 # Find the new move new_moves = list(comp_pieces) piece_index = comp_pieces.index(piece) new_moves[piece_index] = all_moves_for_piece[move] # Get the utility of the node eating_utility = get_utility(new_moves, human_pieces, contents.BLACK) # If this move is the best move so far if eating_utility > current_max: current_max = eating_utility potential_eating_moves = list(new_moves) # Return best move for eating return [potential_eating_moves, current_max, iteration] # If no piece can be eaten for move in all_moves_for_piece: # Accounts for the return of get_moves if type(all_moves_for_piece[move]) is bool: continue # A new node was created NODES_MADE += 1 # Find the new move new_moves = list(comp_pieces) new_moves[i] = all_moves_for_piece[move] # Call the min player minimum = alpha_beta_min(playing_board, new_moves, human_pieces, iteration - 1, alpha, beta) # Get the min utility min_utility = minimum[1] # If the min utility is a min value if min_utility > smallest_value: smallest_value = min_utility potential_moves = list(new_moves) # If the tree can be pruned if smallest_value >= beta: # Prune! MAX_PRUNES += 1 return [list(new_moves), smallest_value, iteration] # Fix alpha value if alpha < smallest_value: alpha = smallest_value # If potential moves did not work out if potential_moves is comp_pieces: return [new_moves, smallest_value, iteration] # If potential moves did work out else: return [potential_moves, smallest_value, iteration]
def alpha_beta_min(playing_board, comp_pieces, human_pieces, iteration, alpha, beta): # Get globals global NODES_MADE global MIN_PRUNES # Find if this node is a terminal node if is_terminal(playing_board, contents.WHITE): return [human_pieces, -1000, iteration] if is_terminal(playing_board, contents.BLACK): return [comp_pieces, 1000, iteration] # If this iteration is as far down as the tree should go if iteration is 0: # Get a utility of this node utility = get_utility(human_pieces, comp_pieces, contents.WHITE) return [human_pieces, utility, iteration] # Start the search! largest_value = 1000 potential_moves = list(human_pieces) # Find all the pieces that can be moved edible = can_eat(playing_board, human_pieces, contents.WHITE, contents.BLACK) # For each piece that can be moved for i, piece in enumerate(edible[0]): # Get the moves for that piece all_moves_for_piece = get_moves(playing_board, piece, contents.WHITE, contents.BLACK) # If a piece has to be eaten if edible[1] is True: current_min = 1001 potential_eating_moves = None # For all the moves that can be taken for move in all_moves_for_piece: # Accounts for the return of get_moves if type(all_moves_for_piece[move]) is bool: continue # A new node was created NODES_MADE += 1 # Check for a bad output if piece in comp_pieces: piece_index = comp_pieces.index(piece) else: continue # Find the new move new_moves = list(comp_pieces) new_moves[piece_index] = all_moves_for_piece[move] # Get the utility of the node eating_utility = get_utility(new_moves, human_pieces, contents.WHITE) # If this move is the best move so far if eating_utility < current_min: current_min = eating_utility potential_eating_moves = list(new_moves) # Return best move for eating return [potential_eating_moves, current_min, iteration] # If no piece can be eaten for move in all_moves_for_piece: # Accounts for the return of get_moves if type(all_moves_for_piece[move]) is bool: continue # A new node was created NODES_MADE += 1 # Find the new move new_moves = list(human_pieces) new_moves[i] = all_moves_for_piece[move] # Call the max player maximum = alpha_beta_max(playing_board, comp_pieces, new_moves, iteration - 1, alpha, beta) # Get the max utility max_utility = maximum[1] # If the max utility is a max value if max_utility < largest_value: largest_value = max_utility potential_moves = list(new_moves) # If the tree can be pruned if largest_value <= alpha: # Prune! MIN_PRUNES += 1 return [list(new_moves), largest_value, iteration] # Fix beta value if beta > largest_value: beta = largest_value # If potential moves did not work out if potential_moves is comp_pieces: return [new_moves, largest_value, iteration] # If potential moves did work out else: return [potential_moves, largest_value, iteration]
import os from sklearn.model_selection import train_test_split from model import get_piece_moved, legal_start from sklearn.linear_model import PassiveAggressiveClassifier from sklearn.metrics import accuracy_score, confusion_matrix import random if __name__ == "__main__": start = datetime(2018, 12, 8) end = datetime(2021, 7, 7) games = 500 print("gathering games") white, black = get_moves('whoisis', start, end, games, split=True) train, test = train_test_split(black) train = black[:-len(black) // 9] test = black[len(black) // 9:] x_train = np.array([data[0] for data in train]) x_test = np.array([data[0] for data in test]) y_train = get_piece_moved([data[0] for data in train], legal_start([data[2] for data in train], False)) y_test = get_piece_moved([data[0] for data in test], legal_start([data[2] for data in test], False)) ans = [] for board in x_test: ans.append(random.randint(0, 5))