示例#1
0
    def test_two_pair(self):
        cards = [Card(13, 'S'), Card(13, 'D'), Card(13, 'C'),
                 Card(2, 'H'), Card(2, 'C'),
                 Card(3, 'H')]
        assert combos.exists(cards, ('two-pair', 13, 2))
        assert not combos.exists(cards, ('two-pair', 13, 3))
        assert not combos.exists(cards, ('two-pair', 6, 2))

        with self.assertRaises(combos.IllegalCombo):
            assert combos.exists(cards, ('two-pair', 13, 13))
示例#2
0
 def test_triple(self):
     cards = [
         Card(13, 'S'),
         Card(2, 'H'),
         Card(2, 'D'),
         Card(13, 'D'),
         Card(13, 'C')
     ]
     assert combos.exists(cards, ('triple', 13))
     assert not combos.exists(cards, ('triple', 2))
     assert not combos.exists(cards, ('triple', 6))
示例#3
0
    def test_two_pair(self):
        cards = [
            Card(13, 'S'),
            Card(13, 'D'),
            Card(13, 'C'),
            Card(2, 'H'),
            Card(2, 'C'),
            Card(3, 'H')
        ]
        assert combos.exists(cards, ('two-pair', 13, 2))
        assert not combos.exists(cards, ('two-pair', 13, 3))
        assert not combos.exists(cards, ('two-pair', 6, 2))

        with self.assertRaises(combos.IllegalCombo):
            assert combos.exists(cards, ('two-pair', 13, 13))
示例#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
示例#5
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
示例#6
0
 def test_single(self):
     cards = [Card(13, 'S'), Card(2, 'H')]
     assert combos.exists(cards, ('single', 13))
     assert not combos.exists(cards, ('single', 6))
示例#7
0
 def test_unknown_combo(self):
     cards = [Card(13, 'S'), Card(13, 'D')]
     with self.assertRaises(combos.IllegalCombo):
         assert combos.exists(cards, ('blah', 4))
示例#8
0
 def test_triple(self):
     cards = [Card(13, 'S'), Card(2, 'H'), Card(2, 'D'),
              Card(13, 'D'), Card(13, 'C')]
     assert combos.exists(cards, ('triple', 13))
     assert not combos.exists(cards, ('triple', 2))
     assert not combos.exists(cards, ('triple', 6))
示例#9
0
 def test_pair(self):
     cards = [Card(13, 'S'), Card(2, 'H'), Card(13, 'D')]
     assert combos.exists(cards, ('pair', 13))
     assert not combos.exists(cards, ('pair', 2))
     assert not combos.exists(cards, ('pair', 6))
示例#10
0
 def test_single(self):
     cards = [Card(13, 'S'), Card(2, 'H')]
     assert combos.exists(cards, ('single', 13))
     assert not combos.exists(cards, ('single', 6))
示例#11
0
 def test_unknown_combo(self):
     cards = [Card(13, 'S'), Card(13, 'D')]
     with self.assertRaises(combos.IllegalCombo):
         assert combos.exists(cards, ('blah', 4))
示例#12
0
 def test_pair(self):
     cards = [Card(13, 'S'), Card(2, 'H'), Card(13, 'D')]
     assert combos.exists(cards, ('pair', 13))
     assert not combos.exists(cards, ('pair', 2))
     assert not combos.exists(cards, ('pair', 6))