-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeck.py
101 lines (86 loc) · 3.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'''
Created on 18.2.2014
@author: Paleksi
'''
from card import Card
import random
class Deck(object):
'''
This class holds (max.) 52 Card-objects that it creates in set_deck(). A new deck is
made every round and the old deck discarded (pretty rough, but that's virtual Casino
for you all right). The deck only knows what cards it has and will only pop() the
last item when a card is drawn from it.
'''
def __init__(self):
self._cards = []
# Setters and getters
########################################
# The real initialization of a deck, but since load_game() adds the cards separately, this can't be in the init.
def set_deck(self):
# Initialize the card deck. Create 52 card objects and add them to the self._cards stack.
i = 0 # Suit index
suits = [Card.CLUBS, Card.SPADES, Card.HEARTS, Card.DIAMONDS]
while (i < 4): # Clubs - Diamonds
suit = suits[i]
n = 0 # Card value
while (n < 13): # Ace - King
card = Card(n+1, suit)
self._cards.append(card)
n += 1
# /while
i += 1
# /while
# Set images for the cards
for card in self._cards:
suit = ''
if (card.get_suit() == Card.CLUBS):
suit = 'Clubs'
elif (card.get_suit() == Card.DIAMONDS):
suit = 'Diamonds'
elif (card.get_suit() == Card.HEARTS):
suit = 'Hearts'
else:
suit = 'Spades'
path = "Images/%s (%d)"%(suit, card.get_value())
card.set_image(path)
path += " selected"
card.set_select_image(path)
# /for
def get_cards(self):
return self._cards
def set_card(self, card):
self._cards.append(card)
########################################
def draw_card(self):
return self._cards.pop()
def shuffle_deck(self):
random.shuffle(self._cards)
# Get the card data for the save-file and return a string
# Containing all the data from the cards and also the size
# of the string in a format YYY. If there are 3 cards left
# in the deck the YYY will be "009" since it takes 3 characters
# to print a single card to save-format
def save_deck(self):
text = ''
for card in self._cards:
text += card.save_card()
card_length = len(text)
if (card_length < 10):
card_length = '00' + str(card_length)
elif (card_length < 100):
card_length = '0' + str(card_length)
else:
card_length = str(card_length)
final = "DCK" + card_length + text
return final
# Load the deck data from a save file
def load_deck(self, chunk):
while (len(chunk) > 0):
info = chunk[0:3]
card = Card(1, Card.SPADES) # just init the card
loaded = card.load_card(info) # loaded properly here
if not (loaded):
return False
self._cards.append(card)
chunk = chunk[3:len(chunk)]
return True