Created
August 7, 2018 21:03
-
-
Save solen003/ad1bb79a72c107550d582a817c47c3ca to your computer and use it in GitHub Desktop.
blackjack
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
suits = ('Hearts', 'Diamonds', 'Clubs', 'Spades') | |
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace') | |
values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10, 'Queen':10, 'King':10, 'Ace':11} | |
import random | |
def prepare_deck(): | |
a_deck = [] | |
for s in suits: | |
for r in ranks: | |
a_deck.append((r,s)) | |
random.shuffle(a_deck) | |
return a_deck | |
def deal_a_card(a_deck): | |
return a_deck.pop() | |
class PlayerHand(): | |
def __init__(self): | |
self.cards_on_hand = [] | |
self.value_on_hand = 0 | |
self.aces_on_hand = 0 | |
def add_card(self, a_card): | |
self.cards_on_hand.append(a_card) | |
self.value_on_hand += values[a_card[0]] | |
if a_card[0] == 'Ace': | |
self.aces_on_hand += 1 | |
def buy_in(): | |
while True: | |
try: | |
buy_in_amount = int(input('How much $ you want to buy? 1 chip = $1: ')) | |
except: | |
print('Please provide only an integer') | |
else: | |
break | |
return buy_in_amount | |
def take_bet(): | |
while True: | |
try: | |
bet_amount = int(input('Please make a bet: ')) | |
except: | |
print('Please provide only an integer') | |
else: | |
if bet_amount > chip_amount: | |
print(f'Not enough chips. You have {chip_amount} chips') | |
else: | |
break | |
return bet_amount | |
def showtime(): | |
if final_showdown: | |
print("\nDealer's Hand:") | |
for r,s in dealer_player.cards_on_hand: | |
print(f'{r} of {s}') | |
print(f'Dealers hand: {dealer_player.value_on_hand}') | |
print("\nPlayer's Hand:") | |
for r,s in human_player.cards_on_hand: | |
print(f'{r} of {s}') | |
print(f'Players hand: {human_player.value_on_hand}') | |
else: | |
print("\nDealer's Hand:") | |
print('<card hidden>') | |
for r,s in dealer_player.cards_on_hand[1:]: | |
print(f'{r} of {s}') | |
print("\nPlayer's Hand:") | |
for r,s in human_player.cards_on_hand: | |
print(f'{r} of {s}') | |
def hit_or_stand(): | |
h_or_s = '' | |
while not (h_or_s == 'H' or h_or_s == 'S'): | |
h_or_s = input('HIT or STAND? H/S: ').upper() | |
if h_or_s == 'H': | |
human_player.add_card(deal_a_card(the_deck)) | |
return h_or_s | |
elif h_or_s == 'S': | |
return h_or_s | |
def adjust_for_ace(whos_hand): | |
while whos_hand.value_on_hand > 21 and whos_hand.aces_on_hand > 0: | |
whos_hand.value_on_hand -= 10 | |
whos_hand.aces_on_hand -= 1 | |
chip_amount = buy_in() | |
while True: | |
# deck preparation | |
the_deck = prepare_deck() | |
# hand preparation | |
dealer_player = PlayerHand() | |
dealer_player.add_card(deal_a_card(the_deck)) | |
dealer_player.add_card(deal_a_card(the_deck)) | |
human_player = PlayerHand() | |
human_player.add_card(deal_a_card(the_deck)) | |
human_player.add_card(deal_a_card(the_deck)) | |
#game set | |
final_showdown = False | |
players_bet = take_bet() | |
# players turn | |
while True: | |
showtime() | |
players_choice = hit_or_stand() | |
adjust_for_ace(human_player) | |
if human_player.value_on_hand > 21: | |
final_showdown = True | |
showtime() | |
chip_amount -= players_bet | |
print('BUST!. Dealer wins') | |
break | |
elif players_choice == 'S': | |
print('Player stands. Dealer is playing.') | |
break | |
# dealers turn | |
if human_player.value_on_hand <= 21: | |
while True: | |
while dealer_player.value_on_hand < 17: | |
dealer_player.add_card(deal_a_card(the_deck)) | |
adjust_for_ace(dealer_player) | |
if dealer_player.value_on_hand > 17 and dealer_player.aces_on_hand == 0: | |
break | |
final_showdown = True | |
showtime() | |
dealer_to_21 = abs(21 - dealer_player.value_on_hand) | |
player_to_21 = abs(21 - human_player.value_on_hand) | |
if dealer_to_21 < player_to_21: | |
print('Dealer wins') | |
chip_amount -= players_bet | |
elif player_to_21 < dealer_to_21: | |
print('Player wins') | |
chip_amount += players_bet | |
elif player_to_21 == dealer_to_21: | |
print('Push') | |
if chip_amount == 0: | |
print('Out of chips') | |
break | |
new_game = input("Would you like to play another hand? Enter 'y' or 'n' ") | |
if new_game[0].lower()=='y': | |
continue | |
else: | |
print("Thanks for playing!") | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment