-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScoreCardModel.py
521 lines (396 loc) · 15.8 KB
/
ScoreCardModel.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
"""Score Card object
This module contains code for a yahtzee score card and functions to support calculating scores and analyzing groups of dice
Attributes:
assign_function_lookup (dict):
give a score box name return the function for calculating the points if a roll is assigned to that box. Box names in include:
Aces, Twos, Threes, Fours, Fives, Sixes, 3 of a Kind, 4 of a Kind, Small Straight, Large Straight, Full House, Yahtzee, Chance
score_box_value_lookup (dict):
for looking up the upper section score box applicable to a given dice value. Example: 1 returns Aces
"""
def dice_roll_count(dice_list, value_to_count):
"""Count the number of dice with a certain roll
Args:
dice_list (obj): A list object containing either numbers or dice objects
value_to_count (int): The value to count the occurence of in the dice list
Returns:
int: Returns the number of times the value occurs in the list of dice
"""
return dice_list.count(value_to_count)
def check_yahtzee(dice_list):
"""Does the list of dice contain a Yahtzee
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
bool: returns whether or not the roll is a Yahtzee
"""
roll_counts = [dice_list.count(value) for value in range(1, 7)]
return 5 in roll_counts
def check_full_house(dice_list):
"""Is the roll a full house
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
bool: Returns whether or not the dice list contains a full house
"""
roll_counts = [dice_list.count(value) for value in range(1, 7)]
return 2 in roll_counts and 3 in roll_counts
def of_a_kind_size(dice_list):
"""Returns the largest of a kind size contained in the dice
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the largest of a kind size
"""
return max([dice_list.count(value) for value in range(1,7)])
def straight_size(dice_list):
"""Calculate how long the longest straight in the dice is
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the size of the largest striaght in the dice
"""
# generates list of count dice values 1-6 in order. Any count of 0 breaks the straight
roll_counts = [str(dice_list.count(value)) for value in range(1, 7)]
return len(max(''.join(roll_counts).split('0'), key=len))
# Functions for score boxes
def aces_points(dice_list):
"""Calculate the number of points based on aces in the dice
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the number of points for aces
"""
return dice_list.count(1) * 1
def twos_points(dice_list):
"""Calculate the number of points based on twos in the dice
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the number of points for twos
"""
return dice_list.count(2) * 2
def threes_points(dice_list):
"""Calculate the number of points based on threes in the dice
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the number of points for threes
"""
return dice_list.count(3) * 3
def fours_points(dice_list):
"""Calculate the number of points based on fours in the dice
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the number of points for fours
"""
return dice_list.count(4) * 4
def fives_points(dice_list):
"""Calculate the number of points based on fives in the dice
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the number of points for fives
"""
return dice_list.count(5)* 5
def sixes_points(dice_list):
"""Calculate the number of points based on sixes in the dice
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the number of points for sixes
"""
return dice_list.count(6) * 6
def three_of_a_kind_points(dice_list):
"""Calculate the number of points based on a three of a kind scorebox
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the number of points for a three of a kind score box
"""
if of_a_kind_size(dice_list) >= 3:
return sum(dice_list)
else:
return 0
def four_of_a_kind_points(dice_list):
"""Calculate the number of points based on a four of a kind scorebox
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the number of points for a four of a kind score box
"""
if of_a_kind_size(dice_list) >= 4:
return sum(dice_list)
else:
return 0
def full_house_points(dice_list):
"""Calculate the number of points based on a full house score box
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the number of points for a full house score box
"""
if check_full_house(dice_list) or check_yahtzee(dice_list):
return 25
else:
return 0
def small_straight_points(dice_list):
"""Calculate the number of points based on a small straight score box
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the number of points for a small straight score box
"""
if straight_size(dice_list) >= 4 or check_yahtzee(dice_list):
return 30
else:
return 0
def large_straight_points(dice_list):
"""Calculate the number of points based on a large straight score box
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the number of points for a large straight score box
"""
if straight_size(dice_list) >= 5 or check_yahtzee(dice_list):
return 40
else:
return 0
def yahtzee_points(dice_list):
"""Calculate the number of points based on a Yahtzee score box
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the number of points for a Yahtzee score box
"""
if of_a_kind_size(dice_list) >= 5:
return 50
else:
return 0
def chance_points(dice_list):
"""Calculate the number of points based on a chance score box
Args:
dice_list (obj): A list object containing either numbers or dice objects
Returns:
int: Returns the number of points for a chance score box
"""
return sum(dice_list)
assign_function_lookup = {
'Aces': aces_points,
'Twos': twos_points,
'Threes': threes_points,
'Fours': fours_points,
'Fives': fives_points,
'Sixes': sixes_points,
'3 of a Kind': three_of_a_kind_points,
'4 of a Kind': four_of_a_kind_points,
'Full House': full_house_points,
'Small Straight': small_straight_points,
'Large Straight': large_straight_points,
'Yahtzee': yahtzee_points,
'Chance': chance_points
}
score_box_value_lookup = {
1: 'Aces',
2: 'Twos',
3: 'Threes',
4: 'Fours',
5: 'Fives',
6: 'Sixes'
}
class ScoreBox:
"""A single score box object for implementation in a Yahtzee Score Card
"""
def __init__(self, box_type):
"""
The constructor for a score box
Args:
box_type (str): The type of score box. Utilize one of the following values:
Aces, Twos, Threes, Fours, Fives, Sixes, 3 of a Kind, 4 of a Kind, Small Straight, Large Straight, Full House, Yahtzee, Chance
"""
self._points = 0
self._assigned = False
self._name = box_type
self._calculate_points = assign_function_lookup[box_type]
@property
def name(self):
"""str: return the name of the scorebox"""
return self._name
@property
def points(self):
"""int: return the points contained in the score box"""
return self._points
@property
def assigned(self):
"""bool: Return whether or not the score box already has a roll assigned."""
return self._assigned
def assign_points(self, dice):
"""Assigns a dice roll to the score box and calculates the points
Args:
dice (obj): A List of die objects
"""
if not self.assigned:
self._assigned = True
self._points = self._calculate_points(dice)
def __str__(self):
"""str: Print a description of the score box"""
return self._name + ': ' + str(self._points)
class ScoreCard:
"""A Yahtzee score card
"""
def __init__(self):
"""
The constructor for a Yahtzee score card
"""
self._yahtzee_count = 0
self._aces = ScoreBox('Aces')
self._twos = ScoreBox('Twos')
self._threes = ScoreBox('Threes')
self._fours = ScoreBox('Fours')
self._fives = ScoreBox('Fives')
self._sixes = ScoreBox('Sixes')
self._upperSum = 0
self._upperTotal = 0
self._bonus = 0
self._threeOfAKind = ScoreBox('3 of a Kind')
self._fourOfAKind = ScoreBox('4 of a Kind')
self._fullHouse = ScoreBox('Full House')
self._smallStraight = ScoreBox('Small Straight')
self._largeStraight = ScoreBox('Large Straight')
self._yahtzee = ScoreBox('Yahtzee')
self._chance = ScoreBox('Chance')
self._lowerTotal = 0
self._grandTotal = 0
self._upper_section = [self._aces, self._twos, self._threes, self._fours, self._fives, self._sixes]
self._lower_section = [self._threeOfAKind, self._fourOfAKind, self._fullHouse, self._smallStraight, self._largeStraight, self._yahtzee, self._chance]
self._boxes = [self._aces, self._twos, self._threes, self._fours, self._fives, self._sixes, self._threeOfAKind,
self._fourOfAKind, self._fullHouse, self._smallStraight, self._largeStraight, self._yahtzee, self._chance
]
def print_points(self, box_index):
"""Prints the points of a score box on the score card at the specified index
Args:
box_index (int): The index of the score box
"""
return str(self._boxes[box_index])
def update_points(self):
"""Recalculates the totals on the score card.
"""
#Calculate Upper Section
total = 0
for box in self._upper_section:
total += box.points
self._upperSum = total
if total >= 63:
self._bonus = 35
total += 35
self._upperTotal = total
# Calculate Lower Section
total = 0
for box in self._lower_section:
total += box.points
if self.get_box("Yahtzee").points > 0:
total = total + (self._yahtzee_count - 1) * 100 # Yahtzee Bonus
self._lowerTotal = total
#Total Points
self._grandTotal = self._upperTotal + self._lowerTotal
@property
def total_points(self):
"""int: Get the total points"""
return self._grandTotal
@property
def upper_total_points(self):
"""int: Get the total points in the upper section"""
return self._upperTotal
@property
def bonus_points(self):
"""int: Get the bonus points"""
return self._bonus
@property
def lower_total_points(self):
"""int: Get the total points in the lower section"""
return self._lowerTotal
def get_box(self, box_name):
"""Gets the score box object with the given name
Args:
box_name (str): The name of the box to get. One of the following values:
Aces, Twos, Threes, Fours, Fives, Sixes, 3 of a Kind, 4 of a Kind, Small Straight, Large Straight, Full House, Yahtzee, Chance
Returns:
obj: The score box with the given name
"""
for box in self._boxes:
if box.name == box_name:
return box
def assign_roll(self, box_name, dice):
"""Assign a dice roll to a give score box and calculate the points
Args:
box_name (str): The name of the box to get. One of the following values:
Aces, Twos, Threes, Fours, Fives, Sixes, 3 of a Kind, 4 of a Kind, Small Straight, Large Straight, Full House, Yahtzee, Chance
dice (obj): A list of dice objects
"""
if check_yahtzee(dice):
self._yahtzee_count = self._yahtzee_count + 1
if check_yahtzee(dice) and not self.get_box('Yahtzee').assigned:
self.get_box('Yahtzee').assign_points(dice)
elif check_yahtzee(dice) and not self.get_box(score_box_value_lookup[dice[0].value]).assigned:
self.get_box(score_box_value_lookup[dice[0].value]).assign_points(dice)
else:
self.get_box(box_name).assign_points(dice)
self.update_points()
def get_box_points(self, box_name):
"""Gets the points in the given score box
Args:
box_name (str): The name of the box to get. One of the following values:
Aces, Twos, Threes, Fours, Fives, Sixes, 3 of a Kind, 4 of a Kind, Small Straight, Large Straight, Full House, Yahtzee, Chance, Upper Total, Bonus, Lower Total, Grand Total
Returns:
The current point value in the box
"""
box = self.get_box(box_name)
if box is None:
if box_name == 'Upper Total':
return self._upperTotal
elif box_name == 'Bonus':
return self._bonus
elif box_name == 'Lower Total':
return self._lowerTotal
elif box_name == 'Grand Total':
return self._grandTotal
else:
return box.points
def get_box_assigned(self, box_name):
"""Gets whether or not a given box already has a roll assigned
Args:
box_name (str): The name of the box to get. One of the following values:
Aces, Twos, Threes, Fours, Fives, Sixes, 3 of a Kind, 4 of a Kind, Small Straight, Large Straight, Full House, Yahtzee, Chance, Upper Total, Bonus, Lower Total, Grand Total
Returns:
bool: Whether or not the box already has a value assigned
"""
box = self.get_box(box_name)
if box is None:
if box_name == 'Upper Total':
return True
elif box_name == 'Bonus':
return True
elif box_name == 'Lower Total':
return True
elif box_name == 'Grand Total':
return True
else:
return box.assigned
def __lt__(self, other):
"""bool: Whether the total points are less than a integer"""
return self._grandTotal < other
def __le__(self, other):
"""bool: Whether or not the total points are less than or equal than an integer"""
return self._grandTotal <= other
def __eq__(self, other):
"""bool: Whether or not the total points are equal to an integer"""
return self._grandTotal == other
def __ne__(self, other):
"""bool: Whether or not the total points are not equal to an integer"""
return self._grandTotal != other
def __ge__(self, other):
"""bool: Whether or not the total points are greater than or equal than an integer"""
return self._grandTotal >= other
def __gt__(self, other):
"""bool: Whether or not the total points are greater than than an integer"""
return self._grandTotal > other