models.py
1 |
class Card: |
---|---|
2 |
"""
|
3 |
This class is a model of map data
|
4 |
that contains the type of map, the
|
5 |
suit of the map, the appearance of
|
6 |
the map, the width, height, position
|
7 |
on the screen, and methods for working with them
|
8 |
"""
|
9 |
data = {'type_card': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 'card_in_deck': 52, |
10 |
'card_in_pyr': 28, 'card_in_last_pyr': 7, 'card_sum': 13, 'start_open_pyr': 21} |
11 |
|
12 |
def __init__(self, atype, suit, visible=False, close=False, width=100, height=150): |
13 |
"""
|
14 |
The method initializes the creation of the class,
|
15 |
passes the type of the map, an integer value from 1 to 13.
|
16 |
The card's value, string value, diamonds, hearts, spades, clubs.
|
17 |
map visibility, Boolean value. width and height integer value
|
18 |
:param atype: integer 1<= atype <= 13
|
19 |
:param suit: string diamonds, hearts, spades, clubs
|
20 |
:param visible: Boolean True or False
|
21 |
:param width: integer
|
22 |
:param height: integer
|
23 |
"""
|
24 |
if type(atype) is int and atype in Card.data['type_card']: |
25 |
self.catype = atype
|
26 |
else:
|
27 |
raise Exception("Type not integral or not within 1..13") |
28 |
|
29 |
if type(suit) is str and suit in ['diamonds', 'hearts', 'spades', 'clubs']: |
30 |
self.csuit = suit
|
31 |
else:
|
32 |
raise Exception("The suit must be a string, and enters the sheet diamonds, hearts, spades, clubs") |
33 |
|
34 |
if type(visible) is bool: |
35 |
self.cvisible = visible
|
36 |
else:
|
37 |
raise Exception("The visibility parameter must be a Boolean value") |
38 |
|
39 |
if type(close) is bool: |
40 |
self.cclose = close
|
41 |
else:
|
42 |
raise Exception("The visibility parameter must be a Boolean value") |
43 |
|
44 |
if type(width) is int: |
45 |
self.cwidth = width
|
46 |
else:
|
47 |
raise Exception("Width must be an integer value") |
48 |
|
49 |
if type(height) is int: |
50 |
self.cheight = height
|
51 |
else:
|
52 |
raise Exception("Height must be an integer value") |
53 |
|
54 |
self.position = [0, 0] |
55 |
|
56 |
@property
|
57 |
def x(self): |
58 |
"""
|
59 |
Card position by x
|
60 |
:return: x integer
|
61 |
"""
|
62 |
return self.position[0] |
63 |
|
64 |
@property
|
65 |
def y(self): |
66 |
"""
|
67 |
Card position by y
|
68 |
:return: y integer
|
69 |
"""
|
70 |
return self.position[1] |
71 |
|
72 |
@x.setter
|
73 |
def x(self, value): |
74 |
"""
|
75 |
Set position x. if type value not integer
|
76 |
raise Exception
|
77 |
:param value: integer
|
78 |
"""
|
79 |
if type(value) is int: |
80 |
self.position[0] = value |
81 |
else:
|
82 |
raise Exception("Position 'x' must be an integer value") |
83 |
|
84 |
@y.setter
|
85 |
def y(self, value): |
86 |
"""
|
87 |
Set position y. if type value not integer
|
88 |
raise Exception
|
89 |
:param value: integer
|
90 |
"""
|
91 |
if type(value) is int: |
92 |
self.position[1] = value |
93 |
else:
|
94 |
raise Exception("Position 'y' must be an integer value") |
95 |
|
96 |
@property
|
97 |
def img(self): |
98 |
"""
|
99 |
Create and return image by format
|
100 |
:return: ATYPE_of_SUIT.png
|
101 |
"""
|
102 |
return "%i_of_%s.png" % (self.catype, self.csuit) |
103 |
|
104 |
@property
|
105 |
def atype(self): |
106 |
"""
|
107 |
This method has been return a type of card
|
108 |
:return: integer
|
109 |
"""
|
110 |
return self.catype |
111 |
|
112 |
@property
|
113 |
def suit(self): |
114 |
"""
|
115 |
This method has been return a suite of card
|
116 |
:return: string
|
117 |
"""
|
118 |
return self.csuit |
119 |
|
120 |
@property
|
121 |
def visible(self): |
122 |
"""
|
123 |
This method has been return a visible of card
|
124 |
:return: boolean
|
125 |
"""
|
126 |
return self.cvisible |
127 |
|
128 |
@visible.setter
|
129 |
def visible(self, value): |
130 |
"""
|
131 |
This method set a visible of card, if value not boolean
|
132 |
raise exception
|
133 |
:param value: bool
|
134 |
"""
|
135 |
if type(value) is bool: |
136 |
self.cvisible = value
|
137 |
else:
|
138 |
raise Exception("The visibility parameter must be a Boolean value") |
139 |
|
140 |
@property
|
141 |
def close(self): |
142 |
"""
|
143 |
This method return closed card or not close
|
144 |
:return: bool
|
145 |
"""
|
146 |
return self.cclose |
147 |
|
148 |
@close.setter
|
149 |
def close(self, close): |
150 |
"""
|
151 |
This method set closed or not closed card. If close not boolean value raise exception
|
152 |
:param close: bool
|
153 |
"""
|
154 |
if type(close) is bool: |
155 |
self.cclose = close
|
156 |
else:
|
157 |
raise Exception("The close parameter must be a Boolean value") |
158 |
|
159 |
# @property
|
160 |
# def clicked(self, x, y):
|
161 |
# """
|
162 |
# This method return clicked in card or not clicked
|
163 |
# if x or y not integer raise exception
|
164 |
# :param x: Integer
|
165 |
# :param y: Integer
|
166 |
# :return: Boolean
|
167 |
# """
|
168 |
# if type(x) is int and type(y) is int:
|
169 |
# if self.x < x < self.x + self.width and self.y < y < self.y + self.height:
|
170 |
# return True
|
171 |
# return False
|
172 |
# raise Exception("Position 'x' and 'y' must be an integer value")
|
173 |
#
|
174 |
# @property
|
175 |
# def width(self):
|
176 |
# """
|
177 |
# This method return a width of card
|
178 |
# :return: integer
|
179 |
# """
|
180 |
# return self.width
|
181 |
#
|
182 |
# @property
|
183 |
# def height(self):
|
184 |
# """
|
185 |
# This method return a height of card
|
186 |
# :return: integer
|
187 |
# """
|
188 |
# return self.height
|
189 |
|
190 |
|
191 |
class Deck: |
192 |
"""
|
193 |
This class is model deck
|
194 |
"""
|
195 |
def __init__(self, cards): |
196 |
self.__cards = cards
|
197 |
|
198 |
@property
|
199 |
def getdiamonds(self): |
200 |
"""
|
201 |
This mwthod return only diamonds card
|
202 |
:return: list of object Card
|
203 |
"""
|
204 |
temp = [] |
205 |
for i in range(len(self.__cards)): |
206 |
if self.__cards[i].atype == "diamonds": |
207 |
temp.append(self.__cards[i])
|
208 |
return temp
|
209 |
|
210 |
@property
|
211 |
def gethearts(self): |
212 |
"""
|
213 |
This method return only spades card
|
214 |
:return: list of object Card
|
215 |
"""
|
216 |
temp = [] |
217 |
for i in range(len(self.__cards)): |
218 |
if self.__cards[i].atype == "hearts": |
219 |
temp.append(self.__cards[i])
|
220 |
return temp
|
221 |
|
222 |
@property
|
223 |
def getspades(self): |
224 |
"""
|
225 |
This method return only spades card
|
226 |
:return: list of object Card
|
227 |
"""
|
228 |
temp = [] |
229 |
for i in range(len(self.__cards)): |
230 |
if self.__cards[i].atype == "spades": |
231 |
temp.append(self.__cards[i])
|
232 |
return temp
|
233 |
|
234 |
@property
|
235 |
def getclubs(self): |
236 |
"""
|
237 |
This method return only clubs card
|
238 |
:return: list of object Card
|
239 |
"""
|
240 |
temp = [] |
241 |
for i in range(len(self.__cards)): |
242 |
if self.__cards[i].atype == "clubs": |
243 |
temp.append(self.__cards[i])
|
244 |
return temp
|
245 |
|
246 |
@property
|
247 |
def mix(self): |
248 |
"""
|
249 |
This method suffle cards in deck
|
250 |
"""
|
251 |
from random import shuffle |
252 |
shuffle(self.__cards)
|
253 |
|
254 |
@property
|
255 |
def get(self): |
256 |
"""
|
257 |
This method return a cards of deck
|
258 |
:return: list of object Card
|
259 |
"""
|
260 |
return self.__cards |