-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ01_deck_of_cards.py
65 lines (52 loc) · 1.36 KB
/
Q01_deck_of_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
57
58
59
60
61
62
63
64
65
import unittest
import random
class Card:
SUITS = ("Clubs", "Diamonds", "Hearts", "Spades")
RANKS = (
"narf",
"Ace",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"Jack",
"Queen",
"King",
)
def __init__(self, suit=0, rank=0):
self.suit = suit
self.rank = rank
def __str__(self):
return "{0} of {1}".format(Card.RANKS[self.rank], Card.SUITS[self.suit])
class Deck:
def __init__(self, cards=[]):
self.cards = cards
def shuffle(self):
for i in range(len(self.cards) - 1, 0, -1):
r = random.randint(0, i)
self.cards[i], self.cards[r] = self.cards[r], self.cards[i]
def draw(self):
return self.cards.pop()
def show(self):
for card in self.cards:
print(card)
class BlackjackHand(Deck):
def value(self):
value = 0
for card in self.cards:
value += min(card.rank, 10)
return value
class Test(unittest.TestCase):
def test_deck_of_cards(self):
deck = BlackjackHand([Card(1, 3), Card(2, 5), Card(2, 11)])
self.assertEqual(18, deck.value())
deck.show()
drawn = deck.draw()
self.assertEqual(2, drawn.suit)
self.assertEqual(11, drawn.rank)
deck.shuffle()