Skip to content

Instantly share code, notes, and snippets.

@twit96
twit96 / blackjack_card_class.py
Last active August 9, 2020 01:54
Python class to represent a single card in a deck of cards.
class Card(object):
RANKS = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
SUITS = ('C', 'D', 'H', 'S')
# constructor
def __init__(self, rank=12, suit='S'):
if (rank in Card.RANKS):
self.rank = rank
else:
@twit96
twit96 / blackjack_deck_class.py
Last active August 9, 2020 01:55
Python class to represent a deck of cards for the game of Blackjack, where each card is its own card object.
class Deck(object):
# constructor
def __init__(self, n=1):
self.deck = []
for i in range(n):
for suit in Card.SUITS:
for rank in Card.RANKS:
card = Card(rank, suit)
self.deck.append(card)
@twit96
twit96 / blackjack_player_class.py
Last active August 9, 2020 01:49
Python class to represent a player in a game of Blackjack, where each card is represented as a Card class object and the deck of cards is represented as a Deck class object.
class Player(object):
# cards is a list of Card objects
def __init__(self, cards):
self.cards = cards
# when a player hits append a card
def hit(self, card):
self.cards.append(card)
# count the points in the Players's hand
@twit96
twit96 / blackjack_dealer_class.py
Last active August 9, 2020 01:51
Python class to represent a dealer in a game of Blackjack, where each player is represented as a Player class object, each card is represented as a Card class object, and the deck of cards is represented as a Deck class object.
class Dealer(Player):
def __init__(self, cards):
Player.__init__(self, cards)
self.show_one_card = True
# over-ride the hit() function in the parent class
def hit(self, deck):
self.show_one_card = False
while (self.get_points() < 17):
self.cards.append(deck.deal())
@twit96
twit96 / blackjack_game_class.py
Created August 9, 2020 01:51
Python class to represent a game of Blackjack, where each dealer is represented as a Dealer class object, each player is represented as a Player class object, each card is represented as a Card class object, and the deck of cards is represented as a Deck class object.
class Blackjack(object):
def __init__(self, num_players=1):
self.deck = Deck()
self.deck.shuffle()
# create the number of Player objects
self.num_players = num_players
self.player_list = []
for i in range(self.num_players):
@twit96
twit96 / blackjack_starter_code.py
Created August 9, 2020 01:53
Python code used to initialize a game of Blackjack, where the game, the dealer, each player, each card, and the deck of cards are represented as class objects.
while True:
try:
num_players = int(input('Enter number of players: '))
while (num_players < 1 or num_players > 6):
num_players = int(input('Enter number of players: '))
break
except ValueError:
pass
print()