-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI-Game.py
396 lines (313 loc) · 11.8 KB
/
GUI-Game.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# use __repr__ for debugging of __str__ and __add__
"""Authors: Joanna Kus and Tristan Blus
cool game file lets you do cool things like play card games
"""
from Enums.Rank import *
from Hand import *
from Deck import *
from Rules import *
from Enums.Sort_Methods import *
from Error import *
import GameTest
import unittest
import tkinter as tk
class Game(tk.Frame):
"""CLASS: Stores information about the whole game and controls it.
METHODS:
>> sort_method(self)
Gets the sort type of the hand.
>> empty_hands(self)
Counts the number of empty hands.
>> active_hand(self)
Gets the currently playing hand.
>> init_hand_size(self)
Gets number of cards in a hand at the start of the game.
>> init_hand_size(self, handsize)
Sets number of cards in a hand at the start of the game.
>> hand_count(self)
Gets number of hands in the game.
>> hand_count(self, handcount)
Sets number of hands in the game.
>> max_hand_size(self)
Gets maximum number of allowed cards in a hand.
>> max_hand_size(self, handsize)
Sets maximum number of allowed cards in a hand.
>> hands(self)
Gets dictionary of hands in the game.
>> ace_rank(self)
Gets the game's ace rank.
>> ace_rank(self, acerank)
Sets the game's ace rank.
>> discard_type(self)
Gets the discard pile's visibility.
>> discard_type(self, discardtype)
Sets the discard pile's visibility.
>> deck(self)
Gets the active deck object.
>> __init__(self, hand_size = 0, hand_count = 0, max_hand_size = 0, ace_rank = None, discard_type = None, sort = None)
Initializes the game object.
>> control_players(self)
Executes main control flow for player turns.
>> get_player_ask(self, active)
Gets the hand the player wants to request a card from.
"""
@property
def sort_method(self):
"""GETTER: gets the sort type of the hand
RETURNS
@sort_type (Sort): the way cards will be sorted
"""
return self._sort
@property
def log_file_name(self):
"""GETTER: gets the file name for the current log file
RETURNS
@_log_file: the name of the file to be logged in
"""
return self._log_file
@property
def empty_hands(self):
"""GETTER: Counts the number of hands that are empty
RETURNS
@count (int): the number of empty hands in the set of self._hands
"""
for hand in self._hands:
if self.hands[hand][0].card_count == 0:
self._empty_hands += 1
return self._empty_hands
@property
def active_hand(self):
"""GETTER: Returns the hand which is currently playing
RETURNS
@active (Hand): The hand currently playing
"""
return self._active
@property
def init_hand_size(self):
"""GETTER: Returns the number of cards in the hand to start the game
RETURNS
@init_hand_size (int): the number of cards in the hand to start the game
"""
return self._init_hand_size
@init_hand_size.setter
def init_hand_size(self, handsize):
"""SETTER: Sets the number of cards in the hand to start the game
PARAMETERS
@handsize (int): integer count of cards per hand to start a game
RAISES
TypeError if init hand size is not an integer
"""
try:
handsize = int(handsize)
self._init_hand_size = handsize
except:
Error.log_error(sys.exc_info()[1], "Game.init_hand_size")
@property
def hand_count(self):
"""GETTER: Returns the number of hands in the game
RETURNS
@count (int): the number of hands to be generated for self._hands
"""
return self._hand_count
@hand_count.setter
def hand_count(self, handcount):
"""SETTER: Sets the number of cards in the hand to start the game
PARAMETERS
@handcount (int): integer count of hands in self._hands to start a game
RAISES
TypeError if handcount is not an integer
"""
try:
handcount = int(handcount)
self._hand_count = handcount
except:
Error.log_error(sys.exc_info()[1], "Game.hand_count")
@property
def max_hand_size(self):
"""GETTER: Returns the maximum number of cards allowed per hand
RETURNS
@max_hand_size (int): the maximum number of cards in a hand
"""
return self._max_hand_size
@max_hand_size.setter
def max_hand_size(self, handsize):
"""SETTER: Sets the maximum number of cards allowed per hand
PARAMETERS
@handsize (int): integer maximum number of cards in a hand
RAISES
TypeError if hand size is not an integer
"""
try:
handsize = int(handsize)
self._max_hand_size = handsize
except:
Error.log_error(sys.exc_info()[1], "Game.max_hand_size")
@property
def hands(self):
"""GETTER: Returns the dictionary of hands in the game
RETURNS
@hands (Dictionary): hand name as the key, and a list of the hand
and the player's score as the value
"""
return self._hands
@property
def ace_rank(self):
"""GETTER: Gets the ace rank to be used for the game
RETURNS
@ace_rank (Rank): determines whether ace will be ACELOW or ACEHIGH
"""
return self._acerank
@ace_rank.setter
def ace_rank(self, acerank):
"""SETTER: Sets the ace rank to be used for the game
PARAMETERS
@acerank (Rank): rank value of Rank.ACEHIGH or Rank.ACELOW
RAISES
TypeError if acerank is not of type Rank
>>
"""
try:
assert type(AceRank) == Rank
self._acerank = acerank
except:
Error.log_error(sys.exc_info()[1], "Game.init_hand_size")
@property
def discard_type(self):
"""GETTER: gets the type of visibility for the discard pile for the game
RETURNS
discard_type (Visibility): the visibility of the discard pile
"""
return self._discard_type
@discard_type.setter
def discard_type(self, discardtype):
"""SETTER: Sets the type of visibility for the discard pile
PARAMETERS
discardtype (Visibility): the visibility for the discard pile
RAISES
TypeError if discardtype is not Visibility
"""
try:
assert type(discardtype) == Visibility
self._discard_type = discardtype
except:
Error.log_error(sys.exc_info()[1], "Game.discard_type")
@property
def deck(self):
"""GETTER: Returns the active deck object for the game instantiation
RETURNS
@deck (Deck): The deck object which the game will use
"""
return self._deck
def __init__(self, hand_size=0, hand_count=0, max_hand_size=0, ace_rank=None, discard_type=None, sort=None):
"""Initializes the game object"""
self._init_hand_size = 0
self._hand_count = 0
self._max_hand_size = 52
self._acerank = Rank.ACELOW
self._discard_type = Visibility.INVISIBLE
self._hands = []
self._deck = []
self._empty_hands = 0
self._active = None
self._sort = Sort.SUITTHENRANKD
self._log_file = Error.log_error("Game initalized", "Game.__init__")
"""Instantiating properties"""
if hand_size != 0 and type(hand_size) == int:
self._init_hand_size = hand_size
if hand_count != 0 and type(hand_count) == int:
self._hand_count = hand_count
if max_hand_size != 0 and type(max_hand_size) == int:
self._max_hand_size = max_hand_size
if ace_rank != None and type(ace_rank) == Rank:
self._acerank = ace_rank
if discard_type != None and type(discard_type) == Visibility:
self._discard_type = discard_type
if sort != None and type(sort) == Sort:
self._sort = sort
"""Building the deck"""
deck = Deck(ace_rank=self.ace_rank)
self._deck = deck
deck.shuffle()
temp_hands = {}
"""Dealing the hands"""
if self._hand_count != 0 and self._hands == []:
for i in range(self.hand_count):
hand = Hand(visibility=self.discard_type, sort=self.sort_method)
for j in range(self.init_hand_size):
card = deck.deal()
hand.add_card(card)
name = "hands%s" % i
score = 0
temp_hands[name] = [hand, score]
self._hands = temp_hands
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Hello, world")
label.pack()
label.bind("<1>", self.quit)
def control_players(self):
"""Executes main control flow for player turns
RAISES
Exception if one occurs
"""
try:
rules = Rules(self.deck, 0)
same_player = True
wincondition = False
while wincondition == False:
for hand in self.hands:
same_player = True
self._active = self.hands[hand][0]
while same_player == True:
print("you: %s" % hand)
choice = self.choose_hand(hand)
self.hands[hand][0].visi_override()
if rules.play_game(self.hands[hand][0], self.hands[choice][0]) == False:
same_player = False
self.hands[hand][1] += rules.points
else:
self.hands[hand][1] += rules.points
same_player = True
if self.empty_hands == self.hand_count:
wincondition = True
except:
Error.log_error(sys.exc_info()[1], "Game.control_players()")
def choose_hand(self, active):
"""Gets the hand the player would like to request a card from
RETURNS
name (string): the name of the hand to be asked
RAISES
ValueError if name is not in self._hands
ValueError if name is equal to the active hand
"""
try:
ask = True
while ask == True:
for hand in self.hands:
print(str(hand) + " = " + str(self.hands[hand][0]) + " --- points = " + str(self.hands[hand][1]))
choice = int(input("Enter the index of the hand you would like to ask"))
hand = "hands%s" % choice
if hand in active:
print("Cannot ask yourself for a card")
elif self._hands[hand]:
ask = False
return hand
else:
print("Cannot ask for a hand which does not exist")
except:
Error.log_error(sys.exc_info()[1], "Game.choose_hand()")
####################################
def quit(self, event=None):
sys.exit()
def main():
# print("Unit test: \n")
# suite = unittest.TestLoader().loadTestsFromModule(GameTest)
# unittest.TextTestRunner().run(suite)
game = Game(hand_size=5,
hand_count=4,
max_hand_size=52,
discard_type=Visibility.INVISIBLE,
ace_rank=Rank.ACELOW,
sort=Sort.RANKTHENSUITA)
game.control_players()
if __name__ == '__main__':
main()