-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcardsdeck.py
More file actions
66 lines (56 loc) · 1.83 KB
/
Copy pathcardsdeck.py
File metadata and controls
66 lines (56 loc) · 1.83 KB
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
'''
Created on 4 Nov 2013
@author: sergey.burnevsky@gmail.com
'''
import random
Suits = ['s','c','d','h']
Faces = ['a','k','q','j','t','9','8','7','6','5','4','3','2']
Jokers = ['r','b']
'''
Cards a represented by a list of 2-char strings, made up
by concatenating suit and face substrings, like "sq" - Queen of Spades.
Jokers a single-char strings.
'''
def to_string(cards):
''' Converts cards list to a single string, for serialization '''
return ' '.join(cards)
def from_string(cards_str):
''' Parsers list of cards from a string created as by tostring()'''
if isinstance(cards_str, basestring):
cards = cards_str.split(' ')
# TODO: check that every substring is correctly encoded card
return cards
else:
raise TypeError('String is expected')
class Deck(object):
''' Represents a deck of cards, which should not be accessed directly '''
def __init__(self, size):
self._cards = Deck.create_deck(size)
random.shuffle(self._cards)
def deal_one(self):
return self._cards.pop()
def deal_many(self, count):
if count > len(self._cards):
raise IndexError
cards = []
for k in range(count):
cards.append(self._cards.pop())
return cards
@classmethod
def create_deck(cls, size):
'''
Size should be one of:
24 - from 9 to ace
36 - from 6 to ace
52 - full deck
54 - full deck with jokers
'''
useJokers = (size == 54)
family = size // 4
cards = []
for s in Suits:
for f in range(family):
cards.append(s + Faces[f])
if useJokers:
cards.extend(Jokers)
return cards