blackjack/blackjack.py

221 lines
5.5 KiB
Python
Raw Normal View History

2022-12-11 00:26:36 -05:00
title = '''\
2022-12-10 04:43:49 -05:00
.oPYo. 8 8 o 8
8 `8 8 8 8 8
o8YooP' 8 .oPYo. .oPYo. 8 .o 8 .oPYo. .oPYo. 8 .o
8 `b 8 .oooo8 8 ' 8oP' 8 .oooo8 8 ' 8oP'
8 8 8 8 8 8 . 8 `b. 8 8 8 8 . 8 `b.
8oooP' 8 `YooP8 `YooP' 8 `o. oP' `YooP8 `YooP' 8 `o.
:......:..:.....::.....:..::......::.....::.....:..::...
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::'''
import random
def make_card(vs, style):
value = vs[0]
2022-12-11 00:26:36 -05:00
2022-12-11 00:06:18 -05:00
if value == '0':
value = '10'
else:
value += ' '
suit = vs[1] + ' '
2022-12-10 04:43:49 -05:00
2022-12-11 04:02:12 -05:00
# request text render of preceeding glyph but can break some fonts
2022-12-11 00:26:36 -05:00
# suit += '\uFE0E'
2022-12-10 04:43:49 -05:00
deck = '''\
'''
card_rest = f'''\
'''
hidden_part = f'''\
'''
hidden_rest = f'''\
'''
card_part = f'''\
2022-12-11 00:06:18 -05:00
{value}
{suit}
2022-12-10 04:43:49 -05:00
'''
if style == 'deck':
return deck
2022-12-11 04:02:12 -05:00
elif style == 'card_rest':
2022-12-10 04:43:49 -05:00
return card_rest
2022-12-11 04:02:12 -05:00
elif style == 'hidden_part':
2022-12-10 04:43:49 -05:00
return hidden_part
2022-12-11 04:02:12 -05:00
elif style == 'hidden_rest':
2022-12-10 04:43:49 -05:00
return hidden_rest
2022-12-11 04:02:12 -05:00
elif style == 'card_part':
2022-12-10 04:43:49 -05:00
return card_part
2022-12-11 00:26:36 -05:00
def generate_deck(n_decks):
2022-12-11 01:15:50 -05:00
n_decks = int(n_decks)
if n_decks > 8:
n_decks = 8
2022-12-10 04:43:49 -05:00
suits = '♠♥♦♣'
cards = 'A234567890JQK'
2022-12-11 00:26:36 -05:00
deck = []
while n_decks > 0:
deck += [card + suit for card in cards for suit in suits]
n_decks -= 1
return deck
2022-12-10 04:43:49 -05:00
def draw_card(player):
players.get(player).append(play_deck.pop(random.choice(range(len(play_deck)))))
def print_hand(player):
player_cards = [make_card(card,'card_part') for card in players.get(player)]
if player == 'Dealer' and game_active == True:
player_cards[0] = make_card('na','hidden_part')
player_cards.append(make_card('na','card_rest'))
card_slices = [str(card).splitlines() for card in player_cards]
for i in range(7):
clist = [card_slice[i] for card_slice in card_slices]
carriage = ''
for chunk in clist:
carriage += chunk
print(carriage)
def deal():
for i in range(2):
for player in players:
2022-12-11 00:26:36 -05:00
draw_card(player)
2022-12-10 04:43:49 -05:00
def show_board():
print('\033c', end='')
2022-12-11 00:26:36 -05:00
print(title)
2022-12-10 04:43:49 -05:00
for player in players:
2022-12-11 00:26:36 -05:00
print(' '+player,
'\t\t\t\t Score:',player_score(player))
print_hand(player)
2022-12-10 04:43:49 -05:00
def score_hand(player):
2022-12-11 04:02:12 -05:00
hand = [card[0] for card in players.get(player)]
2022-12-11 01:15:50 -05:00
if hand == ['0','1'] or hand == ['1','0']:
game_active = False
gameover = player, 'has blackjack!'
return [21]
2022-12-10 04:43:49 -05:00
hand = ['1' if card == 'A' else card for card in hand]
scores = [10 if card in 'JQK0' else int(card) for card in hand]
return scores
def player_score(player):
card_scores = score_hand(player)
if sum(card_scores) < 11:
if 1 in card_scores:
index = card_scores.index(1)
card_scores[index] = 11
if player == 'Dealer' and game_active == True:
return sum(card_scores[1:])
else:
return sum(card_scores)
def main():
global players
global play_deck
global game_active
global gameover
gameover = 'Error'
2022-12-11 00:26:36 -05:00
2022-12-10 04:43:49 -05:00
if len(play_deck) < 4:
2022-12-11 00:26:36 -05:00
play_deck = generate_deck(num_decks)
2022-12-10 04:43:49 -05:00
# 5-9 seats
2022-12-11 00:06:18 -05:00
players = {'Dealer': [],
2022-12-10 04:43:49 -05:00
'Player': []}
deal()
game_active = True
while game_active == True:
show_board()
user_input = input(str(len(play_deck)) + ' cards left in deck\n[H]it or [S]tand? ')
if user_input.lower() == 'q':
gameover = 'q'
game_active = False
2022-12-11 04:02:12 -05:00
elif user_input.lower() == 'r':
2022-12-10 04:43:49 -05:00
game_active = False
2022-12-11 04:02:12 -05:00
elif user_input.lower() == 'h':
2022-12-10 04:43:49 -05:00
draw_card('Player')
2022-12-11 04:02:12 -05:00
elif user_input.lower() == 's':
2022-12-10 04:43:49 -05:00
game_active = False
2022-12-11 00:26:36 -05:00
2022-12-11 04:02:12 -05:00
if player_score('Dealer') == 21:
gameover = 'Dealer has blackjack!'
break
2022-12-10 04:43:49 -05:00
while player_score('Dealer') < 17:
draw_card('Dealer')
2022-12-11 04:02:12 -05:00
2022-12-10 04:43:49 -05:00
if player_score('Dealer') > 21:
gameover = 'Dealer Bust!'
2022-12-11 04:02:12 -05:00
elif player_score('Dealer') == player_score('Player'):
2022-12-10 04:43:49 -05:00
gameover = 'Push.'
2022-12-11 04:02:12 -05:00
elif player_score('Dealer') > player_score('Player'):
2022-12-10 04:43:49 -05:00
gameover = 'House wins.'
2022-12-11 04:02:12 -05:00
elif player_score('Dealer') < player_score('Player'):
2022-12-10 04:43:49 -05:00
gameover = 'You win!'
if player_score('Player') > 21:
gameover = 'Bust!'
game_active = False
kill = False
2022-12-11 00:26:36 -05:00
play_deck = []
2022-12-10 04:43:49 -05:00
2022-12-11 01:15:50 -05:00
num_decks = input('How many decks? (1-8): ')
2022-12-10 04:43:49 -05:00
while kill == False:
main()
if gameover == 'q':
2022-12-11 04:02:12 -05:00
kill = True
2022-12-10 04:43:49 -05:00
break
show_board()
user_input = input(gameover + '\nPlay again? [Y/n] ')
if user_input.lower() == 'n':
kill = True