Example #1
0
    def setUp(self):
        self.rf_1 = PokerHand(namelist=["AS", "KS", "QS", "JS", "TS"])
        self.rf_2 = PokerHand(namelist=["AC", "KC", "QC", "JC", "TC"])
        self.rf_false = PokerHand(namelist=["AC", "KC", "JC", "JC", "TC"])

        self.sf_ace_low = PokerHand(namelist=["AD", "2D", "3D", "4D", "5D"])
        self.sf_king_high = PokerHand(namelist=["9H", "KH", "QH", "JH", "TH"])
        self.sf_ten_high = PokerHand(namelist=["6D", "7D", "8D", "9D", "TD"])
        self.sf_false = PokerHand(namelist=["6D", "7D", "8D", "8D", "TD"])

        self.fk_king_ten = PokerHand(namelist=["KC", "KH", "KS", "KD", "TC"])
        self.fk_king_ace = PokerHand(namelist=["KC", "KH", "KS", "KD", "AC"])
        self.fk_nine_ten = PokerHand(namelist=["9C", "9H", "9S", "9D", "TC"])
        self.fk_nine_ace = PokerHand(namelist=["9C", "9H", "9S", "9D", "AC"])
        self.fk_nine_four = PokerHand(namelist=["9C", "9H", "9S", "9D", "4C"])
        self.fk_ace_ten = PokerHand(namelist=["AC", "AH", "AS", "AD", "TC"])
def main():

    """
    main() function.
    """

    hand_types = ["RF", "SF", "FK", "FH", "FL",
                  "ST", "TK", "TP", "PR", "HI"]
    types_found = {"RF": 0, "SF": 0, "FK": 0, "FH": 0,
                   "FL": 0, "ST": 0, "TK": 0, "TP": 0,
                   "PR": 0, "HI": 0}
    expected = {"RF": 4, "SF": 36, "FK": 624, "FH": 3744,
                "FL": 5108, "ST": 10200, "TK": 54912, "TP": 123552,
                "PR": 1098240, "HI": 1302540}
    total_expected = 2598960
    total_hands = 0
    percent = 0

    deck = Deck()
    cards = deck.draw(52)

    deck = Deck()
    hand = PokerHand(deck)

    # Loop through all hands and store number of hand types

    for a in range(48):
        for b in range(a + 1, 52):
            for c in range(b + 1, 52):
                for d in range(c + 1, 52):
                    for e in range(d + 1, 52):
                        cds = [cards[a], cards[b], cards[c],
                               cards[d], cards[e]]
                        hand._cards = cds
                        hand.evaluate()
                        types_found[hand.show_value(short=True)] += 1
                        total_hands += 1

                        # Print status indicator

                        if total_hands % 25990 == 0:
                            percent += 1
                            print "{0}%.....".format(percent)

    # Output results

    sumscores = 0
    failed = False
    for hand_type in hand_types:
        if expected[hand_type] == types_found[hand_type]:
            result = "passed"
        else:
            result = "failed"
            failed = True
        print "{0}: {1} expected, {2} found...{3}.".format(
            hand_type, expected[hand_type], types_found[hand_type], result)
        sumscores += types_found[hand_type]

    if total_expected == sumscores:
        result = "passed"
    else:
        result = "failed"
        failed = True
    print "Total hands: {0} expected, {1} found...{2}".format(
        total_expected, sumscores, result)

    if not failed:
        print("All tests passed.")
    else:
        print("SOME TESTS FAILED!")
