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
| 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() |
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
| 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): |
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
| 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()) |
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
| 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 |
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
| 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) |
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
| 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: |