示例#1
0
文件: hand.py 项目: mtrevor/pokercore
    def from_chars(cls, *args):
        """return a new object from pairs of character symbols

        works with either multiple arguments, or a single iterable
        """

        try:
            return cls(Card.from_chars(chars) for chars in args)
        except CardCreationError:
            try:
                return cls(Card.from_chars(chars) for chars in args[0])
            except (TypeError, CardCreationError):
                pass
            raise HandCreationError('invalid card symbols')
示例#2
0
    def test_create(self):
        self.assertRaises(TypeError, Hand, Card(1, 2))
        self.assertRaises(HandCreationError, Hand, [])
        self.assertRaises(HandCreationError, Hand, [3, 1])

        self.assertRaises(HandCreationError, Hand.from_chars)
        self.assertRaises(HandCreationError, Hand.from_chars, 4)
        self.assertRaises(HandCreationError, Hand.from_chars, 'aS', '4p')
        self.assertRaises(HandCreationError, Hand.from_chars, ['Th', 9])
        self.assertRaises(HandCreationError, Hand.from_chars,
                          'KH', 'Qs', 'Acd')

        hand = Hand([Card(4, 2), Card.from_chars('AS'), Card(9, 0)])
        self.assertIsInstance(hand, Hand)
        self.assertEqual(hand.value, 0)
        self.assertEqual(Hand.names[hand.value], 'high card')
        self.assertEqual(hand.best_cards, [12, 9, 4])

        hand = Hand.from_chars('4s', 'Th', '3s', '3c', 'Js', '3d')
        self.assertIsInstance(hand, Hand)
        self.assertEqual(hand.value, 3)
        self.assertEqual(Hand.names[hand.value], 'three of a kind')
        self.assertEqual(hand.best_cards, [1, 1, 1, 9, 8])

        hand = Hand.from_chars(['KH', '9H', 'KS', '9C'])
        self.assertIsInstance(hand, Hand)
        self.assertEqual(hand.value, 2)
        self.assertEqual(Hand.names[hand.value], 'two pair')
        self.assertEqual(hand.best_cards, [11, 11, 7, 7])
示例#3
0
    def test_compare(self):
        first = Card(9, 2)
        self.assertRaises(CardComparisonError, lambda c: c > 'A', first)
        self.assertRaises(CardComparisonError, lambda c: 't' <= c, first)
        self.assertRaises(CardComparisonError, lambda c: c == '9', first)
        self.assertRaises(CardComparisonError, lambda c: c != 'j', first)
        self.assertRaises(CardComparisonError, first.identical_to, 9)

        second = Card(11, 1)
        self.assertNotEqual(first, second)
        self.assertLess(first, second)
        self.assertLessEqual(first, second)
        self.assertGreater(second, first)
        self.assertGreaterEqual(second, first)
        self.assertFalse(first.identical_to(second))

        second = Card.from_chars('js')
        self.assertEqual(first, second)
        self.assertFalse(second.identical_to(first))

        second = Card.from_chars('Jh')
        self.assertEqual(second, first)
        self.assertTrue(first.identical_to(second))

        self.assertEqual(first, 9)
        self.assertLess(first, 11)
        self.assertLessEqual(first, 12)
        self.assertGreater(first, 2)
        self.assertGreaterEqual(first, 9)
示例#4
0
    def test_compare(self):
        hand = Hand([Card.from_chars('Qc'), Card(1, 3)])
        self.assertRaises(HandComparisonError, lambda h: h < Card(5, 2), hand)
        self.assertRaises(HandComparisonError, lambda h: h >= 0, hand)
        self.assertRaises(HandComparisonError, lambda h: h != 'Qc', hand)
        self.assertRaises(HandComparisonError, lambda h: h == 10, hand)
        self.assertEqual(hand, hand)

        for i in xrange(len(self.players[0])):
            first = self.players[0][i][0]
            second = self.players[1][i][0]
            self.assertEqual(cmp(first, second), self.wins[i])
示例#5
0
    def test_create(self):
        self.assertRaises(CardCreationError, Card, '3', 2)
        self.assertRaises(CardCreationError, Card, 9, 'H')
        self.assertRaises(CardCreationError, Card, 3, 5)
        self.assertRaises(CardCreationError, Card, 13, 0)
        self.assertRaises(CardCreationError, Card.from_chars, '51')
        self.assertRaises(CardCreationError, Card.from_chars, '10S')

        card = Card(3, 2)
        self.assertIsInstance(card, Card)
        self.assertEqual(card.rank, 3)
        self.assertEqual(card.suit, 2)

        card = Card.from_chars('TC')
        self.assertIsInstance(card, Card)
        self.assertEqual(card.rank, 8)
        self.assertEqual(card.suit, 0)
示例#6
0
    def test_arithmetic(self):
        first = Card.from_chars('8D')
        self.assertRaises(TypeError, lambda c: c * 4, first)
        self.assertRaises(TypeError, lambda c: 1 * c, first)
        self.assertRaises(TypeError, lambda c: c / 2, first)
        self.assertRaises(TypeError, lambda c: 8 / c, first)
        self.assertRaises(CardArithmeticError, lambda c: c + '3', first)
        self.assertRaises(CardArithmeticError, lambda c: c - '1', first)

        self.assertEqual(first + 3, 9)
        self.assertEqual(2 + first, 8)
        self.assertEqual(first - 4, 2)
        self.assertEqual(10 - first, 4)

        second = Card(2, 2)
        self.assertRaises(TypeError, lambda c: c * second, first)
        self.assertRaises(TypeError, lambda c: second / c, first)
        self.assertEqual(first + second, 8)
        self.assertEqual(first - second, 4)
        self.assertEqual(second + 3 - first, -1)
        self.assertEqual(10 - second + first, 14)