-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
366 lines (314 loc) · 11.5 KB
/
Copy pathmain.py
File metadata and controls
366 lines (314 loc) · 11.5 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
from random import randint
class ScoreRound():
rows = [
's1', 's2', 's3', 's4', 's5', 's6', 'kind3', 'kind4', 'full_house',
'sm_strt', 'lg_strt', 'yahtzee', 'chance', 'bonus'
]
rows_pretty = {
's1': ['ones', 'one', '1s', '1'],
's2': ['twos', 'two', '2s', '2'],
's3': ['threes', 'three', '3s', '3'],
's4': ['fours', 'four', '4s', '4'],
's5': ['fives', 'five', '5s', '5'],
's6': ['sixes', 'six', '6s', '6'],
'kind3': ['three of a kind', '3 of a kind'],
'kind4': ['four of a kind', '4 of a kind'],
'full_house': ['full house'],
'sm_strt': [
'small straight', 'sm strt', 'sm straight', 'small strt',
'sm straight'
],
'lg_strt': [
'large straight', 'lg strt', 'lg straight', 'large strt',
'lg straight'
],
'bonus': ['bonus yahtzee', 'yahtzee bonus']
}
def decode_row(field):
field = field.lower()
if field in ScoreRound.rows:
return field
for k in ScoreRound.rows_pretty.keys():
if field in ScoreRound.rows_pretty[k]:
return k
if not field in ScoreRound.rows:
print('Uhoh! "' + field + '" is not a row in our Score Card!')
return None
def row_pretty(field):
field = ScoreRound.decode_row(field)
if field in ScoreRound.rows_pretty.keys():
return ScoreRound.rows_pretty[field][0].title()
return field.title()
def __init__(self, number=1):
self.number = number
for row in ScoreRound.rows:
self.__dict__[row] = None
def score(self):
total = 0
# Upper
for i in range(1, 6):
si = self.__dict__['s' + str(i)]
if si is not None:
total += si
if total >= 63:
total += 35
# Lower
for row in [
'full_house', 'sm_strt', 'lg_strt', 'yahtzee', 'chance',
'bonus'
]:
sl = self.__dict__[row]
if sl is not None:
total += sl
return total
def fill(self, field, score):
field = ScoreRound.decode_row(field)
if field is not None:
if self.__dict__[field] is not None:
print('Nice try! You\'ve already scored "' +
ScoreRound.row_pretty(field) + '"!')
else:
self.__dict__[field] = score
return True
return False
def fill(self, field, dice):
score = 0
field = ScoreRound.decode_row(field)
if field == 's1':
# count the number of ones
score = self.score_num(1, dice)
elif field == 's2':
score = self.score_num(2, dice)
elif field == 's3':
score = self.score_num(3, dice)
elif field == 's4':
score = self.score_num(4, dice)
elif field == 's5':
score = self.score_num(5, dice)
elif field == 's6':
score = self.score_num(6, dice)
elif field == 'kind3':
score = self.get_kind3_score(dice)
elif field == 'kind4':
score = self.get_kind4_score(dice)
elif field == 'full_house':
score = self.get_full_house_score(dice)
elif field == 'sm_strt':
score = self.get_sm_strt_score(dice)
elif field == 'lg_strt':
score = self.get_lg_strt_score(dice)
elif field == 'yahtzee':
score = self.get_yahtzee(dice)
elif field == 'chance':
score = sum(dice)
if field is not None:
if self.__dict__[field] is not None:
print('Nice try! You\'ve already scored "' +
ScoreRound.row_pretty(field) + '"!')
else:
self.__dict__[field] = score
print("Put %d points in %s" % (score, field))
return True
return False
def score_num(self, number, dice):
return sum(number for die in dice if die == number)
def get_kind3_score(self, dice):
num_same = [0] * 6
for die in dice:
num_same[die - 1] += 1
return sum(
dice) if 3 in num_same or 4 in num_same or 5 in num_same else 0
def get_kind4_score(self, dice):
num_same = [0] * 6
for die in dice:
num_same[die - 1] += 1
return sum(dice) if 4 in num_same or 5 in num_same else 0
def get_full_house_score(self, dice):
num_same = [0] * 6
for die in dice:
num_same[die - 1] += 1
return 25 if 3 in num_same and 2 in num_same else 0
def get_sm_strt_score(self, dice):
dice.sort()
straights = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]
for possible in straights:
not_in = False
for num in possible:
if num not in dice:
not_in = True
if not not_in:
return 30
return 0
def get_lg_strt_score(self, dice):
dice.sort()
straights = [[1, 2, 3, 4, 5], [2, 3, 4, 5, 6]]
for possible in straights:
if possible == dice:
return 40
return 0
def get_yahtzee(self, dice):
num_same = [0] * 6
for die in dice:
num_same[die - 1] += 1
return 50 if 5 in num_same else 0
def display_score(self):
header = ['FIELDS: |']
scores = ['SCORES: |']
for field in self.rows:
score = str(self.__dict__[field]) if self.__dict__[field] is not None else '---'
header.append('{0:^10s}|'.format(field))
scores.append('{0:^10s}|'.format(score))
print(''.join(header))
print(''.join(scores))
class TurnState():
def __init__(self):
self.NUM_DICE = 5
self.dice_roll = 0
self.saved_dice = []
def _num_to_roll(self):
return self.NUM_DICE - len(self.saved_dice)
def stop_turn(self):
return self.dice_roll >= 3 or len(self.saved_dice) == self.NUM_DICE
def roll_dice(self):
self.dice_roll += 1
print("Roll number %d" % (self.dice_roll))
num_rolls = self._num_to_roll()
just_rolled = [0] * num_rolls
for die_num in range(num_rolls):
just_rolled[die_num] = randint(1, 6)
self.show_dice(just_rolled)
self.saved_dice += self.get_dice_to_save2(just_rolled)
if self.dice_roll < 3:
print("Saved: " + str(self.saved_dice))
self.get_dice_to_unsave()
def show_dice(self, just_rolled):
print(" [ 1 2 3 4 5 ]")
dice_str = []
length = 0
if len(self.saved_dice) > 0:
dice_str.append("Saved: ")
for die in self.saved_dice:
dice_str.append(" ")
dice_str.append(str(die))
length += 2
dice_str.append("\n")
if len(just_rolled) > 0:
dice_str.append("Rolled: ")
for _ in range(length):
dice_str.append(" ")
for die in just_rolled:
dice_str.append(str(die))
dice_str.append(" ")
dice_str.append("\n")
print(''.join(dice_str))
def get_dice_to_save(self):
nums = input("Enter the number of dice you want to save: ")
nums.strip()
if nums is None or len(nums) == 0:
return []
num_arr = map(int, nums.split(" "))
return [num for num in num_arr if num > len(self.saved_dice)]
def get_dice_to_save2(self, rolled):
"""
Accepts `1 2 3` or `1, 2, 3`
"""
roll_orig = rolled
nums = input("Which dice do you want to save? ")
nums = nums.strip()
if self.dice_roll == 3:
for die in rolled:
self.saved_dice.append(die)
return []
elif nums is None or len(nums) <= 0:
return []
nums = list(
map(
int, "".join(list(filter(lambda c: c in '0123456790 ',
nums))).split(" ")))
if len(nums) > 5 or len(nums) + len(self.saved_dice) > 5:
print("You can't save that many!")
return self.get_dice_to_save2(roll_orig)
keeps = []
def pop_member(dice):
if dice in rolled:
keeps.append(dice)
rolled.remove(dice)
return None
else:
print("You don't have any %ds!" % (dice))
return self.get_dice_to_save2(roll_orig)
for dice in nums:
val = pop_member(dice)
if val is not None:
return val
return keeps
def get_dice_to_unsave(self):
nums = input("Which dice do you want to unsave? ")
nums.strip()
if nums is None or len(nums) <= 0:
return []
nums = list(
map(
int, "".join(list(filter(lambda c: c in '0123456790 ',
nums))).split(" ")))
if len(self.saved_dice) < len(nums):
print("You don't have that many dice!")
return self.get_dice_to_save2(roll_orig)
def pop_member(dice):
if dice in self.saved_dice:
self.saved_dice.remove(dice)
return None
else:
print("You don't have any %ds!" % (dice))
return self.get_dice_to_unsave()
for dice in nums:
pop_member(dice)
def save_rolled(self, just_rolled, indices):
if self.dice_roll == 3:
for die in just_rolled:
self.saved_dice.append(die)
else:
for idx in indices:
idx -= len(self.saved_dice) + 1
dice_score = just_rolled[idx]
just_rolled.remove(dice_score)
self.saved_dice.append(dice_score)
def print_rules():
print("~~~~~~~~Starting the game of Yahtzee~~~~~~~~\n")
print("Okay, here are the rules: ")
print("The game will roll all dice not saved for you. Nice!")
print("You will be prompted to save and unsave dice at the end of a roll")
print(
" Enter the indicies of dice you want to save seperated by spaces!")
print(" eg: \"1 2 5\" will save the first, second, and 5th dice")
print(" (provided they are not already saved!)")
print(" Maximum of only 3 rolls per turn!")
print("Your turn ends when you have saved all your dice, or after the "
"3rd roll")
print("You will then enter your score into the field. Scouts honor!")
print("Game ends when you fill the score card. Good Luck! \n\n")
def get_turn_score(test_turn):
print("\nYour dice from that turn are: ")
print(test_turn.saved_dice)
field = input("Where do you want to put your points? ")
field.strip()
return field
def turn_num(turn):
print("\n~~~~~Turn Number: %d~~~~~" % (turn))
def main():
print_rules()
score = ScoreRound()
turns = []
while len(turns) < 13:
curr_turn = TurnState()
turn_num(len(turns) + 1)
while not curr_turn.stop_turn():
curr_turn.roll_dice()
field = get_turn_score(curr_turn)
while not score.fill(field, curr_turn.saved_dice):
field = get_turn_score(curr_turn)
turns.append(curr_turn)
score.display_score()
print("Game over, you got: " + str(score.score()))
if __name__ == '__main__':
main()