Exemplo n.º 1
0
def alpha_beta_result(game_state: GameState, max_depth: int, best_black: int,
                      best_white: int, eval_fn: Callable) -> int:
    if game_state.is_over():
        return MAX_SCORE if game_state.winner(
        ) == game_state.next_player else MIN_SCORE

    if max_depth == 0:
        return eval_fn(game_state)

    best_so_far = MIN_SCORE
    for candidate_move in game_state.legal_moves():
        next_state = game_state.apply_move(candidate_move)
        opponent_best_result = alpha_beta_result(next_state, max_depth - 1,
                                                 best_black, best_white,
                                                 eval_fn)
        our_result = -opponent_best_result

        if our_result > best_so_far:
            best_so_far = our_result
        if game_state.next_player == Player.WHITE:
            if best_so_far > best_white:
                best_white = best_so_far
            outcome_for_black = -best_so_far
            if outcome_for_black < best_black:
                return best_so_far
        elif game_state.next_player == Player.BLACK:
            if best_so_far > best_black:
                best_black = best_so_far
            outcome_for_white = -best_so_far
            if outcome_for_white < best_white:
                return best_so_far
    return best_so_far
Exemplo n.º 2
0
def best_result(game_state: GameState, max_depth: int, eval_fn: Callable):
    if game_state.is_over():
        return MAX_SCORE if game_state.winner(
        ) == game_state.next_player else MIN_SCORE

    if max_depth == 0:
        return eval_fn(game_state)

    best_so_far = MIN_SCORE
    for candidate_move in game_state.legal_moves():
        next_state = game_state.apply_move(candidate_move)
        opponent_best_result = best_result(next_state, max_depth - 1, eval_fn)
        our_result = -1 * opponent_best_result
        if our_result > best_so_far:
            best_so_far = our_result
    return best_so_far
Exemplo n.º 3
0
def best_result(game_state: GameState) -> GameResult:
    if game_state.is_over():
        if game_state.winner() == game_state.next_player:
            return GameResult.Win
        elif game_state.winner() is None:
            return GameResult.Draw
        else:
            return GameResult.Loss

    best_result_so_far = GameResult.Loss
    for candidate_move in game_state.legal_moves():
        next_state = game_state.apply_move(candidate_move)
        opponent_best_result = best_result(next_state)
        our_result = reverse_game_result(opponent_best_result)
        if our_result.value > best_result_so_far.value:
            best_result_so_far = our_result

    return best_result_so_far