Ejemplo n.º 1
0
    def test_json_repr(example_small_saved_game: SavedGame) -> None:
        """Should convert a SavedGame into a dict with all of its fields."""
        result = example_small_saved_game.json_repr()

        assert result == {
            "all_statements": (
                Statement("I am a Villager.",
                          ((0, frozenset({Role.VILLAGER})), )),
                Statement(
                    "I am a Robber and I swapped with Player 2. I am now a Seer.",
                    ((1, frozenset({Role.ROBBER})),
                     (2, frozenset({Role.SEER}))),
                    ((SwitchPriority.ROBBER, 1, 2), ),
                ),
                Statement(
                    "I am a Seer and I saw that Player 1 was a Robber.",
                    ((2, frozenset({Role.SEER})),
                     (1, frozenset({Role.ROBBER}))),
                ),
            ),
            "game_roles": (Role.VILLAGER, Role.SEER, Role.ROBBER),
            "original_roles": (Role.VILLAGER, Role.ROBBER, Role.SEER),
            "player_objs": (
                Villager(0),
                Robber(1, 2, Role.SEER),
                Seer(2, (1, Role.ROBBER)),
            ),
            "type":
            "SavedGame",
        }
Ejemplo n.º 2
0
    def test_get_all_statements() -> None:
        """Should return possible statements from all possible initializations."""
        player_index = 2
        set_roles(Role.WOLF, Role.SEER, Role.MASON, Role.VILLAGER)
        const.NUM_PLAYERS = 3
        expected_statements = (
            Statement(
                "I am a Mason. The other Mason is not present.",
                (
                    (2, frozenset({Role.MASON})),
                    (0, frozenset({Role.WOLF, Role.SEER, Role.VILLAGER})),
                    (1, frozenset({Role.WOLF, Role.SEER, Role.VILLAGER})),
                ),
            ),
            Statement(
                "I am a Mason. The other Mason is Player 0.",
                ((2, frozenset({Role.MASON})), (0, frozenset({Role.MASON}))),
            ),
            Statement(
                "I am a Mason. The other Mason is Player 1.",
                ((2, frozenset({Role.MASON})), (1, frozenset({Role.MASON}))),
            ),
        )

        result = Mason.get_all_statements(player_index)

        assert result == expected_statements
Ejemplo n.º 3
0
    def test_get_all_statements() -> None:
        """Should return possible statements from all possible initializations."""
        player_index = 1
        set_roles(Role.WOLF, Role.ROBBER, Role.VILLAGER)
        const.NUM_PLAYERS = 2
        expected = (
            Statement(
                "I am a Robber and I swapped with Player 0. I am now a Villager.",
                ((1, frozenset({Role.ROBBER})),
                 (0, frozenset({Role.VILLAGER}))),
                ((SwitchPriority.ROBBER, 1, 0), ),
                Role.ROBBER,
            ),
            Statement(
                "I am a Robber and I swapped with Player 0. I am now a Wolf.",
                ((1, frozenset({Role.ROBBER})), (0, frozenset({Role.WOLF}))),
                ((SwitchPriority.ROBBER, 1, 0), ),
                Role.ROBBER,
            ),
            Statement(
                "I am a Robber and I swapped with Player 0. I am now a Robber.",
                ((1, frozenset({Role.ROBBER})), (0, frozenset({Role.ROBBER}))),
                ((SwitchPriority.ROBBER, 1, 0), ),
                Role.ROBBER,
            ),
        )

        result = Robber.get_all_statements(player_index)

        assert set(result) == set(expected)
