-
Notifications
You must be signed in to change notification settings - Fork 0
/
card_deck.py
42 lines (33 loc) · 1.4 KB
/
card_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
from random import shuffle
from time import sleep
suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
# Note: the strategy with the Ace is to take its value to be 11 unless that makes the player go bust, in
# which case the value is taken to be 1.
values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8,
'Nine':9, 'Ten':10, 'Jack':10, 'Queen':10, 'King':10, 'Ace':11}
class Card:
def __init__(self,suit,rank):
self.suit = suit
self.rank = rank
self.value = values[rank]
def __str__(self):
return self.rank + ' of ' + self.suit
class Deck:
def __init__(self):
# Creation of a new deck
self.all_cards = []
for suit in suits:
for rank in ranks:
# This assumes the Card class has already been defined!
self.all_cards.append(Card(suit,rank))
# We play a 6-deck game
self.all_cards *= 6
def shuffle(self):
# Reorder the cards
shuffle(self.all_cards)
def deal_one(self):
# Note we remove one card from the list of all_cards
return self.all_cards.pop()
def __len__(self):
return len(self.all_cards) # Initially 52*6 = 312 cards