Example #3
0
class TestSequenceFunctions(unittest.TestCase):

    """
    Test sequence class for poker hands.
    """

    def setUp(self):
        self.rf_1 = PokerHand(namelist=["AS", "KS", "QS", "JS", "TS"])
        self.rf_2 = PokerHand(namelist=["AC", "KC", "QC", "JC", "TC"])
        self.rf_false = PokerHand(namelist=["AC", "KC", "JC", "JC", "TC"])

        self.sf_ace_low = PokerHand(namelist=["AD", "2D", "3D", "4D", "5D"])
        self.sf_king_high = PokerHand(namelist=["9H", "KH", "QH", "JH", "TH"])
        self.sf_ten_high = PokerHand(namelist=["6D", "7D", "8D", "9D", "TD"])
        self.sf_false = PokerHand(namelist=["6D", "7D", "8D", "8D", "TD"])

        self.fk_king_ten = PokerHand(namelist=["KC", "KH", "KS", "KD", "TC"])
        self.fk_king_ace = PokerHand(namelist=["KC", "KH", "KS", "KD", "AC"])
        self.fk_nine_ten = PokerHand(namelist=["9C", "9H", "9S", "9D", "TC"])
        self.fk_nine_ace = PokerHand(namelist=["9C", "9H", "9S", "9D", "AC"])
        self.fk_nine_four = PokerHand(namelist=["9C", "9H", "9S", "9D", "4C"])
        self.fk_ace_ten = PokerHand(namelist=["AC", "AH", "AS", "AD", "TC"])

    def test_rf_evaluates(self):

        """
        Test a royal flush correctly evaluates
        """

        self.assertEqual(self.rf_1.show_value(short=True), "RF")

    def test_rf_false_not_evaluates(self):

        """
        Test a false royal flush does not evaluate to one, e.g.
        AC, KC, JC, JC, TC.
        """

        self.assertNotEqual(self.rf_false.show_value(short=True), "RF")

    def test_rf_false_evaluates_flush(self):

        """
        Test a false royal flush evaluates to a flush, e.g.
        AC, KC, JC, JC, TC.
        """

        self.assertEqual(self.rf_false.show_value(short=True), "FL")

    def test_sf_evaluates(self):

        """
        Test a straight flush correctly evaluates
        """

        self.assertEqual(self.sf_ace_low.show_value(short=True), "SF")

    def test_sf_false_not_evaluates(self):

        """
        Test a false straight flush does not evaluate to one, e.g.
        AC, 2C, 3C, 3C, 4C.
        """

        self.assertNotEqual(self.sf_false.show_value(short=True), "SF")

    def test_sf_false_evaluates_flush(self):

        """
        Test a false straight flush evaluates to a flush, e.g.
        AC, 2C, 3C, 3C, 4C.
        """

        self.assertEqual(self.sf_false.show_value(short=True), "FL")

    def test_rf_beats_sf(self):

        """
        Test a royal flush beats a self flush, tests > operator
        """

        self.assertTrue(self.rf_2 > self.sf_king_high)

    def test_sf_loses_to_rf(self):

        """
        Test a self flush loses to a royal flush, tests < operator
        """

        self.assertTrue(self.sf_ten_high < self.rf_1)

    def test_rf_le_rf(self):

        """
        Test a royal flush is <= another royal flush, i.e. that
        similar hands are <= each other.
        """

        self.assertTrue(self.rf_1 <= self.rf_2)

    def test_rf_ge_rf(self):

        """
        Test a royal flush is >= another royal flush, i.e. that
        similar hands are >= each other.
        """

        self.assertTrue(self.rf_1 >= self.rf_2)

    def test_sf_le_rf(self):

        """
        Test a straight flush is <= a royal flush, i.e. that dissimilar
        hands are <= each other with the lower hand on the left.
        """

        self.assertTrue(self.sf_king_high <= self.rf_2)

    def test_rf_ge_sf(self):

        """
        Test a royal flush is >= a straight flush, i.e. that dissimilar
        hands are >= each other with the lower hand on the right.
        """

        self.assertTrue(self.rf_1 >= self.sf_ace_low)

    def test_rf_eq_rf(self):

        """
        Test a royal flush is equal to another royal flush, i.e.
        tests the == operator.
        """

        self.assertTrue(self.rf_1 == self.rf_2)

    def test_rf_ne_sf(self):

        """
        Test a royal flush is not equal to a straight flush, i.e.
        tests the != operator.
        """

        self.assertTrue(self.rf_1 != self.sf_ten_high)

    def test_sfk_beats_sfa(self):

        """
        Test a straight flush with king high beats a straight
        flush ace low, i.e. ensure that the presence of the ace
        does not cause a bad comparison due to it being called
        a "high card".
        """

        self.assertTrue(self.sf_king_high > self.sf_ace_low)

    def test_sfk_beats_sft(self):

        """
        Test a straight flush with king high beats a straight
        flush ten high.
        """

        self.assertTrue(self.sf_king_high > self.sf_ten_high)

    def test_sf_beats_fk(self):

        """
        Test a straight flush beats a four of a kind
        """

        self.assertTrue(self.sf_king_high > self.fk_ace_ten)

    def test_fk_king_beats_fk_nine(self):

        """
        Test four kings beat four nines
        """

        self.assertTrue(self.fk_king_ten > self.fk_nine_ace)

    def test_fk_ace_beats_fk_king(self):

        """
        Test four aces beat four kings
        """

        self.assertTrue(self.fk_ace_ten > self.fk_king_ace)

    def test_fk_king_ace_beats_fk_king_ten(self):

        """
        Test four kings with an ace beats four kings with a ten
        """

        self.assertTrue(self.fk_king_ace > self.fk_king_ten)

    def test_fk_nine_ten_beats_fk_nine_four(self):

        """
        Test four nines with a ten beat four nines with a four
        """

        self.assertTrue(self.fk_nine_ten > self.fk_nine_four)