diff --git a/blackjack/Deck.py b/blackjack/Deck.py new file mode 100644 index 0000000..657199b --- /dev/null +++ b/blackjack/Deck.py @@ -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]) diff --git a/blackjack/Hand.py b/blackjack/Hand.py new file mode 100644 index 0000000..74bacdb --- /dev/null +++ b/blackjack/Hand.py @@ -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 diff --git a/game.py b/game.py new file mode 100644 index 0000000..206eea7 --- /dev/null +++ b/game.py @@ -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()