示例#1
0
def log_probability_dist(
        solution_probs: tuple[dict[Role, float], ...]) -> None:
    """
    Logs probability distributions from solutions.
    If the distribution is uniform, condenses it into a single 'All' key.
    If the distibution only has one nonzero value, only prints that value.
    Else prints all results as a dict.
    """
    results = {}
    for index, probs in enumerate(solution_probs):
        prob_values = set(probs.values())
        if len(prob_values) == 1:
            results[index] = {
                cast(Role, "ALL"): round(next(iter(prob_values)), 3)
            }
        elif len(prob_values) == 2:
            results[index] = {
                role: round(score, 3)
                for role, score in probs.items() if score > 0
            }
        else:
            results[index] = {
                role: round(score, 3)
                for role, score in probs.items()
            }
    logger.debug(pformat(results))
示例#2
0
 def awake_init(cls, player_index: int, game_roles: list[Role]) -> Insomniac:
     """Initializes Insomniac - learns new role."""
     is_user = const.IS_USER[player_index]
     insomniac_new_role = game_roles[player_index]
     logger.debug(f"[Hidden] Insomniac wakes up as a {insomniac_new_role}.")
     if is_user:
         logger.info(f"You woke up as a {insomniac_new_role}!", cache=True)
     return cls(player_index, insomniac_new_role)
示例#3
0
 def awake_init(cls, player_index: int, game_roles: list[Role]) -> Minion:
     """Initializes Minion - gets Wolf indices."""
     is_user = const.IS_USER[player_index]
     wolf_indices = find_all_player_indices(game_roles, Role.WOLF)
     logger.debug(f"[Hidden] Wolves are at indices: {list(wolf_indices)}")
     if is_user:
         logger.info(f"Wolves are at indices: {list(wolf_indices)}", cache=True)
     return cls(player_index, wolf_indices)
示例#4
0
 def awake_init(cls, player_index: int, game_roles: list[Role]) -> Drunk:
     """Initializes Drunk - switches with a card in the center."""
     is_user = const.IS_USER[player_index]
     choice_ind = get_center(is_user)
     logger.debug(
         f"[Hidden] Drunk switches with Center Card {choice_ind - const.NUM_PLAYERS}"
         f" and unknowingly becomes a {game_roles[choice_ind]}.")
     if is_user:
         logger.info("You do not know your new role.", cache=True)
     swap_characters(game_roles, player_index, choice_ind)
     return cls(player_index, choice_ind)
示例#5
0
def get_confidence(all_predictions: tuple[tuple[Role, ...], ...]) -> tuple[float, ...]:
    """
    Creates confidence levels for each prediction and takes most
    common role guess array as the final guess for that index.
    guess_histogram stores counts of prediction arrays.
    wolf_votes stores individual votes for Wolves.
    """
    confidence = []
    for i in range(const.NUM_ROLES):
        role_dict: dict[Role, int] = {role: 0 for role in const.ROLE_SET}
        for prediction in all_predictions:
            role_dict[prediction[i]] += 1
        count = max(role_dict.values())
        confidence.append(count / const.NUM_PLAYERS)

    logger.debug(f"Confidence levels: {[float(f'{conf:.2f}') for conf in confidence]}")
    return tuple(confidence)
示例#6
0
    def awake_init(cls, player_index: int, game_roles: list[Role]) -> Troublemaker:
        """Initializes Troublemaker - switches one player with another player."""
        is_user = const.IS_USER[player_index]
        if is_user:
            logger.info("Choose two players to switch places:")
        choice_1 = get_player(is_user, (player_index,))
        choice_2 = get_player(is_user, (player_index, choice_1))

        swap_characters(game_roles, choice_1, choice_2)
        logger.debug(
            f"[Hidden] Troublemaker switches Player {choice_1} and Player {choice_2}."
        )
        if is_user:
            logger.info(
                f"You switch Player {choice_1} with Player {choice_2}.", cache=True
            )

        return cls(player_index, choice_1, choice_2)
示例#7
0
文件: seer.py 项目: TylerYep/wolfbot
    def awake_init(cls, player_index: int, game_roles: list[Role]) -> Seer:
        """Initializes Seer - either sees 2 center cards or 1 player card."""
        is_user = const.IS_USER[player_index]
        if const.NUM_CENTER > 1:
            if is_user:
                logger.info("Do you want to see 1 player card or 2 center cards?")
                choose_center = bool(get_numeric_input(1, 3) - 1)
            else:
                # Pick two center cards more often, because
                # that generally yields higher win rates.
                choose_center = weighted_coin_flip(const.CENTER_SEER_PROB)

            if choose_center:
                peek_ind1 = get_center(is_user)
                peek_ind2 = get_center(is_user, (peek_ind1,))
                peek_char1 = game_roles[peek_ind1]
                peek_char2 = game_roles[peek_ind2]
                logger.debug(
                    f"[Hidden] Seer sees that Center {peek_ind1 - const.NUM_PLAYERS} "
                    f"is a {peek_char1}, Center {peek_ind2 - const.NUM_PLAYERS} "
                    f"is a {peek_char2}."
                )
                if is_user:
                    logger.info(
                        f"You see that Center {peek_ind1 - const.NUM_PLAYERS} "
                        f"is a {peek_char1}, and "
                        f"Center {peek_ind2 - const.NUM_PLAYERS} is a {peek_char2}.",
                        cache=True,
                    )
                return cls(
                    player_index, (peek_ind1, peek_char1), (peek_ind2, peek_char2)
                )

        peek_ind = get_player(is_user, (player_index,))
        peek_char = game_roles[peek_ind]
        logger.debug(f"[Hidden] Seer sees that Player {peek_ind} is a {peek_char}.")
        if is_user:
            logger.info(f"You see that Player {peek_ind} is a {peek_char}.", cache=True)
        return cls(player_index, (peek_ind, peek_char))
示例#8
0
    def awake_init(cls, player_index: int,
                   game_roles: list[Role]) -> Doppelganger:
        """Initializes Doppelganger - learns new role."""
        from wolfbot.roles import get_role_obj

        is_user = const.IS_USER[player_index]
        choice_ind = get_player(is_user, (player_index, ))
        choice_char = game_roles[choice_ind]
        logger.debug(f"[Hidden] Doppelganger copies Player {choice_ind} "
                     f"and becomes a {choice_char}.")
        if is_user:
            logger.info(
                f"You copied Player {choice_ind} and are now a {choice_char}!",
                cache=True,
            )
        # Temporarily set Doppelganger in game_roles to the new role
        # so she wakes up with the other characters.
        game_roles[player_index] = choice_char
        if choice_char in (Role.WOLF, Role.MASON):
            get_role_obj(choice_char).awake_init(player_index, game_roles)

        # else do switches later
        return cls(player_index, choice_ind, choice_char)
示例#9
0
    def awake_init(cls, player_index: int, game_roles: list[Role]) -> Wolf:
        """
        Constructor: original_roles defaults to [] when a player becomes
        a Wolf and realizes it.
        Initializes Wolf - gets Wolf indices and a random center card, if applicable.
        """
        is_user = const.IS_USER[player_index]
        center_index, center_role = None, None
        wolf_indices = find_all_player_indices(game_roles, Role.WOLF)
        if len(wolf_indices) == 1 and const.NUM_CENTER > 0:
            center_index = get_center(is_user)
            center_role = game_roles[center_index]
            if is_user:
                logger.info(
                    f"You see Center {center_index - const.NUM_PLAYERS} "
                    f"is a {center_role}.",
                    cache=True,
                )
        logger.debug(f"[Hidden] Wolves are at indices: {list(wolf_indices)}")
        if is_user:
            logger.info(f"Wolves are at indices: {list(wolf_indices)}",
                        cache=True)

        return cls(player_index, wolf_indices, center_index, center_role)