Exemplo n.º 1
0
    def test_brute_force_on_card_with_true_result(self):
        brute_forcer = self.empty_brute_forcer
        knowledge_table = self.knowledge_table
        clues = self.clues

        clues.add_rumour(
            Rumour(tom, [Cards.ROODHART, Cards.MES, Cards.EETKAMER],
                   [(menno, Knowledge.TRUE)]))
        knowledge_table.set(menno, Cards.ROODHART, Knowledge.FALSE)
        knowledge_table.set(menno, Cards.MES, Knowledge.FALSE)

        # For speed:
        tom_cards = [Cards.DEWIT, Cards.BUBBELBAD, Cards.BIJL, Cards.KEUKEN]
        menno_cards = [
            Cards.BLAAUWVANDRAET, Cards.HAL, Cards.HALTER, Cards.KANDELAAR
        ]
        michiel_cards = [
            Cards.PISTOOL, Cards.GASTENVERBLIJF, Cards.KNUPPEL, Cards.THEATER
        ]
        player_cards = {
            tom: tom_cards,
            menno: menno_cards,
            michiel: michiel_cards
        }
        for player in [tom, menno, michiel]:
            for card in player_cards[player]:
                knowledge_table.set(player, card, Knowledge.TRUE)
        self.assertEqual(
            Knowledge.TRUE,
            brute_forcer.brute_force_on_card(menno, Cards.EETKAMER))
Exemplo n.º 2
0
    def do_rumour(self, arg):
        """"rumour <claimer> <card1> <card2> <card3>"""
        args = arg.split()

        if len(args) != 4:
            print('Usage: ' + str(self.do_rumour.__doc__))
            return False

        claimer_name = args[0]
        card_names = args[1:]
        rumour_cards = [
            match_card(card_name, self.context.cards)
            for card_name in card_names
        ]
        claimer = match_player(claimer_name, self.context.players)

        if not all(rumour_cards) or not claimer:
            return False

        if not {card.category
                for card in rumour_cards
                } == {Category.CHARACTER, Category.ROOM, Category.WEAPON}:
            print("A rumour should contain all categories")
            return False

        replies = self.ask_replies()
        if not replies:
            return False
        rumour = Rumour(claimer, rumour_cards, replies)
        self.watson.add_rumour(rumour)
        print(self.watson.display_state())
        if self.auto_save:
            self.do_save()
Exemplo n.º 3
0
 def test_add_info_from_clues(self):
     clues = Clues(context_fixture)
     clues.add_card(Cards.BLAAUWVANDRAET, tom)
     clues.add_card(Cards.KANDELAAR, tom)
     rumour_cards = [
         Cards.BLAAUWVANDRAET, Cards.KANDELAAR, Cards.GASTENVERBLIJF
     ]
     clues.add_rumour(
         Rumour(menno, rumour_cards, [(menno, Knowledge.FALSE),
                                      (michiel, Knowledge.TRUE)]))
     watson = self.empty_watson
     watson.add_info_from_clues(clues)
     for card in rumour_cards:
         for player in context_fixture.players:
             if (card is Cards.KANDELAAR
                     or card is Cards.BLAAUWVANDRAET) and player is tom:
                 self.assertEqual(
                     Knowledge.TRUE,
                     watson.get_knowledge_table().get(player, card))
             elif card is Cards.GASTENVERBLIJF and player is michiel:
                 self.assertEqual(
                     Knowledge.TRUE,
                     watson.get_knowledge_table().get(player, card))
             else:
                 self.assertEqual(
                     Knowledge.FALSE,
                     watson.get_knowledge_table().get(player, card))
Exemplo n.º 4
0
 def test_has_solution_too_many_cards(self):
     solution_finder = self.half_full_solution_finder
     solution_finder.clues.add_rumour(
         Rumour(
             tom,
             [Cards.BLAAUWVANDRAET, Cards.BUBBELBAD, Cards.MES],
             [(menno, Knowledge.TRUE)]
         )
     )
     solution_finder.clues.add_rumour(
         Rumour(
             tom,
             [Cards.BLAAUWVANDRAET, Cards.BUBBELBAD, Cards.KANDELAAR],
             [(menno, Knowledge.TRUE)]
         )
     )
     solution_finder.knowledge_table.set(menno, Cards.PISTOOL, Knowledge.TRUE)
     self.assertEqual(None, solution_finder.find_possible_solution())
Exemplo n.º 5
0
 def test_derive_from_new_rumour_with_positive_reply_empty_knowledge_table(
         self):
     deriver = self.empty_deriver
     deriver.derive_from_new_rumour(
         Rumour(tom, [Cards.KANDELAAR, Cards.BUBBELBAD, Cards.DEWIT], [
             (menno, Knowledge.TRUE),
         ]))
     clauses = deriver.get_clauses().get_clauses(menno)
     self.assertEqual([Cards.KANDELAAR, Cards.BUBBELBAD, Cards.DEWIT],
                      clauses[0])
Exemplo n.º 6
0
 def test_derive_from_new_rumour_with_positive_reply_with_card_owned(self):
     deriver = self.empty_deriver
     knowledge_table = self.empty_knowledge_table
     knowledge_table.set(menno, Cards.KANDELAAR, Knowledge.TRUE)
     deriver.derive_from_new_rumour(
         Rumour(tom, [Cards.KANDELAAR, Cards.BUBBELBAD, Cards.DEWIT], [
             (menno, Knowledge.TRUE),
         ]))
     clauses = deriver.get_clauses().get_clauses(menno)
     self.assertEqual(0, len(clauses))
Exemplo n.º 7
0
    def test_brute_force_on_card_with_false_result(self):
        brute_forcer = self.empty_brute_forcer
        clues = self.clues

        clues.add_rumour(
            Rumour(tom, [Cards.ROODHART, Cards.MES, Cards.EETKAMER],
                   [(menno, Knowledge.FALSE)]))
        self.assertEqual(
            Knowledge.FALSE,
            brute_forcer.brute_force_on_card(menno, Cards.EETKAMER))
Exemplo n.º 8
0
    def test_add_rumour(self):
        watson = self.empty_watson

        watson.knowledge_table.set(michiel, Cards.KANDELAAR, Knowledge.FALSE)
        watson.knowledge_table.set(michiel, Cards.HAL, Knowledge.FALSE)

        watson.add_rumour(
            Rumour(menno, [Cards.KANDELAAR, Cards.HAL, Cards.ROODHART],
                   [(michiel, Knowledge.TRUE)]))
        self.assertEqual(Knowledge.TRUE,
                         watson.knowledge_table.get(michiel, Cards.ROODHART))
Exemplo n.º 9
0
    def test_do_rumour(self, mock_inputs):
        self.shell.onecmd("r Menno rood bijl eetkamer")

        reference_rumour = Rumour(
            menno,
            [Cards.ROODHART, Cards.BIJL, Cards.EETKAMER],
            replies=[
                (tom, Knowledge.FALSE),
                (michiel, Knowledge.TRUE),
            ]
        )

        self.watson_mock.add_rumour.assert_called_with(reference_rumour)
Exemplo n.º 10
0
 def test_derive_from_new_rumour_with_negative_reply(self):
     deriver = self.empty_deriver
     knowledge_table = self.empty_knowledge_table
     deriver.derive_from_new_rumour(
         Rumour(tom, [Cards.KANDELAAR, Cards.BUBBELBAD, Cards.DEWIT], [
             (menno, Knowledge.FALSE),
         ]))
     self.assertEqual(Knowledge.FALSE,
                      knowledge_table.get(menno, Cards.KANDELAAR))
     self.assertEqual(Knowledge.FALSE,
                      knowledge_table.get(menno, Cards.BUBBELBAD))
     self.assertEqual(Knowledge.FALSE,
                      knowledge_table.get(menno, Cards.DEWIT))
