def random_board(simulate_till=4): def time_left(): return 100000 randomAgent1 = RandomPlayer() randomAgent2 = RandomPlayer() game = Board(randomAgent1, randomAgent2) move_history = [] for move_idx in range(simulate_till): if move_idx == 0: curr_move = (3, 3, False) elif move_idx == 1: # Non mirrorable moves curr_move = random.choice(((1, 2, False), (1, 4, False), (2, 5, False), (4, 5, False), \ (5, 4, False), (5, 2, False), (4, 1, False), (2, 1, False))) else: curr_move = game.__active_player__.move(game, time_left) curr_move = (curr_move[0], curr_move[1], bool(curr_move[2])) if curr_move not in game.get_active_moves(): raise Exception("Illegal move played") # Append new move to game history if game.__active_player__ == game.__player_1__: move_history.append([curr_move]) else: move_history[-1].append(curr_move) is_over, winner = game.__apply_move__(curr_move) if is_over: raise ("Game over while simulating board") return game, move_history
def new_game(mode): AIPlayer = RandomPlayer if mode == 1: player1 = GuiPlayer() player2 = AIPlayer() elif mode == 2: player1 = AIPlayer() player2 = GuiPlayer() elif mode == 3: player1 = GuiPlayer() player2 = GuiPlayer() elif mode == 4: player1 = AIPlayer() player2 = AIPlayer() board = Board(player1, player2) q_gui.put({'name': 'set board', 'board': board}) curr_time_millis = lambda: int(round(time() * 1000)) time_limit = 500 print('== BEGIN GAME ==') loop = True while loop: q_gui.put({'name': 'draw'}) legal_moves = board.get_legal_moves() if len(legal_moves) == 0: q_gui.put({ 'name': 'game over', 'winner': board.__inactive_player__ }) task = q_game.get() if task == 'die': print('== END GAME ==') loop = False elif task == 'move' and len(legal_moves) > 0: move_start = curr_time_millis() time_left = lambda: time_limit - (curr_time_millis() - move_start) player = board.get_active_player() next_move = player.move(board, legal_moves, time_left) if next_move != (-1, -1): player_symbol = board.__player_symbols__[player] print('player {0}: {1} | took {2} ms'.format( player_symbol, next_move, time_limit - time_left())) board.__apply_move__(next_move) q_game.task_done()
class InteractiveGame(): """This class is used to play the game interactively (only works in jupyter)""" def __init__(self, opponent=None, show_legal_moves=False): self.game = Board(Player, opponent) self.width = self.game.width self.height = self.game.height self.show_legal_moves = show_legal_moves self.gridb = create_board_gridbox(self.game, self.show_legal_moves, click_callback=self.select_move) self.visualized_state = None self.opponent = opponent self.output_section = widgets.Output( layout={'border': '1px solid black'}) self.game_is_over = False display(self.gridb) def select_move(self, b): if self.game_is_over: return print('The game is over!') ### swap move workaround ### # find if current location is in the legal moves # legal_moves is of length 1 if move exists, and len 0 if move is illegal legal_moves = [(x, y, s) for x, y, s in self.game.get_active_moves() if (x, y) == (b.x, b.y)] if not legal_moves: print(f"move {(b.x, b.y)} is illegal!") return else: # there is only one move in swap isolation game move = legal_moves[0] ### swap move workaround end ### self.game_is_over, winner = self.game.__apply_move__(move) if (not self.game_is_over) and (self.opponent is not None): opponents_legal_moves = self.game.get_active_moves() opponent_move = self.opponent.move(self.game, time_left=1000) assert opponent_move in opponents_legal_moves, \ f"Opponents move {opponent_move} is not in list of legal moves {opponents_legal_moves}" self.game_is_over, winner = self.game.__apply_move__(opponent_move) if self.game_is_over: print(f"Game is over, the winner is: {winner}") board_vis_state = get_viz_board_state(self.game, self.show_legal_moves) for r in range(self.height): for c in range(self.width): new_name, new_style = get_details(board_vis_state[r][c]) self.gridb[r, c].description = new_name self.gridb[r, c].style = new_style