This is an updated version of my previously posted "Blackjack game made in Python 3". I think I did almost everything @Austin Hastings recommend me to do and I hope you like it.
from random import shuffle
import os
def shuffled_shoe():
shoe = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'A', 'J', 'Q', 'K']*4
shuffle(shoe)
return shoe
def deal_card(shoe, person, number):
for _ in range(number):
person.append(shoe.pop())
def deal_hand(shoe, player, dealer):
deal_card(shoe, player, 2)
deal_card(shoe, dealer, 2)
def score(person):
non_aces = [c for c in person if c != 'A']
aces = [c for c in person if c == 'A']
total = 0
for card in non_aces:
if card in 'JQK':
total += 10
else:
total += int(card)
for card in aces:
if total <= 10:
total += 11
else:
total += 1
return total
def display_info(player, dealer, player_stands):
os.system('cls' if os.name == 'nt' else 'clear')
print("Your cards: [{}] ({})".format("][".join(player), score(player)))
if player_stands:
print("Dealer cards: [{}] ({})".format("][".join(dealer), score(dealer)))
else:
print(f"Dealer cards: [{dealer[0]}][?]")
def hit_or_stand():
while True:
print("What do you choose?")
print("[1] Hit")
print("[2] Stand")
ans = input("> ")
if ans in '12':
return ans
def player_play(shoe, player, dealer, player_plays, dealer_plays, player_stands):
while not player_stands:
if hit_or_stand() == '2':
player_plays = False
dealer_plays = True
player_stands = True
display_info(player, dealer, True)
elif not player_stands:
deal_card(shoe, player, 1)
display_info(player, dealer, False)
if score(player) >= 21:
player_plays = False
break
return (player_plays, dealer_plays, player_stands)
def dealer_play(shoe, dealer, DEALER_MINIMUM_SCORE, player):
while score(dealer) <= DEALER_MINIMUM_SCORE:
deal_card(shoe, dealer, 1)
display_info(player, dealer, True)
return False
def results(player, dealer, player_stands, first_hand, still_playing):
if score(player) == 21:
print("Blackjack! You won")
still_playing = False
elif first_hand and score(dealer) == 21:
print("Dealer got a blackjack. You lost!")
still_playing = False
elif score(player) > 21:
print("Busted! You lost!")
still_playing = False
if player_stands:
if score(dealer) > 21:
print("Dealer busted! You won")
elif score(player) > score(dealer):
print("You beat the dealer! You won!")
elif score(player) < score(dealer):
print("Dealer has beaten you. You lost!")
else:
print("Push. Nobody wins or losses.")
still_playing = False
return still_playing
def main():
shoe = shuffled_shoe()
player = []
dealer = []
player_plays = True
still_playing = True
dealer_plays = False
player_stands = False
deal_hand(shoe, player, dealer)
display_info(player, dealer, player_stands)
still_playing = results(player, dealer, player_stands, True, still_playing)
while still_playing:
while player_plays:
(player_plays, dealer_plays, player_stands) = player_play(shoe, player, dealer, player_plays, dealer_plays, player_stands)
while dealer_plays:
dealer_plays = dealer_play(shoe, dealer, 17, player)
still_playing = results(player, dealer, player_stands, False, still_playing)
if __name__ == '__main__':
main()