Ejemplo n.º 4
0
def medium_statement_list() -> tuple[Statement, ...]:
    return (
        Statement(
            "I am a Seer and I saw that Player 2 was a Drunk.",
            ((0, frozenset({Role.SEER})), (2, frozenset({Role.DRUNK}))),
        ),
        Statement(
            "I am a Seer and I saw that Player 3 was a Minion.",
            ((1, frozenset({Role.SEER})), (3, frozenset({Role.MINION}))),
        ),
        Statement(
            "I am a Drunk and I swapped with Center 0.",
            ((2, frozenset({Role.DRUNK})),),
            ((SwitchPriority.DRUNK, 2, 5),),
        ),
        Statement(
            "I am a Robber and I swapped with Player 2. I am now a Drunk.",
            ((3, frozenset({Role.ROBBER})), (2, frozenset({Role.DRUNK}))),
            ((SwitchPriority.ROBBER, 3, 2),),
        ),
        Statement(
            "I am a Seer and I saw that Player 1 was a Wolf.",
            ((4, frozenset({Role.SEER})), (1, frozenset({Role.WOLF}))),
        ),
    )
Ejemplo n.º 5
0
def small_statement_list() -> tuple[Statement, ...]:
    return (
        Statement("I am a Villager.", ((0, frozenset({Role.VILLAGER})),)),
        Statement(
            "I am a Robber and I swapped with Player 2. I am now a Seer.",
            ((1, frozenset({Role.ROBBER})), (2, frozenset({Role.SEER}))),
            ((SwitchPriority.ROBBER, 1, 2),),
        ),
        Statement(
            "I am a Seer and I saw that Player 1 was a Robber.",
            ((2, frozenset({Role.SEER})), (1, frozenset({Role.ROBBER}))),
        ),
    )
Ejemplo n.º 6
0
def example_statement() -> Statement:
    return Statement(
        "test",
        ((2, frozenset({Role.ROBBER})), (0, frozenset({Role.SEER}))),
        ((SwitchPriority.ROBBER, 2, 0),),
        Role.ROBBER,
    )
Ejemplo n.º 7
0
    def test_get_villager_statements() -> None:
        """Execute initialization actions and return the possible statements."""
        player_index = 0

        result = Villager.get_villager_statements(player_index)

        assert result == (Statement("I am a Villager.",
                                    ((0, frozenset({Role.VILLAGER})), )), )
Ejemplo n.º 8
0
    def test_get_all_statements() -> None:
        """Should return possible statements from all possible initializations."""
        player_index = 2

        result = Villager.get_all_statements(player_index)

        assert result == (Statement("I am a Villager.",
                                    ((2, frozenset({Role.VILLAGER})), )), )
Ejemplo n.º 9
0
    def test_constructor() -> None:
        """Should initialize using the given sentence and knowledge."""
        result = Statement("test", ((1, frozenset({Role.VILLAGER})), ))

        assert result.sentence == "test"
        assert result.knowledge == ((1, frozenset({Role.VILLAGER})), )
        assert result.switches == ()
        assert result.speaker is Role.VILLAGER
Ejemplo n.º 10
0
    def test_awake_init() -> None:
        """Should initialize a Hunter."""
        player_index = 5

        hunter = Hunter.awake_init(player_index, [])  # game_roles is unused.

        assert hunter.statements == (Statement(
            "I am a Hunter.", ((5, frozenset({Role.HUNTER})), )), )
Ejemplo n.º 11
0
    def test_awake_init() -> None:
        """Should initialize a Villager."""
        player_index = 5

        villager = Villager.awake_init(player_index,
                                       [])  # game_roles is unused.

        assert villager.statements == (Statement(
            "I am a Villager.", ((5, frozenset({Role.VILLAGER})), )), )
Ejemplo n.º 12
0
    def test_eq(example_statement: Statement) -> None:
        """Should declare two Statements with identical fields to be equal."""
        result = Statement(
            "test",
            ((2, frozenset({Role.ROBBER})), (0, frozenset({Role.SEER}))),
            ((SwitchPriority.ROBBER, 2, 0), ),
        )

        assert result == example_statement
Ejemplo n.º 13
0
    def test_constructor() -> None:
        """Should initialize correctly."""
        villager_statement = Statement("I am a Villager.",
                                       ((0, frozenset({Role.VILLAGER})), ))

        result = SavedGame((Role.VILLAGER, ), (Role.VILLAGER, ),
                           (villager_statement, ), (Villager(0), ))

        assert isinstance(result, stats.SavedGame)
Ejemplo n.º 14
0
def example_small_saved_game(small_game_roles: tuple[Role, ...]) -> SavedGame:
    return SavedGame(
        (Role.VILLAGER, Role.ROBBER, Role.SEER),
        (Role.VILLAGER, Role.SEER, Role.ROBBER),
        (
            Statement("I am a Villager.", ((0, frozenset({Role.VILLAGER})),)),
            Statement(
                "I am a Robber and I swapped with Player 2. I am now a Seer.",
                ((1, frozenset({Role.ROBBER})), (2, frozenset({Role.SEER}))),
                ((SwitchPriority.ROBBER, 1, 2),),
            ),
            Statement(
                "I am a Seer and I saw that Player 1 was a Robber.",
                ((2, frozenset({Role.SEER})), (1, frozenset({Role.ROBBER}))),
            ),
        ),
        (Villager(0), Robber(1, 2, Role.SEER), Seer(2, (1, Role.ROBBER), (None, None))),
    )
Ejemplo n.º 15
0
 def get_drunk_statements(player_index: int,
                          choice_ind: int) -> tuple[Statement, ...]:
     """Gets Drunk Statement."""
     sentence = (
         f"I am a Drunk and I swapped with Center {choice_ind - const.NUM_PLAYERS}."
     )
     knowledge = ((player_index, frozenset({Role.DRUNK})), )
     switches = ((SwitchPriority.DRUNK, player_index, choice_ind), )
     return (Statement(sentence, knowledge, switches), )
Ejemplo n.º 16
0
    def test_get_statement_inheritance() -> None:
        """Classes extending Player should contain a get_statement method."""
        villager = Villager(0)
        knowledge_base = KnowledgeBase()

        statement = villager.get_statement(knowledge_base)

        assert statement == Statement("I am a Villager.",
                                      ((0, frozenset({Role.VILLAGER})), ))
Ejemplo n.º 17
0
    def test_default_saved_game(example_small_saved_game: SavedGame) -> None:
        """Should convert objects of different types to JSON."""
        player_objs = [
            Villager(0),
            Robber(1, 2, Role.SEER),
            Seer(2, (1, Role.ROBBER))
        ]
        player_strs = ", ".join(
            [json.dumps(player, cls=WolfBotEncoder) for player in player_objs])
        statement_objs = [
            Statement(
                "I am a Villager.",
                ((0, frozenset({Role.VILLAGER})), ),
                speaker=Role.VILLAGER,
            ),
            Statement(
                "I am a Robber and I swapped with Player 2. I am now a Seer.",
                ((1, frozenset({Role.ROBBER})), (2, frozenset({Role.SEER}))),
                ((SwitchPriority.ROBBER, 1, 2), ),
                Role.ROBBER,
            ),
            Statement(
                "I am a Seer and I saw that Player 1 was a Robber.",
                ((2, frozenset({Role.SEER})), (1, frozenset({Role.ROBBER}))),
                speaker=Role.SEER,
            ),
        ]
        statement_strs = ", ".join([
            json.dumps(statement, cls=WolfBotEncoder)
            for statement in statement_objs
        ])

        result = json.dumps(example_small_saved_game, cls=WolfBotEncoder)
        assert result == (
            '{"type": "SavedGame",'
            ' "original_roles": [{"type": "Role", "data": "Villager"},'
            ' {"type": "Role", "data": "Robber"}, {"type": "Role", "data": "Seer"}],'
            ' "game_roles": [{"type": "Role", "data": "Villager"},'
            ' {"type": "Role", "data": "Seer"}, {"type": "Role", "data": "Robber"}],'
            f' "all_statements": [{statement_strs}],'
            f' "player_objs": [{player_strs}]}}')

        reverted_result = json.loads(result, cls=WolfBotDecoder)
        assert reverted_result == example_small_saved_game
Ejemplo n.º 18
0
    def test_get_mason_statements() -> None:
        """Execute initialization actions and return the possible statements."""
        player_index = 9

        result = Mason.get_mason_statements(player_index, (6, 9))

        assert result == (Statement(
            "I am a Mason. The other Mason is Player 6.",
            ((9, frozenset({Role.MASON})), (6, frozenset({Role.MASON}))),
        ), )
