Skip to content

Instantly share code, notes, and snippets.

@Lvl4Sword
Last active July 3, 2023 07:03
Show Gist options
  • Save Lvl4Sword/3b95f9b991ca66a17210f8783f91ddc4 to your computer and use it in GitHub Desktop.
Save Lvl4Sword/3b95f9b991ca66a17210f8783f91ddc4 to your computer and use it in GitHub Desktop.
import random
import sys
BLACKJACK = 21
ACE_IS_ELEVEN = 10
ACE_IS_ONE = 11
EXITS = ['e', 'x', 'q', 'exit', 'quit', 'escape', 'leave']
cards = {'🂾': 10, '🂽': 10, '🂻': 10, '🂺': 10, '🂹': 9, '🂸': 8, '🂷': 7,
'🂶': 6, '🂵': 5, '🂴': 4, '🂳': 3, '🂲': 2, '🂱': 'A', '🃎': 10,
'🃍': 10, '🃋': 10, '🃊': 10, '🃉': 9, '🃈': 8, '🃇': 7, '🃆': 6,
'🃅': 5, '🃄': 4, '🃃': 3, '🃂': 2, '🃁': 'A', '🂮': 10, '🂭': 10,
'🂫': 10, '🂪': 10, '🂩': 9, '🂨': 8, '🂧': 7, '🂦': 6, '🂥': 5,
'🂤': 4, '🂣': 3, '🂢': 2, '🂡': 'A', '🃞': 10, '🃝': 10, '🃛': 10,
'🃚': 10, '🃙': 9, '🃘': 8, '🃗': 7, '🃖': 6, '🃕': 5, '🃔': 4,
'🃓': 3, '🃒': 2, '🃑': 'A'}
card_faces = list(cards.keys())
dealer_hand = []
your_hand = []
your_total = 0
dealer_total = 0
dealer_temp_total = 0
user_lost = False
dealer_lost = False
def shuffle_the_cards():
shuffle_this_many_times = random.choice(range(20, 2049))
for each in range(shuffle_this_many_times):
random.shuffle(card_faces)
deck = [(card, cards[card]) for card in card_faces]
return deck
if __name__ == '__main__':
the_deck = shuffle_the_cards()
your_hand.append(the_deck[0][0])
try:
your_total += the_deck[0][1]
# This only happens on the ace
except TypeError:
if your_total + 11 > BLACKJACK:
your_total += 1
else:
your_total += 11
the_deck.pop(0)
dealer_hand.append(the_deck[0][0])
try:
dealer_total += the_deck[0][1]
dealer_temp_total += the_deck[0][1]
# This only happens on the ace
except TypeError:
if dealer_total + 11 > BLACKJACK:
dealer_total += 1
dealer_temp_total += 1
else:
dealer_total += 11
dealer_temp_total += 11
the_deck.pop(0)
your_hand.append(the_deck[0][0])
try:
your_total += the_deck[0][1]
except TypeError:
if your_total + 11 > BLACKJACK:
your_total += 1
else:
your_total += 11
the_deck.pop(0)
dealer_hand.append(the_deck[0][0])
try:
dealer_total += the_deck[0][1]
except TypeError:
if dealer_total + 11 > BLACKJACK:
dealer_total += 1
else:
dealer_total += 11
the_deck.pop(0)
if your_total > 21:
print(f'Your hand: {your_hand[0]} / {your_hand[1]} -- TOTAL: {your_total}')
print(f'Dealer hand: {dealer_hand[0]} / {dealer_hand[1]} -- TOTAL: {dealer_total}')
print('YOU LOST')
user_lost = True
sys.exit()
if dealer_total > 21:
print(f'Dealer hand: {dealer_hand[0]} / {dealer_hand[1]} -- TOTAL: {dealer_total}')
print('DEALER LOST')
dealer_lost = True
sys.exit()
if not user_lost or not dealer_lost:
print(f"Your hand: {' /'.join([your_hand[x] for x in range(len(your_hand))])} -- TOTAL: {your_total}")
print(f'Dealer hand: {dealer_hand[0]} /? -- Total: {dealer_temp_total}')
while your_total < 21:
try:
user_hit = input('Hit/Stand? ( Exit to exit ): ').lower()
except KeyboardInterrupt:
print()
sys.exit()
if user_hit in ['h', 'hit', '0', 'first']:
user_hit = True
your_hand.append(the_deck[0][0])
try:
your_total += the_deck[0][1]
except TypeError:
if your_total + 11 > BLACKJACK:
your_total += 1
else:
your_total += 11
the_deck.pop(0)
if your_total > 21:
print(f"Your hand: {' /'.join([your_hand[x] for x in range(len(your_hand))])} -- TOTAL: {your_total}")
print(f"Dealer hand: {' /'.join([dealer_hand[x] for x in range(len(dealer_hand))])} -- TOTAL: {dealer_total}")
print('YOU LOST')
user_lost = True
sys.exit()
else:
print(f"Your hand: {' /'.join([your_hand[x] for x in range(len(your_hand))])} -- TOTAL: {your_total}")
print(f'Dealer hand: {dealer_hand[0]} /? -- TOTAL: {dealer_temp_total}')
elif user_hit in ['stand', 's']:
break
elif user_hit.casefold() in EXITS:
sys.exit()
else:
print("Let's try this again..")
while dealer_total <= 15:
dealer_hand.append(the_deck[0][0])
try:
dealer_total += the_deck[0][1]
except TypeError:
if dealer_total + 11 > BLACKJACK:
dealer_total += 1
else:
dealer_total += 11
the_deck.pop(0)
if dealer_total > 21:
print(f"Your hand: {' /'.join([your_hand[x] for x in range(len(your_hand))])} -- TOTAL: {your_total}")
print(f"Dealer hand: {' /'.join([dealer_hand[x] for x in range(len(dealer_hand))])} -- TOTAL: {dealer_total}")
print('DEALER LOST')
dealer_lost = True
else:
print(f"Your hand: {' /'.join([your_hand[x] for x in range(len(your_hand))])} -- TOTAL: {your_total}")
print(f"Dealer hand: {' /'.join([dealer_hand[x] for x in range(len(dealer_hand))])} -- TOTAL: {dealer_total}")
if your_total <= BLACKJACK:
if dealer_total >= your_total:
print('DEALER WON')
elif your_total > dealer_total:
print('YOU WON')
elif your_total == BLACKJACK:
print('BLACKJACK!')
else:
if dealer_total <= BLACKJACK:
print('DEALER WON')
if dealer_total == BLACKJACK:
print('DEALER BLACKJACK')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment