def get_move_list_for_biggest_play_from(game_state: BotGameState, strategy: Strategy, origin): """ Get a list of moves corresponding to the largest play from the specified origin point. :param game_state: the BotGameState for the current turn. :param strategy: The Strategy to use to determine the valid move to return. :param origin: The number to start on, or an iterable containing start numbers. All paths will start with one of those number. :return:A list of moves corresponding to the largest play :rtype: List(BotMove) """ try: play = strategy.choose_play(game_state, game_state.get_biggest_plays_from(origin)) moves = [] used_trains = [] # Loop through the sorted paths, so that any path which demands satisfaction is last for path in sorted(play.paths, key=attrgetter('demands_satisfaction')): if path.size > 0: train = strategy.choose_train_for_path(game_state, path, used_trains) used_trains.append(train) moves.extend([BotMove(make_domino_from_edge(edge), train) for edge in path.edge_list]) except AttributeError as e: print(e) moves = BaseBot.get_move_list_for_longest_paths_from(game_state, strategy, origin) return moves
def get_valid_move(game_state: BotGameState, strategy: Strategy) -> BotMove: """ Get a valid move for the bot using the specified game state and strategy. :param game_state: the BotGameState for the current turn. :param strategy: The Strategy to use to determine the valid move to return. :return: A valid move :rtype: BotMove """ return strategy.choose_move(game_state, game_state.get_all_valid_moves())
def get_move(self, game_state: BotGameState): if self.turn == 0: if len(self.first_turn_moves) == 0: self.first_turn_moves = self.get_move_list_for_biggest_play_from(game_state, self.strategy, game_state.get_playable_numbers()) if len(self.first_turn_moves) > 0: move = self.first_turn_moves.pop(0) return move move = self.get_valid_move(game_state, self.strategy) return move
def get_move_list_for_longest_paths_from(game_state: BotGameState, strategy: Strategy, origin): """ Get a list of moves corresponding to the longest path from the specified origin point. :param game_state: the BotGameState for the current turn. :param strategy: The Strategy to use to determine the valid move to return. :param origin: The number to start on, or an iterable containing start numbers. All paths will start with one of those number. :return: A list of moves corresponding to the longest path :rtype: List(BotMove) """ path, train = strategy.choose_path_and_train(game_state, game_state.get_longest_paths_from(origin), game_state.playable_trains) return [BotMove(make_domino_from_edge(edge), train) for edge in path.edge_list]
def take_turn(self): """ Take a turn for the current active player, as determined by the GameState. """ player = self.game_state.current_player player.can_play = True bot_game_state = BotGameState(self.game_state, player) drew = False played = False done = False first = player.turn == 0 player.bot.start_turn(player.turn) while not done: valid_moves = bot_game_state.get_all_valid_moves() if len(valid_moves) == 0: if not played and not drew: domino = self.game_state.draw_domino(player) if not domino: player.can_play = False done = True else: bot_game_state.draw_domino(domino) drew = True else: done = True else: move = player.bot.get_move(bot_game_state) if not self.validate_move(move, player): player.bot.report_invalid_move(move) shuffle(valid_moves) move = valid_moves.pop() if move.domino.is_double: drew = False played = False else: played = True self.do_move(player, move) bot_game_state.do_move(move) if played and not first: done = True player.turn += 1
def get_move(self, game_state: BotGameState): return self.strategy.choose_move(game_state, game_state.get_all_valid_moves())