Ejemplo n.º 19
0
    def test_get_seer_statements() -> None:
        """Execute initialization actions and return the possible statements."""
        player_index = 1

        result = Seer.get_seer_statements(player_index, (6, Role.ROBBER))

        assert result == (Statement(
            "I am a Seer and I saw that Player 6 was a Robber.",
            ((1, frozenset({Role.SEER})), (6, frozenset({Role.ROBBER}))),
        ), )
Ejemplo n.º 20
0
def example_small_game_result(
        small_game_roles: tuple[Role, ...]) -> GameResult:
    return GameResult(
        (Role.VILLAGER, Role.ROBBER, Role.SEER),
        (Role.VILLAGER, Role.ROBBER, Role.SEER),
        (),
        Team.VILLAGE,
        (
            Statement("I am a Villager.", ((0, frozenset({Role.VILLAGER})), )),
            Statement(
                "I am a Seer and I saw that Player 2 was a Robber.",
                ((1, frozenset({Role.SEER})), (2, frozenset({Role.ROBBER}))),
            ),
            Statement(
                "I am a Robber and I swapped with Player 1. I am now a Seer.",
                ((2, frozenset({Role.ROBBER})), (1, frozenset({Role.SEER}))),
                ((SwitchPriority.ROBBER, 2, 1), ),
            ),
        ),
    )
Ejemplo n.º 21
0
    def test_hash(example_statement: Statement) -> None:
        """Should give two Statements with identical fields the same hash."""
        identical_statement = Statement(
            "test",
            ((2, frozenset({Role.ROBBER})), (0, frozenset({Role.SEER}))),
            ((SwitchPriority.ROBBER, 2, 0), ),
        )

        result = {identical_statement, example_statement}

        assert result == {example_statement}
Ejemplo n.º 22
0
    def test_awake_init(large_game_roles: tuple[Role, ...]) -> None:
        """Should initialize a Mason."""
        player_index = 6

        mason = Mason.awake_init(player_index, list(large_game_roles))

        assert mason.mason_indices == (6, 9)
        assert mason.statements == (Statement(
            "I am a Mason. The other Mason is Player 9.",
            ((6, frozenset({Role.MASON})), (9, frozenset({Role.MASON}))),
        ), )
Ejemplo n.º 23
0
 def get_troublemaker_statements(
     player_index: int, tmkr_ind1: int, tmkr_ind2: int
 ) -> tuple[Statement, ...]:
     """Gets Troublemaker Statement."""
     sentence = (
         f"I am a Troublemaker and I swapped Player {tmkr_ind1} "
         f"and Player {tmkr_ind2}."
     )
     knowledge = ((player_index, frozenset({Role.TROUBLEMAKER})),)
     switches = ((SwitchPriority.TROUBLEMAKER, tmkr_ind1, tmkr_ind2),)
     return (Statement(sentence, knowledge, switches),)
Ejemplo n.º 24
0
    def test_get_robber_statements() -> None:
        """Execute initialization actions and return the possible statements."""
        player_index = 4

        result = Robber.get_robber_statements(player_index, 3, Role.SEER)

        assert result == (Statement(
            "I am a Robber and I swapped with Player 3. I am now a Seer.",
            ((4, frozenset({Role.ROBBER})), (3, frozenset({Role.SEER}))),
            ((SwitchPriority.ROBBER, 4, 3), ),
            Role.ROBBER,
        ), )
Ejemplo n.º 25
0
    def test_get_insomniac_statements() -> None:
        """Execute initialization actions and return the possible statements."""
        player_index = 0
        expected = (Statement(
            ("I am a Insomniac and when I woke up I was a Hunter. "
             "I don't know who I switched with."),
            ((0, frozenset({Role.INSOMNIAC})), ),
        ), )

        result = Insomniac.get_insomniac_statements(player_index, Role.HUNTER)

        assert result == expected
