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())
Beispiel #2
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