Last active
July 3, 2023 07:03
-
-
Save Lvl4Sword/3b95f9b991ca66a17210f8783f91ddc4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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