Ejemplo n.º 26
0
    def test_get_troublemaker_statements() -> None:
        """Execute initialization actions and return the possible statements."""
        player_index = 1

        result = Troublemaker.get_troublemaker_statements(player_index, 6, 3)

        assert result == (Statement(
            "I am a Troublemaker and I swapped Player 6 and Player 3.",
            ((1, frozenset({Role.TROUBLEMAKER})), ),
            ((SwitchPriority.TROUBLEMAKER, 6, 3), ),
            Role.TROUBLEMAKER,
        ), )
Ejemplo n.º 27
0
    def test_awake_init() -> None:
        """Should initialize a Insomniac."""
        player_index = 1
        game_roles = [Role.INSOMNIAC, Role.ROBBER, Role.VILLAGER]
        expected = (Statement(
            ("I am a Insomniac and when I woke up I was a Robber. "
             "I don't know who I switched with."),
            ((1, frozenset({Role.INSOMNIAC})), ),
        ), )

        insomniac = Insomniac.awake_init(player_index, game_roles)

        assert insomniac.new_role is Role.ROBBER
        assert insomniac.statements == expected
Ejemplo n.º 28
0
def example_medium_saved_game(medium_game_roles: tuple[Role, ...]) -> SavedGame:
    return SavedGame(
        (Role.SEER, Role.WOLF, Role.DRUNK, Role.ROBBER, Role.MINION, Role.TROUBLEMAKER),
        (Role.SEER, Role.WOLF, Role.TROUBLEMAKER, Role.DRUNK, Role.MINION, Role.ROBBER),
        (
            Statement(
                "I am a Seer and I saw that Player 2 was a Drunk.",
                ((0, frozenset({Role.SEER})), (2, frozenset({Role.DRUNK}))),
            ),
            Statement(
                "I am a Robber and I swapped with Player 0. I am now a Seer.",
                ((1, frozenset({Role.ROBBER})), (0, frozenset({Role.SEER}))),
                ((SwitchPriority.ROBBER, 1, 0),),
            ),
            Statement(
                "I am a Drunk and I swapped with Center 0.",
                ((2, frozenset({Role.DRUNK})),),
                ((SwitchPriority.DRUNK, 2, 5),),
            ),
            Statement(
                "I am a Robber and I swapped with Player 2. I am now a Drunk.",
                ((3, frozenset({Role.ROBBER})), (2, frozenset({Role.DRUNK}))),
                ((SwitchPriority.ROBBER, 3, 2),),
            ),
            Statement(
                "I am a Seer and I saw that Player 3 was a Robber.",
                ((4, frozenset({Role.SEER})), (3, frozenset({Role.ROBBER}))),
            ),
        ),
        (
            Seer(0, (2, Role.DRUNK)),
            Wolf(1, (1,), 5, Role.TROUBLEMAKER),
            Drunk(2, 5),
            Robber(3, 2, Role.DRUNK),
            Minion(4, (1,)),
        ),
    )
Ejemplo n.º 29
0
 def get_insomniac_statements(
     player_index: int,
     insomniac_new_role: Role,
     new_insomniac_index: int | None = None,
 ) -> tuple[Statement, ...]:
     """Gets Insomniac Statement."""
     knowledge = ((player_index, frozenset({Role.INSOMNIAC})),)
     sentence = f"I am a Insomniac and when I woke up I was a {insomniac_new_role}."
     if new_insomniac_index is None:
         if insomniac_new_role is not Role.INSOMNIAC:
             sentence += " I don't know who I switched with."
     else:
         sentence += f" I switched with Player {new_insomniac_index}."
     # switches = ((player_index, new_insomniac_index),)  # TODO
     return (Statement(sentence, knowledge),)
Ejemplo n.º 30
0
    def test_negate_all(large_game_roles: tuple[Role, ...],
                        example_statement: Statement) -> None:
        """Negate-all statements contain the opposite of all clauses."""
        expected = Statement(
            "NOT - test",
            (
                (2, const.ROLE_SET - frozenset({Role.ROBBER})),
                (0, const.ROLE_SET - frozenset({Role.SEER})),
            ),
            speaker=Role.ROBBER,
        )

        result = example_statement.negate_all()

        assert str(result) == str(expected)