Last active
April 2, 2019 14:50
-
-
Save davegotz/672c310d9fca77e88ac1ed075d5f314f to your computer and use it in GitHub Desktop.
Added hand implementation and __str__ methods.
This file contains hidden or 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
| import random | |
| # Playing Card class, represents a single playing card. | |
| class PlayingCard: | |
| # Value should be 1-13, with 1 for Ace, 11 for Jack, etc. | |
| def __init__(self, value, suit): | |
| # Setup the instance variables. | |
| self.value = value | |
| self.suit = suit | |
| self.face_up = False | |
| def __str__(self): | |
| if self.face_up: | |
| return self.get_string_value() + " of " + self.get_suit() | |
| else: | |
| return "#####################" | |
| def get_suit(self): | |
| return self.suit | |
| # Returns Ace, 1-10 (as strings), Jack, Queen, or King | |
| def get_string_value(self): | |
| if self.value == 1: | |
| return 'Ace' | |
| elif self.value == 11: | |
| return 'Jack' | |
| elif self.value == 12: | |
| return 'Queen' | |
| elif self.value == 13: | |
| return 'King' | |
| else: | |
| return str(self.value) | |
| # Returns a number, 1-10, corresponding to the card face value. | |
| # Aces are 1, Royals are 10. | |
| def get_number_value(self): | |
| if self.value > 10: | |
| return 10 | |
| else: | |
| return self.value | |
| # Inverts the face up status. | |
| def flip(self): | |
| self.face_up = not self.face_up | |
| def is_face_up(self): | |
| return self.face_up | |
| # A deck of playing cards. | |
| class Deck: | |
| # Creates a deck in sorted order. | |
| def __init__(self): | |
| self.cards = [] | |
| for suit in ['Spades', 'Diamonds', 'Hearts', 'Clubs']: | |
| for val in range(1, 14): | |
| # Create a card with val in suit | |
| new_card = PlayingCard(val, suit) | |
| self.cards.append(new_card) | |
| def draw_card(self): | |
| return self.cards.pop() | |
| # Add the list_of_cards to the deck. | |
| def add_cards(self, list_of_cards): | |
| self.cards += list_of_cards | |
| # Shuffle 7 times. | |
| def shuffle(self): | |
| for i in range(7): | |
| random.shuffle(self.cards) | |
| # A hand of blackjack cards | |
| class Hand: | |
| # Initialize the hand | |
| def __init__(self): | |
| self.cards = [] | |
| def get_total_value(self): | |
| total = 0 | |
| # Sum up the value of all cards and return | |
| for card in self.cards: | |
| total += card.get_number_value() | |
| return total | |
| def add_card(self, new_card): | |
| self.cards.append(new_card) | |
| def get_size(self): | |
| return len(self.cards) | |
| def remove_all_cards(self): | |
| # Remember the cards to return | |
| cards_to_return = self.cards | |
| # Empty the hand | |
| self.cards = [] | |
| # Return the old cards | |
| return cards_to_return | |
| # Convert to a string for display | |
| def __str__(self): | |
| str_version = "Hand of " + str(self.get_size()) + " cards:\n" | |
| for card in self.cards: | |
| str_version += "\t" + str(card) + "\n" | |
| return str_version | |
| def main(): | |
| # Shuffle the deck | |
| deck_of_cards = Deck() | |
| deck_of_cards.shuffle() | |
| # Create a hand. | |
| hand_of_cards = Hand() | |
| for i in range(5): | |
| new_card = deck_of_cards.draw_card() | |
| new_card.flip() | |
| hand_of_cards.add_card(new_card) | |
| # Print the hand | |
| print(hand_of_cards) | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment