def test_add_to_poker_hand():
    try:
        a_hand = PokerHand()
        a_hand.add_card(Card('S', 11))
        a_hand.add_card(Card('S', 10))
        assert_equals("Making sure that PokerHand object has add_card method.",
                      True,
                      type(a_hand) is PokerHand)
    except:
        fail_on_error("Trying to create a PokerHand object and add cards to it.",
                      sys.exc_info())
def test_str():
    try:
        a_hand = PokerHand()
        a_hand.add_card(Card('S', 11))
        a_hand.add_card(Card('S', 10))
        a_hand_string = str(a_hand)
        assert_in("Added card 'S11'. String representation of hand should contain 'Jack of Spades'.",
                  "Jack of Spades",
                  a_hand_string)
        assert_in("Added card 'S10'. String representation of hand should contain '10 of Spades'.",
                  "10 of Spades",
                  a_hand_string)
    except:
        fail_on_error("Runtime error when calling __str__ of PokerHand object.", sys.exc_info())
 def __get_all_five_card_hands(self):
     """Determines all possible 5-card PokerHand objects from combined stud and community
     cards and returns them"""
     for i in range(0, len(self.community_card_set.community_cards)):
         card = self.community_card_set.get_card(i)
         self.add_card(card)
     five_card_hands = combinations(self.stud_hand, 5)
     a_list = []
     for combination in five_card_hands:
         hand = PokerHand()
         for i in range(0, len(combination)):
             card = combination[i]
             hand.add_card(card)
         a_list.append(hand)
     return a_list
def test_compare_to_safe(msg, cards_a, cards_b, expected):
    try:
        hand_a = PokerHand()
        for s, v in cards_a:
            hand_a.add_card(Card(s, v))
        hand_b = PokerHand()
        for s, v in cards_b:
            hand_b.add_card(Card(s, v))
        msg += "\nA: {}\nB: {}".format(str(cards_a), str(cards_b))
        if expected > 0:
            assert_equals(msg, True, hand_a.compare_to(hand_b) > 0)
        elif expected < 0:
            assert_equals(msg, True, hand_a.compare_to(hand_b) < 0)
        else:
            assert_equals(msg, 0, hand_a.compare_to(hand_b))
    except:
        fail_on_error(msg, sys.exc_info())
Ejemplo n.º 5
0
def play_1(player):
    """User decides whether Hand #1 or Hand #2 is worth more. If user answer matches
        the correct answer given by compare_to function, user gains 1 point; if user answers
        wrong or deck runs out of cards, game ends and score is printed."""
    playing = True
    deck = Deck()
    deck.shuffle()
    score = 0
    print(
        input("Welcome to the casino, " + player +
              ". Press Enter to play the game."))
    while playing:
        if deck.out():
            print("Deck is out of cards. Game over. Your score was ",
                  str(score))
            playing = False
        else:
            hand_a = PokerHand()
            hand_b = PokerHand()
            for i in range(0, MAX_HAND_SIZE):
                card = deck.deal()
                hand_a.add_card(card)
            for i in range(0, MAX_HAND_SIZE):
                card = deck.deal()
                hand_b.add_card(card)
            print("Hand #1: ", hand_a)
            print("Hand #2: ", hand_b, "\n")
            user_answer = int(
                input(
                    "Which hand is worth more? "
                    "Type 1 if Hand #1, -1 if Hand #2, and 0 if they are worth the same\n "
                ))
            correct_answer = hand_a.compare_to(hand_b)
            if user_answer == correct_answer:
                print("Correct! Let's try again\n")
                score += 1
            else:
                print("Game over. Your score was ", str(score))
                playing = False