-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck.py
84 lines (71 loc) · 1.92 KB
/
deck.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from card import card
from collections import deque
import random # shuffle
class deck :
def __init__ (self, n=1) :
"""
the constructor for deck
adds all 52 cards to its cards array
n decks
"""
self.cards = deque()
#supports multiple decks
for d in xrange(n) :
for i in xrange(4) :
for j in xrange(2, 15) :
self.cards.append(card(j, i))
def print_deck (self) :
"""
a method that prints the entire deck
"""
for x in self.cards :
print x
def top (self) :
"""
a method for dealing the top card off the deck
return the top card in the deck
"""
try :
return self.cards.popleft()
except IndexError :
return None
def shuffle (self) :
"""
a method for shuffling the deck
"""
random.shuffle(self.cards)
def __len__ (self) :
"""
return the number of cards still in this deck
"""
return len(self.cards)
def add (self, c) :
"""
a method for adding cards to the bottom of the desk
"""
self.cards.append(c)
def add_all (self, c) :
"""
a method for adding multiple cards
"""
for i in c :
self.cards.append(i)
def create_stakes (self) :
"""
a method to create the stakes for a battle.
always leaves one card for the end.
if deck is empty, returns None
"""
if len(self.cards) == 0 :
return None
else :
return [self.top() for i in xrange(min(3,len(self.cards)-1))]
if (__name__ == "__main__") :
x = deck()
x.print_deck()
print "shuffling..."
x.shuffle()
x.print_deck()
print len(x)
print x.top()
print len(x)