Skip to content

Instantly share code, notes, and snippets.

@davegotz
Created March 28, 2019 14:40
Show Gist options
  • Select an option

  • Save davegotz/7aa98fb9acb63cdbd5cffe1fd76efeca to your computer and use it in GitHub Desktop.

Select an option

Save davegotz/7aa98fb9acb63cdbd5cffe1fd76efeca to your computer and use it in GitHub Desktop.
import random
# Playing Card class, represents a single playing card.
class PlayingCard:
def __init__(self, value, suit):
# Setup the instance variables.
if value == 1:
value = 'Ace'
elif value == 11:
value = 'Jack'
elif value == 12:
value = 'Queen'
elif value == 13:
value = 'King'
self.value = value
self.suit = suit
self.face_up = False
def get_suit(self):
return self.suit
def get_value(self):
return self.value
# Returns a number, 1-10, corresponding to the card face value.
# Aces are 1, Royals are 10.
def get_number_value(self):
try:
return int(self.value)
except ValueError:
# This will only happen if self.value is not a number.
if self.value == "Ace":
return 1
else:
return 10
# 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)
def main():
deck_of_cards = Deck()
# Shuffle the deck
deck_of_cards.shuffle()
# Draw three cards
test_card = deck_of_cards.draw_card()
another_card = deck_of_cards.draw_card()
third_card = deck_of_cards.draw_card()
# Printing card values.
print(test_card.get_value())
print(another_card.get_value())
print(third_card.get_value())
# Printing card number values
print(test_card.get_number_value())
print(another_card.get_number_value())
print(third_card.get_number_value())
# Test flipping a card.
print("Card three face up = ", third_card.is_face_up())
third_card.flip()
print("Card three face up = ", third_card.is_face_up())
third_card.flip()
print("Card three face up = ", third_card.is_face_up())
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment