Example #1
0
    def __init__(self, state, p_hand, p_id):
        """ must be fed with hands, which is a result from inverse_legal_moves, 
        which must have already had constraints propagated. And checked for solveability."""
        self.Q = 0
        self.N = 0
        self.children = {}
        self.parent = None
        self.state = state
        self.p_hand = set(p_hand)
        self.p_id = p_id
        self.card_constraints, self.number_constraints = inverse_legal_moves(
            state, p_hand, p_id)
        self.card_constraints = propagate_constraints(self.card_constraints,
                                                      self.number_constraints)

        if p_id == state.active:
            self.untried_actions = set(
                filter_equivalent_cards(state, state.actions(self.p_hand)))
            # if we are the current active player, we can save time by ignoring
            # equivalent options
        else:
            game_mode = state.game_mode
            current_round = state.get_current_round()
            if current_round:
                card = state.get_current_round()[1:4]
                starting_suit = con.SUITS_MAPPING[state.game_mode][card]
            else:
                starting_suit = None
            player = state.active
            self.untried_actions = filter_playable_cards(
                self.card_constraints, self.number_constraints, player,
                starting_suit, game_mode)
Example #2
0
    def test_davonlaufen_2(self):
        """ Test if we can recognize if someone has run away, and enough cards
        of the called suit have been played such that we can conclude that the 
        person who ran away has ALL of the remaining cards of the called suit. """
        game_mode = "Partner Eichel"
        state = GameState(game_mode=game_mode, offensive_player=0, active=1)
        called_suit = "Eichel"
        suits_mapping = con.SUITS_MAPPING[game_mode]

        fixed_history = ["E7_", "EK_", "E10", "H8_"]
        hand = ["EO_", "GO_", "SO_", "HO_", "EU_", "GU_", "HU_"]
        for card in fixed_history:
            state = state.result(card)

        all_cards_except_eichel = {
            c
            for c in con.ALL_CARDS if suits_mapping[c] != called_suit
        }
        remaining_cards_except_eichel = {
            c
            for c in all_cards_except_eichel
            if not (c in hand or c in fixed_history)
        }
        expected = {
            1: remaining_cards_except_eichel | {"EA_", "E8_", "E9_"},
            2: remaining_cards_except_eichel,
            3: remaining_cards_except_eichel
        }

        card_constraints, number_constraints = inverse_legal_moves(
            state, hand, 0)
        self.assertEqual(expected, card_constraints)
Example #3
0
    def test_full_game(self):
        """ Test to see if during the course of a full game, we can correctly
        deduce which cards the other player has. I'm undecided as to whether
        we want this test to have a random element or not."""
        # Random but fixed hands.
        hands = {
            0: {'SK_', 'S7_', 'H10', 'H7_', 'HK_', 'E8_', 'HU_', 'GU_'},
            1: {'G9_', 'HO_', 'S10', 'H9_', 'EO_', 'E10', 'GO_', 'GK_'},
            2: {'G10', 'SU_', 'G8_', 'E9_', 'G7_', 'SO_', 'S9_', 'S8_'},
            3: {'GA_', 'EU_', 'E7_', 'H8_', 'SA_', 'EA_', 'EK_', 'HA_'}
        }

        state = GameState(game_mode="Herz Solo", offensive_player=1, active=0)

        for _ in range(32):
            active = state.active
            action = random.choice(state.actions(hands[active]))
            hands[active].remove(action)
            state = state.result(action)

            for i in range(4):
                card_constraints, _ = inverse_legal_moves(state, hands[i], i)
                for p_num, card_set in card_constraints.items():
                    actual_hand = hands[p_num]
                    self.assertTrue(actual_hand.issubset(card_set))
Example #4
0
    def setUp(self):
        fixed_history = [
            "EO_", "HU_", "SU_", "E7_", "EA_", "E10", "EK_", "S7_", "SA_",
            "S10", "SK_", "G7_"
        ]

        hand = ["GO_", "SO_", "HO_", "EU_", "GU_"]
        state = GameState(game_mode="Herz Solo", offensive_player=0, active=0)

        for c in fixed_history:
            state = state.result(c)

        self.state = state
        self.card_constraints, self.number_constraints = inverse_legal_moves(
            state, hand, 0)
Example #5
0
    def __init__(self, state, p_hand, p_id):
        self.Q = 0
        self.N = 0
        self.children = {}
        self.parent = None
        self.state = state
        self.p_hand = set(p_hand)
        self.p_id = p_id
        self.card_constraints, self.number_constraints = inverse_legal_moves(
            state, p_hand, p_id)
        self.card_constraints = propagate_constraints(self.card_constraints,
                                                      self.number_constraints)
        if not check_solveable(self.card_constraints, self.number_constraints):
            assert (True == False)

        if p_id == state.active:
            self.untried_actions = set(iter(state.actions(p_hand)))
        else:
            self.untried_actions = set(self.card_constraints[state.active])
Example #6
0
    def test_davonlaufen_1(self):
        """ Test if we can recognize if someone has run away."""
        game_mode = "Partner Eichel"
        state = GameState(game_mode=game_mode, offensive_player=0, active=1)

        fixed_history = ["E7_", "EK_", "H7_", "H8_"]
        hand = ["EO_", "GO_", "SO_", "HO_", "EU_", "GU_", "HU_"]
        for card in fixed_history:
            state = state.result(card)

        remaining_cards = {
            c
            for c in con.ALL_CARDS if not c in (hand + fixed_history)
        }
        expected = {
            1: remaining_cards,
            2: remaining_cards.difference({"EA_"}),
            3: remaining_cards.difference({"EA_", "E10", "E8_", "E9_"})
        }

        card_constraints, number_constraints = inverse_legal_moves(
            state, hand, 0)

        self.assertEqual(expected, card_constraints)