MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / main

Function main

src/gamesbyexample/blackjack.py:19–120  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

17
18
19def main():
20 print('''Blackjack, by Al Sweigart al@inventwithpython.com
21
22 Rules:
23 Try to get as close to 21 without going over.
24 Kings, Queens, and Jacks are worth 10 points.
25 Aces are worth 1 or 11 points.
26 Cards 2 through 10 are worth their face value.
27 (H)it to take another card.
28 (S)tand to stop taking cards.
29 On your first play, you can (D)ouble down to increase your bet
30 but must hit exactly one more time before standing.
31 In case of a tie, the bet is returned to the player.
32 The dealer stops hitting at 17.''')
33
34 money = 5000
35 while True: # Main game loop.
36 # Check if the player has run out of money:
37 if money <= 0:
38 print("You're broke!")
39 print("Good thing you weren't playing with real money.")
40 print('Thanks for playing!')
41 sys.exit()
42
43 # Let the player enter their bet for this round:
44 print('Money:', money)
45 bet = getBet(money)
46
47 # Give the dealer and player two cards from the deck each:
48 deck = getDeck()
49 dealerHand = [deck.pop(), deck.pop()]
50 playerHand = [deck.pop(), deck.pop()]
51
52 # Handle player actions:
53 print('Bet:', bet)
54 while True: # Keep looping until player stands or busts.
55 displayHands(playerHand, dealerHand, False)
56 print()
57
58 # Check if the player has bust:
59 if getHandValue(playerHand) > 21:
60 break
61
62 # Get the player's move, either H, S, or D:
63 move = getMove(playerHand, money - bet)
64
65 # Handle the player actions:
66 if move == 'D':
67 # Player is doubling down, they can increase their bet:
68 additionalBet = getBet(min(bet, (money - bet)))
69 bet += additionalBet
70 print('Bet increased to {}.'.format(bet))
71 print('Bet:', bet)
72
73 if move in ('H', 'D'):
74 # Hit/doubling down takes another card.
75 newCard = deck.pop()
76 rank, suit = newCard

Callers 1

blackjack.pyFile · 0.70

Calls 5

getBetFunction · 0.85
getDeckFunction · 0.85
getMoveFunction · 0.85
displayHandsFunction · 0.70
getHandValueFunction · 0.70

Tested by

no test coverage detected