-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecks_and_cards.py
56 lines (46 loc) · 1.23 KB
/
decks_and_cards.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import math
import random
values = {
2: "Two",
3: "Three",
4: "Four",
5: "Five",
6: "Six",
7: "Seven",
8: "Eight",
9: "Nine",
10: "Ten",
11: "Jack",
12: "Queen",
13: "King",
14: "Ace"
}
class Card(object):
# all cards (kind of) in this game are objects which have suit and value
def __init__(self, suit, val):
self.suit = suit
self.value = val
def show(self):
return "{} of {}".format(values.get(self.value), self.suit)
class Deck(object):
# deck object to keep track of a deck and to deal cards when asked
def __init__(self, numDecks):
self.cards = []
self.numDecks = numDecks
self.build()
# Display all cards in the deck
def show(self):
for card in self.cards:
return card.show()
# Generate 52 cards
def build(self):
for i in range(self.numDecks):
for suit in ['Hearts', 'Clubs', 'Diamonds', 'Spades']:
for val in range(2, 15):
self.cards.append(Card(suit, val))
# Shuffle the deck
def shuffle(self):
random.shuffle(self.cards)
# Returns and pops the top card
def draw(self):
return self.cards.pop()