def create_test_board(self, width, height, ghost_location, static_objects=None): root = Tk() board = Board() board.field = [[None for x in range(width)] for y in range(height)] if static_objects is not None: for object in static_objects: board.field[object.location.x][object.location.y] = object board.ghost = Ghost(ghost_location.x, ghost_location.y, board, 1) board.moving_gameObjects = [board.ghost] return board
def result_handler(): result = request.form session.pop("game_board", None) session.pop("tie", None) session.pop("winner_found", None) player1 = None player2 = None if "player1_type" not in result or "player2_type" not in result: abort(Response('I send it back!')) if result["player1_type"] == "human": player1 = Player(marker="X") elif result["player1_type"] == "CPU": player1 = AIPlayer(marker="X", max_depth=int(result["CPU_lookahead1"])) elif result["player1_type"] == "random": player1 = RandomPlayer(marker="X") if result["player2_type"] == "human": player2 = Player(marker="O") elif result["player2_type"] == "CPU": player2 = AIPlayer(marker="O", max_depth=int(result["CPU_lookahead1"])) elif result["player2_type"] == "random": player2 = RandomPlayer(marker="O") game_board = Board(player1, player2) session["game_board"] = game_board return redirect(url_for("game_handler"))
def minimax(self, board: Board, legal_actions: Actions, color: Color, level: int = 0, prev_best_points: float = None) -> Tuple[float, Location]: cur_best_score: None = None cur_best_location: None = None opponent_color: Color = Color.WHITE if color is Color.BLACK else Color.BLACK for location in legal_actions: new_board: Board = board.get_deepcopy() new_board.take_action(location, legal_actions[location], color) if level < self.depth: new_legal_actions: Actions = new_board.get_legal_actions( opponent_color) if not new_legal_actions: # opponent passes -> player plays again new_legal_actions: Actions = new_board.get_legal_actions( color) points, _ = self.minimax(new_board, new_legal_actions, color, level + 1, cur_best_score) else: # opponent plays next ply points, _ = self.minimax(new_board, new_legal_actions, opponent_color, level + 1, cur_best_score) else: points: float = self.immediate_reward.reward(new_board, color) # when points is not assigned -> due to nobody can play anymore if points is None: ended, won = self._finished(new_board) if ended: if won == color.value: points: float = 1000.0 elif won == 1 - color.value: points: float = -1000.0 else: points: float = 0.0 if color.value == color.value: # max_step if cur_best_score is None or cur_best_score < points: cur_best_score: float = points cur_best_location: Location = location if prev_best_points is not None and cur_best_score > prev_best_points: break elif opponent_color == color.value: # min step if cur_best_score is None or cur_best_score > points: cur_best_score: float = points cur_best_location: Location = location if prev_best_points is not None and cur_best_score < prev_best_points: break return cur_best_score, cur_best_location
def __init__(self, board_size: int, black: Agent, config: Config, episode: int, random_start: bool = False) -> None: self.board_size = board_size self.config: Config = config self.episode: int = episode self.board: Board = Board(self.board_size, random_start=random_start) self.ply = self.board.num_black_disks + self.board.num_white_disks - 4 self.black = black self.agent: Agent = black self.prev_pass: bool = False self.done: bool = False
def _finished(board: Board) -> Tuple[bool, Color]: black_legal_actions: Actions = board.get_legal_actions(Color.BLACK) white_legal_actions: Actions = board.get_legal_actions(Color.WHITE) ended: bool = False won: Union[Color, None] = None if not white_legal_actions and not black_legal_actions: ended: bool = True # someone won if board.num_black_disks > board.num_white_disks: won: Color = Color.BLACK elif board.num_white_disks > board.num_black_disks: won: Color = Color.WHITE else: won: Color = Color.EMPTY return ended, won
def __init__(self, episode: int, black: Agent, white: Agent, board_size: int, verbose: bool): # check arguments assert black.color is Color.BLACK, f'Invalid black agent' assert white.color is Color.WHITE, f'Invalid white agent' self.episode: int = episode self.black: Agent = black self.white: Agent = white self.board: Board = Board(board_size) # create a new board self.verbose: bool = verbose # state of the game self.ply = 0 # no plies so far self.agent: Agent = black # black begins self.prev_pass: bool = False # opponent did not pass in previous ply self.done: bool = False # not done yet