-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeck.py
52 lines (39 loc) · 1.23 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
from abc import ABCMeta
from random import random
from util import bridge_shuffle, cut_deck
shuffle_methods = {
'bridge': bridge_shuffle,
}
class Deck(object, metaclass=ABCMeta):
cards = None
def __init__(self, cards=None):
if cards:
self.cards = cards
else:
self.cards = []
def __str__(self):
return 'Deck:\n{}'.format('\n'.join([str(card) for card in self.cards]))
def __len__(self):
return len(self.cards)
def shuffle_well(self):
# Thorough shuffle
for i in range(5):
self.shuffle()
for j in range(5):
self.cut(pivot=0.3)
def shuffle(self, method='bridge', pivot=0.5):
self.cards = shuffle_methods[method](self.cards, pivot=pivot)
def cut(self, pivot=0.5):
self.cards = cut_deck(self.cards, pivot=pivot)
def draw(self, cards_num=1):
drawn_cards = self.cards[:cards_num]
self.cards = self.cards[cards_num:]
if cards_num == 1:
return drawn_cards[0]
else:
return drawn_cards
def add(self, card, to_top=False):
if to_top:
self.cards.insert(0, card)
else:
self.cards.append(card)