-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyahtzeeScoreCalculator.py
More file actions
452 lines (392 loc) · 13.1 KB
/
yahtzeeScoreCalculator.py
File metadata and controls
452 lines (392 loc) · 13.1 KB
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
# Description: Yahtzee Score Calculator is a program that calculates scores
# in the game of Yahtzee for any # of players. Scores are calculated using
# standard yahtzee scoring algorithms and are generated via user given die
# values.
"""
!!! KNOWN BUGS !!!
1. Program will quit if invalid integer value is input during player or
die entry
2.
"""
import random
class PlayerScore:
"""
Holds player score and contains methods to calculate score based on
given die values
"""
def __init__(self):
self._player_scores = [{
# Upper Scores *If sum >= 63 -> Total Score += 35
'aces': None,
'twos': None,
'threes': None,
'fours': None,
'fives': None,
'sixes': None,
}, {
# Lower Scores
'match three': None,
'match four': None,
'match five': None,
'full house': None,
'small straight': None,
'large straight': None,
'choice': None
}]
def calc_aces(self, dice_list: list[int]) -> int:
"""
TODO: Implement
Count and add only aces
:param dice_list:
:return:
"""
total = 0
value = 1
for die in dice_list:
if die == value:
total += value
return total
def calc_twos(self, dice_list: list[int]) -> int:
"""
TODO: Implement
Count and add only twos
:param dice_list:
:return:
"""
total = 0
value = 2
for die in dice_list:
if die == value:
total += value
return total
def calc_threes(self, dice_list: list[int]) -> int:
"""
TODO: Implement
Count and add only threes
:param dice_list:
:return:
"""
total = 0
value = 3
for die in dice_list:
if die == value:
total += value
return total
def calc_fours(self, dice_list: list[int]) -> int:
"""
TODO: Implement
Count and add only fours
:param dice_list:
:return:
"""
total = 0
value = 4
for die in dice_list:
if die == value:
total += value
return total
def calc_fives(self, dice_list: list[int]) -> int:
"""
TODO: Implement
Count and add only fives
:param dice_list:
:return:
"""
total = 0
value = 5
for die in dice_list:
if die == value:
total += value
return total
def calc_sixes(self, dice_list: list[int]) -> int:
"""
TODO: Implement
Count and add only sixes
:param dice_list:
:return:
"""
total_aces = 0
value = 6
for die in dice_list:
if die == value:
total_aces += value
return total_aces
def calc_match_three(self, dice_list: list[int]) -> int:
"""
TODO: docstring
Add total of all three dice
:param dice_list:
:return:
"""
value_range = set()
for die in dice_list:
value_range.add(die)
count_dict = {}
count = 0
for value in value_range:
for die in dice_list:
if value == die:
count += 1
count_dict[value] = count
count = 0
for value in count_dict:
if count_dict[value] >= 3:
return 3 * value
return 0
def calc_match_four(self, dice_list: list[int]) -> int:
"""
TODO: Implement
Add total of all four dice
:param dice_list:
:return:
"""
value_range = set()
for die in dice_list:
value_range.add(die)
count_dict = {}
count = 0
for value in value_range:
for die in dice_list:
if value == die:
count += 1
count_dict[value] = count
count = 0
for value in count_dict:
if count_dict[value] >= 4:
return 4 * value
return 0
def calc_match_five(self, dice_list: list[int]) -> int:
"""
TODO: Implement
Yahtzee: Set 50 points
:param dice_list:
:return:
"""
value_range = set()
for die in dice_list:
value_range.add(die)
count_dict = {}
count = 0
for value in value_range:
for die in dice_list:
if value == die:
count += 1
count_dict[value] = count
count = 0
for value in count_dict:
if count_dict[value] >= 5:
return 50
return 0
def calc_full_house(self, dice_list: list[int]) -> int:
"""
TODO: Implement
Set 25 points
:param dice_list:
:return:
"""
if self.calc_match_four(dice_list) > 0:
return 0
value_range = set()
for die in dice_list:
value_range.add(die)
# check set: if only two values in set then they must be a match 3
# and match 2, therefore full house
if len(value_range) == 2:
return 25
return 0
def calc_straight_small(self, dice_list: list[int]) -> int:
"""
TODO: Implement
Set 30 points
:param dice_list:
:return:
"""
sort_dice = sorted(dice_list)
prev_die = sort_dice[0]
straight_count = 0
for die_index in range(1, len(sort_dice)):
die = sort_dice[die_index]
if prev_die == die-1:
straight_count += 1
prev_die = die
if straight_count >= 3:
return 30
return 0
def calc_straight_large(self, dice_list: list[int]) -> int:
"""
TODO: Implement
Set 40 points
:param dice_list:
:return:
"""
sort_dice = sorted(dice_list)
prev_die = sort_dice[0]
straight_count = 0
for die_index in range(1, len(sort_dice)):
die = sort_dice[die_index]
if prev_die == die-1:
straight_count += 1
prev_die = die
if straight_count >= 4:
return 40
return 0
def calc_choice(self, dice_list: list[int]) -> int:
"""
TODO: Implement
Total of all five dice regardless of number
:param dice_list:
:return:
"""
choice = 0
for die in dice_list:
choice += die
return choice
def display_calcs(self, dice_list: list[int]) -> None:
"""
Based on user input dic_tup prints score calculations in a numbered
list format. Asks for user input and adds score value to total score
(upper and lower score as appropriate). Returns None.
:param dice_list:
:return: None
"""
# upper_list = ['aces', 'twos', 'threes', 'fours', 'fives', 'sixes']
score_dict = [{
# upper scores
'aces': self.calc_aces(dice_list),
'twos': self.calc_twos(dice_list),
'threes': self.calc_threes(dice_list),
'fours': self.calc_fours(dice_list),
'fives': self.calc_fives(dice_list),
'sixes': self.calc_sixes(dice_list),
}, {
# lower scores
'match three': self.calc_match_three(dice_list),
'match four': self.calc_match_four(dice_list),
'match five': self.calc_match_five(dice_list),
'full house': self.calc_full_house(dice_list),
'small straight': self.calc_straight_small(dice_list),
'large straight': self.calc_straight_large(dice_list),
'choice': self.calc_choice(dice_list)
}]
score_choices = []
section_index = 0
for section in score_dict:
for score in section:
if self._player_scores[section_index][score] is None:
print(f'{score}: {section[score]}')
score_choices.append(score)
section_index += 1
# NEED TO REPEAT WHEN SCORE_CHOICE HAS ALREADY BEEN TAKEN
print('')
score_choice = str(input('Which score would you like to add: '
'')).lower()
while score_choice not in score_choices:
score_choice = str(input('Selection not displayed, please '
're-enter which score you would like '
'to add: ')).lower()
# add chosen score to total score
section_index = 0
for section in self._player_scores:
for score in section:
if score == score_choice:
section[score_choice] = score_dict[section_index][score_choice]
section_index += 1
def display_player_score_sheet(self) -> None:
"""
:return:
"""
for section in self._player_scores:
for score in section:
print(f'{score}: {section[score]}')
def display_total_score(self) -> int:
"""
Todo: implement
:return:
"""
total_score = 0
for section in self._player_scores:
for score in section:
if section[score] is not None:
total_score += section[score]
# add bonus if score in upper list is >= 63
if total_score >= 63:
total_score += 35
return total_score
if __name__ == '__main__':
print('\nWelcome to Yahtzee Score Calculator\n')
number_of_players = int(input('Enter # of Players: '))
player_list = []
for player in range(number_of_players):
player_list.append(PlayerScore())
turns_left = len(player_list)*13
player_turn = 0
for player in player_list:
player.display_player_score_sheet()
print(
'------------------------------------------------------------------')
print('1 2 3 4 5 6 GAME START 6 5 4 3 2 1')
print(
'------------------------------------------------------------------')
while turns_left > 0:
############################ START OF TURN ##########################
print(f'\n------------------------------------------------------------------\n'
f' PLAYER #{player_turn+1} TURN START\n'
'------------------------------------------------------------------')
# DICE ROLL INPUT
redo = 'y'
while redo == 'y':
dice_roll = []
for die in range(1, 6):
die_input = int(input(f'Enter die # {die}: '))
while die_input <= 0 or die_input >= 7:
die_input = int(input(f'Invalid Entry Please Enter die #'
f' {die}: '))
dice_roll.append(die_input)
redo = str(input('\nWould you like to enter dice again? [y/n]: '
'')).lower()
# RANDOM DICE ROLL FOR PLAYER
# dice_roll = []
# for die in range(5):
# roll = random.randint(1, 6)
# dice_roll.append(roll)
# print(dice_roll)
print(
f'------------------------------------------------------------------\n'
f' PLAYER {player_turn+1} SCORE '
f'CHOICE\n'
'------------------------------------------------------------------')
# SCORE CALCULATIONS AND CHOICE
player_list[player_turn].display_calcs(dice_roll)
print('')
for player in range(len(player_list)):
print(f'PLAYER #{player+1} TOTAL SCORE: ',
player_list[player].display_total_score())
print(f'\n------------------------------------------------------------------\n'
f' PLAYER #{player_turn+1} TURN END\n'
'------------------------------------------------------------------')
# Decrement turns left
turns_left -= 1
# Change player turn
player_turn+= 1
if player_turn >= len(player_list):
player_turn = 0
############################# END OF TURN ###########################
# WINNER/TIE DECLARATION
winner = []
winner_score = 0
for player in range(len(player_list)):
if player_list[player].display_total_score() > winner_score:
winner = [player+1]
winner_score = player_list[player].display_total_score()
elif player_list[player].display_total_score() == winner_score:
winner_score = player_list[player].display_total_score()
winner.append(player+1)
if len(winner) > 1:
print('Players ')
for player in winner:
print('#',player, ', ')
print('TIE')
print(f'Total Score = {winner_score}')
else:
print(f'Player #{winner[0]} WINS')
print(f'Player #{winner[0]} Score: {winner_score}')