Esempio n. 1
0
def test_MultiplePlayerDrawCard():
    """
    Test that multiple players can draw cards from a deck to their hand. Ensure the card is present in the player's hand
    but not present in the deck after being drawn or in the other player's hand.
    """

    d = Deck()
    d.shuffle()
    p1 = Player("Matt")
    p2 = Player("Sarah")

    for i in range(2):
        p1.draw(d)
        p2.draw(d)

    assert len(p1.hand) > 0
    assert len(p2.hand) > 0

    for card in p1.hand:
        assert card not in d.cards
        assert card not in p2.hand

    for card in p2.hand:
        assert card not in d.cards
        assert card not in p1.hand

    return
Esempio n. 2
0
def test_can_take_card_from_deck():
    deck = Deck()
    deck.shuffle()

    card = deck.take_card()
    assert type(card) is Card
    assert card not in deck.cards
Esempio n. 3
0
 def test_shuffle(self):
     """
     Make sure Deck.shuffle is randomized
     """
     deck1 = Deck(num_decks=1)
     deck2 = Deck(num_decks=1)
     deck1.shuffle()
     deck2.shuffle()
     self.assertNotEqual(deck1, deck2)
Esempio n. 4
0
    def test_determine_winners(self):
        """
        Test that the dealer hits at the appropriate times and
        that winning hands are appropriated updated as such.
        """

        # setup deck
        deck = Deck()
        deck.create()
        deck.shuffle()

        # setup gambler, dealer & hands
        dealer = Dealer()
        dealer_hand = Hand()
        dealer.hands.append(dealer_hand)
        gambler = Gambler("Test")
        gambler_hand = Hand()
        gambler.hands.append(gambler_hand)
        players = [gambler, dealer]

        # must hit a soft 17 (A, 6)
        gambler_hand.final_value = 20
        dealer_hand.cards.append(Card('Hearts', 'Ace', 1))
        dealer_hand.cards.append(Card('Hearts', 'Six', 6))
        bj.determine_winners(players, deck)
        self.assertTrue(len(dealer_hand.cards) > 2)

        # must hit on 16 or less (K, 6)
        self.reset_hand_attrs(dealer_hand)
        dealer_hand.cards.append(Card('Hearts', 'King', 10))
        dealer_hand.cards.append(Card('Hearts', 'Six', 6))
        bj.determine_winners(players, deck)
        self.assertTrue(len(dealer_hand.cards) > 2)

        # check dealer bust (K, 6, J)
        dealer_hand.cards.pop()
        dealer_hand.cards.append(Card('Hearts', 'Jack', 10))
        bj.determine_winners(players, deck)
        self.assertTrue(dealer_hand.busted)
        self.assertTrue(gambler_hand.win)

        # check dealer stands on 17 (K, 7)
        self.reset_hand_attrs(dealer_hand)
        dealer_hand.cards.append(Card('Hearts', 'King', 10))
        dealer_hand.cards.append(Card('Hearts', 'Seven', 7))
        bj.determine_winners(players, deck)
        self.assertTrue(len(dealer_hand.cards) == 2)

        # gambler wins with 20 to dealer's 17
        self.assertTrue(gambler_hand.win)

        # check dealer stands on soft 18 (Ace, 7)
        self.reset_hand_attrs(dealer_hand)
        dealer_hand.cards.append(Card('Hearts', 'Ace', 1))
        dealer_hand.cards.append(Card('Hearts', 'Seven', 7))
        bj.determine_winners(players, deck)
        self.assertTrue(len(dealer_hand.cards) == 2)
Esempio n. 5
0
class TestHand(unittest.TestCase):
    def test_deal_card(self):
        """
        Test that dealing a card removes 1 card
        from the deck and adds 1 card to the hand.
        """
        self.deck = Deck(num_decks=1)
        self.deck.create()
        self.deck.shuffle()
        self.hand = Hand()
        self.hand.deal_card(self.deck)
        self.assertEqual(len(self.deck.cards), 51)
        self.assertEqual(len(self.hand.cards), 1)

    def test_check_busted(self):
        """
        Check to see if a hand is a bust (> 21) or not.
        """

        # hand is busted
        self.hand = Hand()
        self.hand.cards.append(Card('Hearts', 'King', 10))
        self.hand.cards.append(Card('Hearts', 'Three', 3))
        self.hand.cards.append(Card('Hearts', 'Jack', 10))
        self.hand.check_busted()
        self.assertTrue(self.hand.busted)

        # hand is not busted
        self.hand = Hand()
        self.hand.cards.append(Card('Hearts', 'King', 10))
        self.hand.cards.append(Card('Hearts', 'Three', 3))
        self.hand.cards.append(Card('Hearts', 'Six', 6))
        self.hand.check_busted()
        self.assertFalse(self.hand.busted)

    def test_get_hand_value(self):
        """
        Make sure get_hand_value() returns accurate hand values.
        """

        # test hand values with Aces
        self.hand = Hand()
        self.hand.cards.append(Card('Hearts', 'Ace', 1))
        self.hand.cards.append(Card('Spades', 'Ace', 1))
        self.hand.cards.append(Card('Hearts', 'Six', 6))
        hand_values = self.hand.get_hand_value()
        self.assertEqual(hand_values, [8, 18])

        # test hand values with hidden card (Dealer)
        self.hand.cards[2].hidden = True
        hand_values = self.hand.get_hand_value()
        self.assertEqual(hand_values, [2, 12])

        # test hand values with hidden card included (Dealer)
        hand_values = self.hand.get_hand_value(include_hidden=True)
        self.assertEqual(hand_values, [8, 18])
Esempio n. 6
0
def test_hand_value():
    deck = Deck()
    deck.shuffle()
    hand = Hand(deck)
    assert isinstance(hand.cards[0], Card)
    assert isinstance(hand.cards[1], Card)

    hand.value = 20
    hand.deck.deal_card = MagicMock(return_value=Card('spade', 'A'))
    hand.get_card()
    assert hand.value == 21
Esempio n. 7
0
def test_deal_hands():
    deck = Deck()

    assert isinstance(deck.deal_card(), Card)

    deck = Deck()
    deck.shuffle()
    card1, card2 = deck.deal_hand()

    assert isinstance(card1, Card)
    assert isinstance(card2, Card)
    assert card1.value != card2.value or card1.suit != card2.suit
Esempio n. 8
0
def test_Shuffle():
    """
    Test that a deck of cards is shuffled correctly.
    """

    d = Deck()
    # store deck before shuffling for comparison
    cardsBeforeShuffle = str(d.cards)
    d.shuffle()

    assert str(d.cards) != cardsBeforeShuffle

    return
Esempio n. 9
0
    def test_getCard(self):
        """Getting the next card should produces cards in sequencial index order""" 
        deck = Deck(5)

        deck.shuffle()
        a = []

        for i in range(len(deck._Deck__deck)):
            card = deck.getCard()
            a.append(card)

        for i in range(len(deck._Deck__deck)):
            assert(a[i] == deck._Deck__deck[i])
Esempio n. 10
0
def test_DrawCard():
    """
    Test that a card is drawn from a deck correctly.
    """

    d = Deck()
    d.shuffle()

    c = d.drawCard()

    assert isinstance(c, Card) == True
    assert c not in d.cards

    return
Esempio n. 11
0
def test_shuffle_deck():
    deck_a = Deck()
    deck_b = Deck()

    for c_a, c_b in zip(deck_a.cards, deck_b.cards):
        assert c_a.value == c_b.value

    deck_b.shuffle()
    randomness = []

    for c_a, c_b in zip(deck_a.cards, deck_b.cards):
        randomness.append(c_a.value != c_b.value)

    assert any(randomness)
Esempio n. 12
0
class deck_shuffleTest(unittest.TestCase):

    def setUp(self):
        """Call before every test case."""
        self.deck = Deck()

    def testAddCard(self):
        assert self.deck.shuffle() == dummy(self), 'Deck.shuffle() does not provide the right return value'
Esempio n. 13
0
def test_shuffle_deck():

    deck_a = Deck()
    deck_b = Deck()

    for c_a, c_b in zip(deck_a.cards, deck_b.cards):
        assert c_a.value == c_b.value

    # OK the decks are identical
    # now let's shuffle one of the decks

    deck_b.shuffle()
    randomness = []

    for c_a, c_b in zip(deck_a.cards, deck_b.cards):
        randomness.append(c_a.value != c_b.value)

    assert any(randomness)
Esempio n. 14
0
    def test_createDeckShuffled(self):
        """Cards in a shuffled deck should be in a different order than it was previously """
        a1 = [] 
        a2 = []
        deck = Deck(5)
        for card in deck._Deck__deck:
            a1.append(card)
        deck.shuffle()

        for card in deck._Deck__deck:
            a2.append(card)

        different = False
        for i in range(len(a1)):
            if a1[i] != a2[i]:
                different = True

        assert(different == True)
Esempio n. 15
0
def test_SinglePlayerDrawCard():
    """
    Test that a single player can draw a card from a deck to their hand. Ensure the card is present in the player's hand
    but not present in the deck after being drawn.
    """

    d = Deck()
    d.shuffle()
    p1 = Player("Matt")

    for i in range(2):
        p1.draw(d)

    assert p1.hand != None
    assert len(p1.hand) > 0

    for card in p1.hand:
        assert card not in d.cards

    return
Esempio n. 16
0
    def test_compareShuffledDecks(self):
        """The same Deck shuffled at different times should have different ordering  """
        a1 = [] 
        a2 = []
        deck = Deck(5)
        deck2 = copy.deepcopy(deck)

        deck.shuffle()
        deck2.shuffle()

        for card in deck._Deck__deck:
            a1.append(card)

        for card in deck2._Deck__deck:
            a2.append(card)

        different = False
        for i in range(len(a1)):
            if a1[i] != a2[i]:
                different = True
