From 5a9a72ce23cea0eea4eff1203dd8c3e9550ba18d Mon Sep 17 00:00:00 2001 From: SorenOlegnowicz Date: Thu, 4 Jun 2015 00:43:53 -0400 Subject: [PATCH 1/2] Works well enough --- blackjack/Deck.py | 33 ++++++++++++++++++++++++++ blackjack/Hand.py | 41 ++++++++++++++++++++++++++++++++ game.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 blackjack/Deck.py create mode 100644 blackjack/Hand.py create mode 100644 game.py diff --git a/blackjack/Deck.py b/blackjack/Deck.py new file mode 100644 index 0000000..706a7aa --- /dev/null +++ b/blackjack/Deck.py @@ -0,0 +1,33 @@ + +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]) + + +# def __init__(self): + # self.RANK = RANK + # self.SUIT = SUIT + # self.p_dict = p_dict + # def e_list(self): + # return [[] for x in xrange(0, self.RANK)] + + """def point_dict(self): + for i in self.RANK: + if int(i) is True: + p_dict[i] = int(i) + elif i == 'ace': + p_dict[i] = 11 + else: + p_dict[i] = 10 + return p_dict""" diff --git a/blackjack/Hand.py b/blackjack/Hand.py new file mode 100644 index 0000000..2d81b18 --- /dev/null +++ b/blackjack/Hand.py @@ -0,0 +1,41 @@ +import re + +class Hand(): + hand = [] + + def __init__(self, hand): + self.hand = 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 + +class Player_hand(Hand): + pass + + +class Dealer_hand(Hand): + pass diff --git a/game.py b/game.py new file mode 100644 index 0000000..f525d97 --- /dev/null +++ b/game.py @@ -0,0 +1,59 @@ +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 + +# a = ['jack of hearts', 'queen of spades'] +# print(hand_check(a)) + + +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() + # Hand.check + # initialize dealer, END() From a2d1dfd68dbcff8a7382fdde60b461c9616fc437 Mon Sep 17 00:00:00 2001 From: SorenOlegnowicz Date: Thu, 4 Jun 2015 00:49:23 -0400 Subject: [PATCH 2/2] cleaned up a little more, works the same --- blackjack/Deck.py | 18 ------------------ blackjack/Hand.py | 13 +------------ game.py | 5 ----- 3 files changed, 1 insertion(+), 35 deletions(-) diff --git a/blackjack/Deck.py b/blackjack/Deck.py index 706a7aa..657199b 100644 --- a/blackjack/Deck.py +++ b/blackjack/Deck.py @@ -13,21 +13,3 @@ class Deck(): deck_list.append(i + SUIT[1]) deck_list.append(i + SUIT[2]) deck_list.append(i + SUIT[3]) - - -# def __init__(self): - # self.RANK = RANK - # self.SUIT = SUIT - # self.p_dict = p_dict - # def e_list(self): - # return [[] for x in xrange(0, self.RANK)] - - """def point_dict(self): - for i in self.RANK: - if int(i) is True: - p_dict[i] = int(i) - elif i == 'ace': - p_dict[i] = 11 - else: - p_dict[i] = 10 - return p_dict""" diff --git a/blackjack/Hand.py b/blackjack/Hand.py index 2d81b18..74bacdb 100644 --- a/blackjack/Hand.py +++ b/blackjack/Hand.py @@ -1,10 +1,7 @@ import re -class Hand(): - hand = [] - def __init__(self, hand): - self.hand = hand +class Hand(): def hand_check(hand): points = 0 @@ -21,7 +18,6 @@ def hand_check(hand): points += int(re.match(r'\d', card).group()) return points - def dealer_check(hand): points = 0 for card in hand: @@ -32,10 +28,3 @@ def dealer_check(hand): elif re.match(r'\d*', card) is not None: points += int(re.match(r'\d', card).group()) return points - -class Player_hand(Hand): - pass - - -class Dealer_hand(Hand): - pass diff --git a/game.py b/game.py index f525d97..206eea7 100644 --- a/game.py +++ b/game.py @@ -13,9 +13,6 @@ def dealer_protocol(hand, deck): hand.append(card_pick(deck)) return hand -# a = ['jack of hearts', 'queen of spades'] -# print(hand_check(a)) - def game(): deck = Deck.Deck.deck_list @@ -55,5 +52,3 @@ def game(): else: print("You'll be back") exit() - # Hand.check - # initialize dealer, END()