示例#1
0
    def test_sample(self):
        ordered_combos = [
            ('single', 4),
            ('single', 7),
            ('pair', 3),
            ('pair', 13),
            ('two-pair', 4),
            ('two-pair', 9),
            ('triple', 3),
            ('triple', 10),
            ('straight', 3),
            ('straight', 8),
            ('flush', 'D'),
            ('flush', 'S'),
            ('fullhouse', 3, 7),
            ('fullhouse', 4, 7),
            ('four-of-a-kind', 3),
            ('four-of-a-kind', 8),
            ('straight-flush', 'D', 5),
            ('straight-flush', 'D', 7),
            ('straight-flush', 'H', 3),
        ]

        num_combos = len(ordered_combos)
        for i in range(num_combos):
            c1 = ordered_combos[i]
            if i == num_combos:
                break
            for c2 in ordered_combos[i + 1:]:
                assert combos.greater_than(c2, c1)
                assert not combos.greater_than(c1, c2)
示例#2
0
    def test_sample(self):
        ordered_combos = [
            ('single', 4),
            ('single', 7),
            ('pair', 3),
            ('pair', 13),
            ('two-pair', 4),
            ('two-pair', 9),
            ('triple', 3),
            ('triple', 10),
            ('straight', 3),
            ('straight', 8),
            ('flush', 'D'),
            ('flush', 'S'),
            ('fullhouse', 3, 7),
            ('fullhouse', 4, 7),
            ('four-of-a-kind', 3),
            ('four-of-a-kind', 8),
            ('straight-flush', 'D', 5),
            ('straight-flush', 'D', 7),
            ('straight-flush', 'H', 3),
        ]

        num_combos = len(ordered_combos)
        for i in range(num_combos):
            c1 = ordered_combos[i]
            if i == num_combos:
                break
            for c2 in ordered_combos[i + 1:]:
                assert combos.greater_than(c2, c1)
                assert not combos.greater_than(c1, c2)
示例#3
0
    def update_state(self, player_id, update_json):
        """Updates game state based on player and update_json.

        update_json should contain:

            move
                This should be either 'challenge' or 'claim'.
            combo
                If the 'move' is 'combo', this should be the claimed combo.

        Returns:

            InvalidActionError
                If the update was invalid.
            None
                If the update was successful.
        """
        assert player_id == self._player_ordering[self._turn], 'Not your turn'
        update = json.loads(update_json)
        if update['move'] == 'challenge':
            if self._new_round:
                return InvalidActionError('No claim to challenge.')
            dealt_cards = []
            for p in self._players.values():
                dealt_cards.extend(p.hand)
            if combos.exists(dealt_cards, self._last_combo):
                losing_player_id = player_id
            else:
                losing_player_id = self._player_ordering[self._last_turn]
            losing_player = self._players[losing_player_id]
            losing_player.num_cards += 1
            if losing_player.num_cards > self._hand_limit:
                losing_player.lost = True
            self._deal()
            self._new_round = True
        elif update['move'] == 'claim':
            combo = update['combo']
            if not self._new_round and \
               not combos.greater_than(combo, self._last_combo):
                return InvalidActionError('Does not beat previous combo.')
            self._last_combo = combo
            self._new_round = False
        else:
            return InvalidActionError('Unknown action.')

        self._last_turn = self._turn
        num_players = len(self._players)
        for i in range(1, num_players):
            idx = (self._turn + i) % num_players
            if not self._players[self._player_ordering[idx]].lost:
                self._turn = idx
                break
示例#4
0
    def update_state(self, player_id, update_json):
        """Updates game state based on player and update_json.

        update_json should contain:

            move
                This should be either 'challenge' or 'claim'.
            combo
                If the 'move' is 'combo', this should be the claimed combo.

        Returns:

            InvalidActionError
                If the update was invalid.
            None
                If the update was successful.
        """
        assert player_id == self._player_ordering[self._turn], 'Not your turn'
        update = json.loads(update_json)
        if update['move'] == 'challenge':
            if self._new_round:
                return InvalidActionError('No claim to challenge.')
            dealt_cards = []
            for p in self._players.values():
                dealt_cards.extend(p.hand)
            if combos.exists(dealt_cards, self._last_combo):
                losing_player_id = player_id
            else:
                losing_player_id = self._player_ordering[self._last_turn]
            losing_player = self._players[losing_player_id]
            losing_player.num_cards += 1
            if losing_player.num_cards > self._hand_limit:
                losing_player.lost = True
            self._deal()
            self._new_round = True
        elif update['move'] == 'claim':
            combo = update['combo']
            if not self._new_round and \
               not combos.greater_than(combo, self._last_combo):
                return InvalidActionError('Does not beat previous combo.')
            self._last_combo = combo
            self._new_round = False
        else:
            return InvalidActionError('Unknown action.')

        self._last_turn = self._turn
        num_players = len(self._players)
        for i in range(1, num_players):
            idx = (self._turn + i) % num_players
            if not self._players[self._player_ordering[idx]].lost:
                self._turn = idx
                break