|
| 1 | +import random |
| 2 | + |
| 3 | +# each card must have a |
| 4 | +# suit (spade, diamond, heart, club) |
| 5 | +# value A, J, Q, K, 2 - 10 |
| 6 | + |
| 7 | +class Card: |
| 8 | + suit = "" |
| 9 | + value = "" |
| 10 | + |
| 11 | + def __init__(self, suit, value): |
| 12 | + self.suit = suit |
| 13 | + self.value = value |
| 14 | + |
| 15 | + def getSuit(self): |
| 16 | + return self.suit |
| 17 | + |
| 18 | + def getValue(self): |
| 19 | + return self.value |
| 20 | + |
| 21 | +# each card must have a |
| 22 | +# suit (spade, diamond, heart, club) |
| 23 | +# value A, J, Q, K, 2 - 10 |
| 24 | + |
| 25 | +class Deck: |
| 26 | + deck = [] |
| 27 | + dealtCards = [] |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + # Create deck of cards |
| 31 | + # Each deck will have 52 Cards |
| 32 | + # 13 Hearts (A, J, K, Q, 2 - 10) |
| 33 | + # 13 Spade (A, J, K, Q, 2 - 10) |
| 34 | + # 13 Club (A, J, K, Q, 2 - 10) |
| 35 | + # 13 Diamonds (A, J, K, Q, 2 - 10) |
| 36 | + |
| 37 | + # Loop over all suits |
| 38 | + for suit in ['Heart', 'Spade', 'Diamond', 'Club']: |
| 39 | + # For all suits, loop over all values |
| 40 | + for value in ['A', 'J', 'K', 'Q', '2', '3', '4', '5', '6', '7', '8', '9', '10']: |
| 41 | + self.deck.append(Card(suit, value)) |
| 42 | + |
| 43 | + def dealCard(self): |
| 44 | + # deal a random card |
| 45 | + # once dealt, a card cannot be dealt again |
| 46 | + |
| 47 | + # Make a random number using randint between 0 and the number of the cards in the deck |
| 48 | + # The 'pop' method removes this card and returns it. |
| 49 | + |
| 50 | + # We could add a check to see if we still have more cards! |
| 51 | + return self.deck.pop(random.randint(0, len(self.deck)-1)) |
| 52 | + |
| 53 | + def howManyCardsRemain(self): |
| 54 | + # return count of undealt cards |
| 55 | + return len(self.deck) |
| 56 | + |
0 commit comments