Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions blackjack/Deck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

import random


class Deck():
RANK = ['2', '3', '4', '5', '6', '7', '8', '9', 'ten', 'jack', 'queen', 'king', 'ace']
SUIT = [' of clubs', ' of hearts', ' of spades', ' of diamonds']
deck_list = []
# Should this be a function?

for i in RANK:
deck_list.append(i + SUIT[0])
deck_list.append(i + SUIT[1])
deck_list.append(i + SUIT[2])
deck_list.append(i + SUIT[3])
30 changes: 30 additions & 0 deletions blackjack/Hand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import re


class Hand():

def hand_check(hand):
points = 0
for card in hand:
if 'jack' in card or 'queen' in card or 'king' in card or 'ten' in card:
points += 10
elif 'ace' in card:
ace = input('Ace high? or low? ')
if ace == 'high':
points += 11
if ace == 'low':
points += 1
elif re.match(r'\d*', card) is not None:
points += int(re.match(r'\d', card).group())
return points

def dealer_check(hand):
points = 0
for card in hand:
if 'jack' in card or 'queen' in card or 'king' in card or 'ten' in card:
points += 10
elif 'ace' in card:
points += 11
elif re.match(r'\d*', card) is not None:
points += int(re.match(r'\d', card).group())
return points
54 changes: 54 additions & 0 deletions game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import random
import re
from blackjack import Hand
from blackjack import Deck


def card_pick(a_deck):
return a_deck.pop(random.randrange(len(a_deck)))


def dealer_protocol(hand, deck):
while Hand.Hand.hand_check(hand) < 17:
hand.append(card_pick(deck))
return hand


def game():
deck = Deck.Deck.deck_list
p_hand = [card_pick(deck), card_pick(deck)]
d_hand = [card_pick(deck), card_pick(deck)]
print("The Dealer's: {}".format(d_hand[1:]))
while True:
dupdate = Hand.Hand.dealer_check(d_hand)
print("Your hand: {}".format(p_hand))
print("Your point total is: {}".format(Hand.Hand.hand_check(p_hand)))
update = Hand.Hand.hand_check(p_hand)
if update > 21:
print("You lose!")
break
decision = input('Hit or Stay? ').lower()
if decision == 'hit':
p_hand.append(card_pick(deck))
elif decision == 'stay':
dup = dealer_protocol(d_hand, deck)
print("The dealer is worth: {}".format(dup))
duplex = Hand.Hand.dealer_check(dup)
if update == duplex:
print("TIE!")
break
elif update > duplex or duplex > 21:
print('You WIN!')
break
else:
print("You lose!")
break

game()
while True:
again = input("Would you like to play again? y/n: ")
if again == 'y':
game()
else:
print("You'll be back")
exit()