Esempio n. 17
0
def test_deal_hand():
    deck = Deck()

    assert isinstance(deck.deal_card(), Card)

    card1, card2 = deck.deal_hand()

    assert isinstance(card1, Card)
    assert isinstance(card2, Card)

    assert card1 != card2

    deck = Deck()
    deck.shuffle()
    card1, card2 = deck.deal_hand()

    assert card1 != card2

    card1, card2 = deck.deal_card(), deck.deal_card()

    assert card1 != card2
Esempio n. 18
0
def start_game(room):
    deck = Deck()
    deck.shuffle()
    print('starting game for room ' + room)

    starter = -1

    club = Card(Card.CLUBS, 2)
    print(rooms[room])
    for i in range(0, len(rooms[room]['players'])):
        rooms[room]['players'][i]['player'] = Player(
            rooms[room]['players'][i]['user'])
        player_ = rooms[room]['players'][i]
        player_['player'].draw_n(deck, 13)
        if player_['player'].has(club):
            starter = i

        emit('hand', player_['player'].serialize(), room=player_['sid'])

    print('starting player is ' + str(starter))
    rooms[room]['starter'] = starter

    emit('declare', True, room=rooms[room]['players'][0]['sid'])
    rooms[room]['declaring'] = 0
Esempio n. 19
0
class TestDeck(unittest.TestCase):

    def setUp(self):
        self.deck = Deck(shuffle_func)

    def test_cards(self):
        self.assertEqual(52, len(self.deck.cards))

    def test_shuffle(self):
        cards_before = list(self.deck.cards)
        self.deck.shuffle()
        cards_after = list(self.deck.cards)

        self.assertNotEqual(cards_before, cards_after)

    def test_deal(self):
        expected = self.deck.cards[0]
        card = self.deck.deal()

        self.assertEqual(expected, card)

    def test_deal_card_gone(self):
        card = self.deck.deal()
        self.assertEqual(51, len(self.deck.cards))
Esempio n. 20
0
def game_play():

    print('Welcome to Blackjack!')
    print()
    playing = True
    player_chips = Chips()

    while playing:
        player = Hand()
        computer = Hand()

        print(f'You have {player_chips.balance} chips')
        amount = bet(player_chips)
        if amount == 0:
            return

        game_deck = Deck()
        game_deck.shuffle()

        print()
        print('PLAYER CARDS:')
        player.play_one(game_deck.deal_one())
        player.play_one(game_deck.deal_one())
        print(f'Total hand value: {player.hand_value}')
        print()

        print('COMPUTER CARDS:')
        computer.play_one(game_deck.deal_one())
        computer.play_one_hidden(game_deck.deal_one())
        print('***Hidden card***')
        print('Total computer hand value: unknown')
        print()

        game_on = True
        player_turn = True

        while game_on:
            if win_check(game_on, player, computer, player_chips,
                         amount) == False:
                break

            while player_turn:
                if win_check(player_turn, player, computer, player_chips,
                             amount) == False:
                    break

                choice = player_choice()
                print()
                if choice == 'hit':
                    player.play_one(game_deck.deal_one())
                    print(f'Total hand value: {player.hand_value}')
                else:
                    break
            game_on = False

        if player.hand_value < 21:
            comp_turn = True

            while comp_turn:
                if computer.hand_value < 16:
                    print('COMPUTER TURN:')
                    computer.play_one(game_deck.deal_one())
                    continue
                else:
                    print(f'Total computer hand value: {computer.hand_value}')
                    comp_win_check(comp_turn, player, computer, player_chips,
                                   amount)
                    break

        if player_chips.balance == 0:
            playing = False
            print('Game Ended: You have run out of chips!')
            break
        elif play_again() == False:
            playing = False
            print('Game Ended: Thank you for playing!')
            break
        else:
            continue
    pass
Esempio n. 21
0
from blackjack import Deck, Player, Blackjack

#The classes are instantiated
deck = Deck()
deck.shuffle()
player = Player("Mikkel")
dealer = Player("dealer")
bj = Blackjack(player, dealer, deck)


while bj.game_running == True:                                          #A loop that keeps the game running untill the player decides he wants to quit
    bj.run_game()                                                       #The game is started
    if bj.player.cash == 0:
        print("Your balance is 0 and you will have to rebuy to play again.")
    if input("Play again? Press any key to play or press q to quit! ") == "q":   #If the player presses "q" the game stops - if not the game starts again  
        quit()
    else:                                                               #The else block resets the game
        bj.game_running = True                                          
        player.hand = []
        dealer.hand = []
Esempio n. 22
0
def get_deck():
    """Return a shuffled deck object."""
    deck = Deck()
    deck.shuffle()
    return deck