Exemplo n.º 11
0
 def test_has_solution_false_negative(self):
     solution_finder = self.half_full_solution_finder
     solution_finder.clues.add_rumour(
         Rumour(
             tom,
             [Cards.KNUPPEL, Cards.BUBBELBAD, Cards.BLAAUWVANDRAET],
             [
                 (menno, Knowledge.FALSE),
                 (michiel, Knowledge.TRUE)
             ]
         )
     )
     self.assertEqual(None, solution_finder.find_possible_solution())
Exemplo n.º 12
0
 def test_inspect_clauses(self):
     deriver = self.empty_deriver
     knowledge_table = self.empty_knowledge_table
     deriver.derive_from_new_rumour(
         Rumour(tom, [Cards.KANDELAAR, Cards.BUBBELBAD, Cards.DEWIT], [
             (menno, Knowledge.TRUE),
         ]))
     deriver.derive_from_new_knowledge(menno, Cards.KANDELAAR,
                                       Knowledge.FALSE)
     deriver.derive_from_new_knowledge(menno, Cards.BUBBELBAD,
                                       Knowledge.FALSE)
     deriver.inspect_clauses()
     self.assertEqual(Knowledge.TRUE,
                      knowledge_table.get(menno, Cards.DEWIT))
Exemplo n.º 13
0
 def test_possible_solution_only_rumours(self):
     solution_finder = self.empty_solution_finder
     solution_finder.clues.add_rumour(
         Rumour(
             tom,
             [Cards.KANDELAAR, Cards.BUBBELBAD, Cards.DEWIT],
             [
                 (menno, Knowledge.FALSE),
                 (michiel, Knowledge.TRUE)
             ]
         )
     )
     solution_finder.clues.add_rumour(
         Rumour(
             menno,
             [Cards.HALTER, Cards.HAL, Cards.PIMPEL],
             [
                 (tom, Knowledge.TRUE),
                 (michiel, Knowledge.FALSE)
             ]
         )
     )
     self.assertFalse(solution_finder.find_possible_solution() is None)
Exemplo n.º 14
0
def retrieve_rumours(players, rumours_json, used_cards) -> List[Rumour]:
    rumours = []
    for rumour_json in rumours_json:
        claimer = find_player_by_name(rumour_json["claimer"], players)
        rumour_cards = []
        for card_name in rumour_json["cards"]:
            rumour_cards.append(find_card_by_name(card_name, used_cards))
        replies = []
        for player_name in rumour_json["replied_yes"]:
            replies.append((find_player_by_name(player_name,
                                                players), Knowledge.TRUE))
        for player_name in rumour_json["replied_no"]:
            replies.append((find_player_by_name(player_name,
                                                players), Knowledge.FALSE))
        rumour = Rumour(claimer, rumour_cards, replies)
        rumours.append(rumour)
    return rumours
Exemplo n.º 15
0
    def test_brute_forcer_timeout(self):
        brute_forcer = self.empty_brute_forcer
        knowledge_table = self.knowledge_table
        clues = self.clues

        clues.add_rumour(
            Rumour(tom, [Cards.ROODHART, Cards.MES, Cards.EETKAMER],
                   [(menno, Knowledge.TRUE)]))
        knowledge_table.set(menno, Cards.ROODHART, Knowledge.FALSE)
        knowledge_table.set(menno, Cards.MES, Knowledge.FALSE)
        timeout_sec = 1

        start_time_sec = time.time()
        with self.assertRaises(BruteForceTimeoutError):
            brute_forcer.brute_force_on_card(menno,
                                             Cards.EETKAMER,
                                             timeout_sec=timeout_sec)
        end_time_sec = time.time()

        diff_time_sec = end_time_sec - start_time_sec
        self.assertAlmostEqual(timeout_sec, diff_time_sec, delta=0.1)