/
deck.py
49 lines (35 loc) · 1.21 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
import random
import operator as op
import card
class Deck:
def __bool__(self):
return bool(self.cards)
def __contains__(self, card_):
return any(repr(c) == card_ for c in self.cards)
def __str__(self):
if self.cards:
return '[' + ']['.join(map(repr, self.cards)) + ']'
else:
return ''
def add(self, card):
self.cards.append(card)
def draw(self):
return self.cards.pop()
def get(self, card_):
return next((c for c in self.cards if repr(c) == card_ or repr(c) == card.name_for_card(card_)), None)
def remove(self, card):
self.cards.remove(card)
def shuffle(self):
random.shuffle(self.cards)
def __init__(self, cards=None):
if cards is None:
cards = []
self.cards = cards
def load_deck(file_path):
"""Given a file path, load and return the deck."""
with open(file_path, 'r') as deck_file:
# Remove space from the file
# Every character in the file excluding whitespace is a card
cards = list(''.join(deck_file.read().split()))
cards = [card.Card(card.Card[card.name_for_card(card_)]) for card_ in cards]
return Deck(cards)