Skip to content

Instantly share code, notes, and snippets.

@Lvl4Sword
Last active July 3, 2023 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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