Exemplo n.º 1
0
def calculate_probabilities_for_player(
        board: Board,
        player_state: PlayerState,
        depth: int,
        step_offset: int = 1,
        probability_cutoff: float = 0,
        global_probability_factor: float = 1.
) -> Tuple[np.ndarray, np.ndarray]:
    """
    Returns tuple of numpy arrays:
        - probability of reaching the given cells in the [depth] next steps
        - minimum amount of steps needed for the given player to reach the cell
    """

    probabilities = np.zeros((board.height, board.width))
    min_player_steps = np.ones(
        (board.height, board.width)) * __INIT_VALUE_PLAYER_STEPS

    valid_player_state_tuples = []

    for action in PlayerAction:

        new_player_state = player_state.copy()
        new_player_state.do_action(action)
        next_player_state = new_player_state.do_move()

        if next_player_state.verify_move(board):
            valid_player_state_tuples.append(
                (new_player_state, next_player_state))

    possible_action_count = len(valid_player_state_tuples)
    if possible_action_count > 0:
        local_probability_factor = (
            1 / possible_action_count) * global_probability_factor

        for new_player_state, next_player_state in valid_player_state_tuples:

            affected_cells = next_player_state.steps_to_this_point
            for cell_x, cell_y in affected_cells:
                if board.point_is_on_board(cell_x, cell_y):
                    probabilities[cell_y, cell_x] += local_probability_factor
                    min_player_steps[cell_y, cell_x] = step_offset

            # print(f"{local_probability_factor}\t{probability_cutoff}")
            if depth > 1 and local_probability_factor > probability_cutoff:
                recursion_probabilities, recursion_min_player_steps = \
                    calculate_probabilities_for_player(board, next_player_state,
                                                       depth=depth - 1,
                                                       step_offset=step_offset + 1,
                                                       probability_cutoff=probability_cutoff,
                                                       global_probability_factor=local_probability_factor)
                probabilities += recursion_probabilities
                min_player_steps = np.minimum(min_player_steps,
                                              recursion_min_player_steps)

    return probabilities, min_player_steps
 def verify_state(self, board: Board) -> bool:
     # check for speed conditions and if the new position is on the board
     return self.verify_speed() and board.point_is_on_board(
         self.position_x, self.position_y)