def test_card_count(): deck = Deck() deck.set_cards([ Cards.TEN, Cards.QUEEN, Cards.KING, Cards.TEN, Cards.KING, Cards.ACE, Cards.TEN, Cards.QUEEN, Cards.QUEEN, Cards.JACK, Cards.TEN, ]) for i in range(11): deck.deal() assert deck.get_count() == -11 cards = [ Cards.FOUR, Cards.FIVE, Cards.SIX, Cards.SEVEN, Cards.EIGHT, Cards.TEN, Cards.ACE, ] deck = Deck() deck.set_cards(cards) for i in range(len(cards)): deck.deal() assert deck.get_count() == 1
def test_deck_basic_functions(): deck = Deck() assert deck.get_cards_left() == 52 deck = Deck(2) assert deck.get_cards_left() == 52 * 2 deck = Deck(6) assert deck.get_cards_left() == 52 * 6 deck.deal() assert deck.get_cards_left() == 52 * 6 - 1
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))
def command_line_interface(): """Defines the CLI (Command Line Interface) of the Game""" deck = Deck() playerHand = Hand() dealerHand = Hand(dealer=True) playing = True num_cards_to_draw = 2 try: while playing: for _ in range(num_cards_to_draw): playerHand.addCard(deck.deal()) dealerHand.addCard(deck.deal()) num_cards_to_draw = 1 # after 1st iter, draw single card each time "stick" print( f"Player Cards [{playerHand.getValue}] :\n{playerHand.showHand()}" ) print( f"Dealer Cards [{dealerHand.getValue}] :\n{dealerHand.showHand()}" ) # check if black jack obtained for p, msg in zip([playerHand, dealerHand], ["Player", "Dealer"]): if p.is_blackjack: print(f"{msg} WINS!") playing = False raise GameComplete wager = input( "Choose [Hit/Stick] : ").lower() # should draw more cards? while wager not in ["h", "s", "hit", "stick"]: wager = input( "Please Enter 'Hit' or 'Stick' (or H/S) : ").lower() if wager not in ["h", "hit"]: playing = False # find the winning player print(get_winning_player(playerHand.getValue, dealerHand.getValue)) except GameComplete: pass
from blackjack import Deck, Card, Player if __name__ == '__main__': deck = Deck() dealer = Player("Dealer") patron = Player("Patron") patron.add_cards(deck.deal(2)) dealer.add_cards(deck.deal(2)) game_on = True round_num = 1 player_turn = True while game_on: if patron.total == 21: print("---------- BLACKJACK! ----------") game_on = False break if player_turn: print(f"---------- ROUND {round_num} ----------") patron.print_total() choice = input("Hit or Stay: ") if choice.lower() == "hit": patron.add_cards(deck.deal(1)) elif choice.lower() == "stay": player_turn = False
def test_true_count_and_wager(): wager_amts = [1, 1, 1, 1, 1, 1, 4, 8, 16] ranges = [-3, -2, -1, 0, 0, 1, 2, 3] betting_policy = (wager_amts, ranges) player = Player( bankroll=100, hard_policy=hard_policy, soft_policy=soft_policy, split_policy=split_policy, betting_policy=betting_policy, ) deck = Deck() deck.set_cards([ Cards.TEN, Cards.QUEEN, Cards.KING, Cards.TEN, Cards.KING, Cards.ACE, Cards.TEN, Cards.QUEEN, Cards.QUEEN, Cards.JACK, Cards.TEN, ]) assert deck.get_num_decks() == 1 assert deck.get_cards_left() == 11 for i in range(10): deck.deal() assert deck.get_true_count() == -10 / (1 / 52) assert player.calculate_wager(deck.get_true_count()) == 1 deck = Deck() deck.set_cards( [Cards.FOUR, Cards.TWO, Cards.SEVEN, Cards.TEN, Cards.NINE, Cards.TWO]) for i in range(5): deck.deal() assert deck.get_true_count() == 1 / (1 / 52) assert player.calculate_wager(deck.get_true_count()) == 16 deck = Deck() deck.set_cards( [Cards.FOUR, Cards.TWO, Cards.SEVEN, Cards.TEN, Cards.NINE, Cards.TWO ] * 100) for i in range(6 * 50): deck.deal() assert deck.get_true_count() == (2 * 50) / ((600 - 6 * 50) / 52) assert player.calculate_wager(deck.get_true_count()) == 16 deck = Deck() deck.set_cards([ Cards.TWO, Cards.THREE, Cards.FOUR, Cards.FIVE, Cards.SIX, Cards.SEVEN, Cards.EIGHT, Cards.NINE, Cards.TEN, Cards.JACK, Cards.QUEEN, Cards.KING, Cards.ACE, ] * 100) for i in range(13 * 69): deck.deal() assert deck.get_true_count() == 0 assert player.calculate_wager(deck.get_true_count()) == 1 deck = Deck() deck.set_cards([Cards.FOUR, Cards.TWO, Cards.FOUR, Cards.FOUR] * 13) # +1 deck.deal() assert deck.get_true_count() == 1 / (51 / 52) assert player.calculate_wager(deck.get_true_count()) == 4 # +2 deck.deal() assert deck.get_true_count() == 2 / (50 / 52) assert player.calculate_wager(deck.get_true_count()) == 8 # +3 deck.deal() assert deck.get_true_count() == 3 / (49 / 52) assert player.calculate_wager(deck.get_true_count()) == 16 deck = Deck() deck.set_cards([Cards.QUEEN, Cards.JACK, Cards.ACE, Cards.TEN] * 13) wager_amts = [-16, -8, -4, -1, 0, 1, 4, 8, 16] ranges = [-3, -2, -1, 0, 0, 1, 2, 3] betting_policy_alt = (wager_amts, ranges) player = Player( bankroll=100, hard_policy=hard_policy, soft_policy=soft_policy, split_policy=split_policy, betting_policy=betting_policy_alt, ) assert player.calculate_wager(deck.get_true_count()) == 0 # +1 deck.deal() assert deck.get_true_count() == -1 / (51 / 52) assert player.calculate_wager(deck.get_true_count()) == -4 # +2 deck.deal() assert deck.get_true_count() == -2 / (50 / 52) assert player.calculate_wager(deck.get_true_count()) == -8 # +3 deck.deal() assert deck.get_true_count() == -3 / (49 / 52) assert player.calculate_wager(deck.get_true_count()) == -16
''' GAME LOOP ''' while True: print("WELLCOME TO BLACKJACK") #create and shuffle the deck, deal two cards to each player deck = Deck() deck.shuffle() player_hand = Hand() player_hand.add_card(deck.deal()) player_hand.add_card(deck.deal()) dealer_hand = Hand() dealer_hand.add_card(deck.deal()) dealer_hand.add_card(deck.deal()) # Set up plauer's chips player_chips = Chips() # prompt the player for their bet take_bet(player_chips) # show cards (but keep one dealer card hidden) show_some(player_hand, dealer_hand)