def __init__(self): self.az_table = Table() self.az_player = Player("MyPlayer", self.az_table) self.ai_player = AiPlayer("AiPlayer", self.az_table) self.players.append(self.az_player) self.players.append(self.ai_player) self.ai_player.get_opponents(self.players)
def play_one_episode(): logger = getLogger(__name__) rows = 8 columns = 8 black_player = AiPlayer(AlphaBeta(depth=4), Evaluator()) white_player = AiPlayer(AlphaBeta(depth=4), Evaluator()) othello_engine = Engine(black_player, white_player, rows, columns) othello_engine.reset() transitions, winner = othello_engine.run_one_game() logger.info("Game finished! winner was: %s", winner) for board in transitions: logger.info("Game transitions:\n%s", board)
def main(): deck = Deck(True) sam = AiPlayer("Sam", 17) hannah = AiPlayer("Hannah", 16) matt = AiPlayer("Matthew", 15) players = deque([matt, hannah, sam]) # while deck: for _ in range(100000): game(players, deck, False) players.rotate(1) print("====================") print_overall_results(players) print("====================")
class Game: players = [] last_round = False winner = " " maximum_scores = 0 def __init__(self): self.az_table = Table() self.az_player = Player("MyPlayer", self.az_table) self.ai_player = AiPlayer("AiPlayer", self.az_table) self.players.append(self.az_player) self.players.append(self.ai_player) self.ai_player.get_opponents(self.players) def play_game(self): self.az_table.fill_boxes() while not self.last_round: self.az_table.print_table() if self.az_table.has_tile(): for player in self.players: if self.az_table.has_tile(): print("Player = %s" % player.name) player.play() self.az_table.print_table() else: break for player in self.players: if player.is_last_round(): self.last_round = player.is_last_round() for player in self.players: player.move_tiles_to_board() if not self.last_round and not self.az_table.has_tile(): self.az_table.fill_boxes() for player in self.players: player.calculate_scores_after_round() if player.scores > self.maximum_scores: self.maximum_scores = player.scores self.winner = player.name print("The winner is %s" % self.winner)
def play_game_vs_ai(): game = TicTacToeGame({ 'players': [ConsolePlayer(), AiPlayer()], 'on_game_start': on_game_start, 'on_move_made': on_move_made, 'on_board_change': on_board_change, 'on_game_end': on_game_end, 'on_move_exception': on_move_exception }) game.play()
def _add_random_ais(self) -> None: """adds random AIs to the population in order to reach the original population size""" missing_ais = self.population_size - len(self.population) ais = [] for ai_number in range(len(self.ai_histories), len(self.ai_histories) + missing_ais): ai = AiPlayer(str(ai_number), Trainer._build_random_strategy(), Trainer._build_random_strategy(), Trainer._build_random_strategy()) # ai = AiPlayer(str(ai_number), SampleStrategies.bodo_quadratic_factor, # SampleStrategies.bodo_linear_factor, SampleStrategies.bodo_bias) self.ai_histories[ai] = dict() self._add_event_to_ai_history(ai, "INITialization") ais.append(ai) self.population.extend(ais) assert (len(self.population) <= self.population_size)
def _mix_strategies(self, parent1, parent2) -> AiPlayer: """mixes the strategies of the parents by creating averages of the different factors / bias to be their child's strategy""" child_quadratic_factor = np.array([ x / 2 for x in (parent1.quadratic_factor + parent2.quadratic_factor) ]) child_linear_factor = np.array( [x / 2 for x in (parent1.linear_factor + parent2.linear_factor)]) child_bias = np.array([x / 2 for x in (parent1.bias + parent2.bias)]) assert (len(child_linear_factor) == len(parent1.linear_factor) and len(child_linear_factor) == len(parent2.linear_factor)) self._add_event_to_ai_history(parent1, "PAREnt") self._add_event_to_ai_history(parent2, "PAREnt") ai_number = len(self.ai_histories) return AiPlayer(str(ai_number), child_quadratic_factor, child_linear_factor, child_bias)
def build(self): rows = 8 columns = 8 self._board = self.setup_board(rows, columns) self._engine = Engine(HumanPlayer(self._board), AiPlayer(AlphaBeta(), Evaluator()), rows, columns) self._engine.set_board_state_change_listener(self.on_state_change) self._root = BoxLayout(orientation='vertical') self._start_button = Button(text="start", size_hint=(0.5, 0.2)) self._start_button.background_color = (0.7, 0.7, 0.7, 1.0) self._start_button.bind(on_press=self.on_start_press_listener) self._stop_button = Button(text="stop", size_hint=(0.5, 0.2)) self._stop_button.background_color = (0.7, 0.7, 0.7, 1.0) self._stop_button.bind(on_press=self.on_stop_press_listener) self._root.add_widget(self._board) self._buttons = BoxLayout(orientation='horizontal') self._buttons.add_widget(self._start_button) self._buttons.add_widget(self._stop_button) self._root.add_widget(self._buttons) return self._root
class TestAiPlayer(TestCase): az_table = Table() ai_player = AiPlayer("TestAiPlayer", az_table) ai_player.az_table.round_boxes = [[ int(TileType.WHITE), int(TileType.WHITE), int(TileType.BLACK), int(TileType.YELLOW) ], [0, 0, 0, 0], [ int(TileType.RED), int(TileType.YELLOW), int(TileType.YELLOW), int(TileType.YELLOW) ], [ int(TileType.BLUE), int(TileType.YELLOW), int(TileType.BLUE), int(TileType.RED) ], [ int(TileType.BLACK), int(TileType.BLACK), int(TileType.BLUE), int(TileType.RED) ]] ai_player.az_table.round_boxes_by_number = [{ 4: [], 3: [], 2: [int(TileType.WHITE)], 1: [int(TileType.BLACK), int(TileType.YELLOW)] }, { 4: [], 3: [], 2: [], 1: [] }, { 4: [], 3: [int(TileType.YELLOW)], 2: [], 1: [int(TileType.RED)] }, { 4: [], 3: [], 2: [int(TileType.BLUE)], 1: [int(TileType.YELLOW), int(TileType.RED)] }, { 4: [], 3: [], 2: [int(TileType.BLACK)], 1: [int(TileType.RED), int(TileType.BLUE)] }] ai_player.az_table.round_boxes_by_color = [ { int(TileType.BLUE): 0, int(TileType.BLACK): 1, int(TileType.RED): 0, int(TileType.WHITE): 2, int(TileType.YELLOW): 1 }, { int(TileType.BLUE): 0, int(TileType.BLACK): 0, int(TileType.RED): 0, int(TileType.WHITE): 0, int(TileType.YELLOW): 0 }, { int(TileType.BLUE): 0, int(TileType.BLACK): 0, int(TileType.RED): 1, int(TileType.WHITE): 0, int(TileType.YELLOW): 3 }, { int(TileType.BLUE): 2, int(TileType.BLACK): 0, int(TileType.RED): 1, int(TileType.WHITE): 0, int(TileType.YELLOW): 1 }, { int(TileType.BLUE): 1, int(TileType.BLACK): 2, int(TileType.RED): 1, int(TileType.WHITE): 0, int(TileType.YELLOW): 0 }, ] def test_find_next_move(self): result = self.ai_player.find_next_move() result.print_tiles() self.assertEqual(result.source_box, 2) self.assertEqual(result.destination_row, 1) self.assertEqual(result.type, int(TileType.YELLOW)) self.assertEqual(result.quantity, 3) def test_get_most_tiles(self): result = self.ai_player.get_most_tiles_from_box() result.print_tiles() self.assertEqual(result.source_box, 2) self.assertEqual(result.type, int(TileType.YELLOW)) self.assertEqual(result.quantity, 3)
if not game_in_progress: break # todo execute turns for all players and evaluate turns (separate) def load_best_ai(): """loads the AI that was saved""" file = open("best_ai.dat", "rb") best_ai = pickle.load(file) file.close() return best_ai if __name__ == "__main__": ui = PyGameUi() ui.show_board() # ai_opponent = load_best_ai() # game = Game([HumanPlayer("meep", 1, ui), # AiPlayer("meeep", 1, np.random.randn(18), np.random.randn(18), np.random.randn(18))]) game = Game([ AiPlayer("", SampleStrategies.bodo_quadratic_factor, SampleStrategies.bodo_linear_factor, SampleStrategies.bodo_bias, ui), AiPlayer("", SampleStrategies.bodo_quadratic_factor, SampleStrategies.bodo_linear_factor, SampleStrategies.bodo_bias) ]) game.play(True)
# inform all players about new game situation AFTER they made their turns for player_index in range(self.player_count): self.lst_player[player_index].inform(self.lst_boards, player_index) if not game_in_progress: break # todo execute turns for all players and evaluate turns (separate) def load_best_ai(): """loads the AI that was saved""" file = open("best_ai.dat", "rb") best_ai = pickle.load(file) file.close() return best_ai if __name__ == "__main__": ui = PyGameUi() ui.show_board() # ai_opponent = load_best_ai() # game = Game([HumanPlayer("meep", ui), # AiPlayer("meeep", np.random.randn(18), np.random.randn(18), np.random.randn(18))]) game = Game([HumanPlayer("visitor", ui), AiPlayer("", SampleStrategies.alpha_qwixx_quadratic_factor, SampleStrategies.alpha_qwixx_linear_factor, SampleStrategies.alpha_qwixx_bias)]) game.play(True)