-
Notifications
You must be signed in to change notification settings - Fork 0
/
standard_rules.py
365 lines (270 loc) · 11.1 KB
/
standard_rules.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
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 10 14:36:50 2022
@author: Jacob
"""
import numpy as np
import scoring
def standard_rules(num_dice, use_bonus, block_yatzy):
if num_dice not in [5, 6]:
raise ValueError("Standard rules must use 5 or 6 dice")
name = f"standard_{num_dice}dice_bonus{use_bonus}_blockyatzy{block_yatzy}"
ruleset = scoring.Ruleset(name, num_dice, num_rolls=3, use_bonus=use_bonus, bonus_points=50, block_yatzy=block_yatzy)
# 1-6 categories
ruleset.add_categories(eye_categories(num_dice))
# 1 par
category_1par = scoring.Category("1 par", score_pair)
ruleset.add_category(category_1par)
# 2 par
category_2par = scoring.Category("2 par", score_two_pairs)
ruleset.add_category(category_2par)
if num_dice == 6:
# 3 par
category_2par = scoring.Category("3 par", score_three_pairs)
ruleset.add_category(category_2par)
# 3 ens
category_3ens = scoring.Category("3 ens", score_three_of_a_kind)
ruleset.add_category(category_3ens)
# 4 ens
category_4ens = scoring.Category("4 ens", score_four_of_a_kind)
ruleset.add_category(category_4ens)
if num_dice == 6:
# 2 x 3 ens
category_2par = scoring.Category("2x3 ens", score_two_x_three)
ruleset.add_category(category_2par)
# lille straight
category_lille = scoring.Category("Lille straight", score_small_straight)
ruleset.add_category(category_lille)
# stor straight
category_stor = scoring.Category("Stor straight", score_large_straight)
ruleset.add_category(category_stor)
if num_dice == 6:
# royal straight
category_2par = scoring.Category("Royal straight", score_royal_straight)
ruleset.add_category(category_2par)
# hus
category_hus = scoring.Category("Hus", score_house)
ruleset.add_category(category_hus)
# chance
category_chance = scoring.Category("Chance", score_chance)
ruleset.add_category(category_chance)
# YATZY
category_yatzy = scoring.Category("YATZY!", lambda outcome: score_yatzy(outcome, num_dice, 50))
ruleset.add_category(category_yatzy)
return ruleset
def standard_5dice():
return standard_rules(5, True, False)
# num_dice = 5
# num_rolls = 3
# ruleset = scoring.Ruleset("5dice_standard", num_dice, num_rolls, use_bonus=True, bonus_points=50)
# # 1-6 categories
# ruleset.add_categories(eye_categories(num_dice))
# # 1 pair
# category_1par = scoring.Category("1 par", lambda outcome, scoreboard: score_pair(outcome))
# ruleset.add_category(category_1par)
# # 2 pairs
# category_2par = scoring.Category("2 par", lambda outcome, scoreboard: score_two_pairs(outcome))
# ruleset.add_category(category_2par)
# # 3 ens
# category_3ens = scoring.Category("3 ens", lambda outcome, scoreboard: score_three_of_a_kind(outcome))
# ruleset.add_category(category_3ens)
# # 4 ens
# category_4ens = scoring.Category("4 ens", lambda outcome, scoreboard: score_four_of_a_kind(outcome))
# ruleset.add_category(category_4ens)
# # lille straight
# category_lille = scoring.Category("Lille straight", lambda outcome, scoreboard: score_small_straight(outcome))
# ruleset.add_category(category_lille)
# # stor straight
# category_stor = scoring.Category("Stor straight", lambda outcome, scoreboard: score_large_straight(outcome))
# ruleset.add_category(category_stor)
# # hus
# category_hus = scoring.Category("Hus", lambda outcome, scoreboard: score_house(outcome))
# ruleset.add_category(category_hus)
# # chance
# category_chance = scoring.Category("Chance", lambda outcome, scoreboard: score_chance(outcome))
# ruleset.add_category(category_chance)
# # YATZY
# category_yatzy = scoring.Category("YATZY!", lambda outcome, scoreboard: score_yatzy(outcome, num_dice, 50))
# ruleset.add_category(category_yatzy)
# return ruleset
def nobonus_5dice():
num_dice = 5
num_rolls = 3
ruleset = scoring.Ruleset("nobonus_5dice", num_dice, num_rolls, use_bonus=False)
# 1-6 categories
ruleset.add_categories(eye_categories(num_dice))
# 1 pair
category_1par = scoring.Category("1 par", lambda outcome, scoreboard: score_pair(outcome))
ruleset.add_category(category_1par)
# 2 pairs
category_2par = scoring.Category("2 par", lambda outcome, scoreboard: score_two_pairs(outcome))
ruleset.add_category(category_2par)
# 3 ens
category_3ens = scoring.Category("3 ens", lambda outcome, scoreboard: score_three_of_a_kind(outcome))
ruleset.add_category(category_3ens)
# 4 ens
category_4ens = scoring.Category("4 ens", lambda outcome, scoreboard: score_four_of_a_kind(outcome))
ruleset.add_category(category_4ens)
# lille straight
category_lille = scoring.Category("Lille straight", lambda outcome, scoreboard: score_small_straight(outcome))
ruleset.add_category(category_lille)
# stor straight
category_stor = scoring.Category("Stor straight", lambda outcome, scoreboard: score_large_straight(outcome))
ruleset.add_category(category_stor)
# hus
category_hus = scoring.Category("Hus", lambda outcome, scoreboard: score_house(outcome))
ruleset.add_category(category_hus)
# chance
category_chance = scoring.Category("Chance", lambda outcome, scoreboard: score_chance(outcome))
ruleset.add_category(category_chance)
# YATZY
category_yatzy = scoring.Category("YATZY!", lambda outcome, scoreboard: score_yatzy(outcome, num_dice, 50))
ruleset.add_category(category_yatzy)
return ruleset
def gentleman_5dice():
num_dice = 5
num_rolls = 3
ruleset = scoring.Ruleset("5dice_standard", num_dice, num_rolls, use_bonus=True, bonus_points=50)
# 1-6 categories
ruleset.add_categories(eye_categories(num_dice))
# 1 pair
category_1par = scoring.Category("1 par", lambda outcome, scoreboard: score_pair(outcome))
ruleset.add_category(category_1par)
# 2 pairs
category_2par = scoring.Category("2 par", lambda outcome, scoreboard: score_two_pairs(outcome))
ruleset.add_category(category_2par)
# 3 ens
category_3ens = scoring.Category("3 ens", lambda outcome, scoreboard: score_three_of_a_kind(outcome))
ruleset.add_category(category_3ens)
# 4 ens
category_4ens = scoring.Category("4 ens", lambda outcome, scoreboard: score_four_of_a_kind(outcome))
ruleset.add_category(category_4ens)
# lille straight
category_lille = scoring.Category("Lille straight", lambda outcome, scoreboard: score_small_straight(outcome))
ruleset.add_category(category_lille)
# stor straight
category_stor = scoring.Category("Stor straight", lambda outcome, scoreboard: score_large_straight(outcome))
ruleset.add_category(category_stor)
# hus
category_hus = scoring.Category("Hus", lambda outcome, scoreboard: score_house(outcome))
ruleset.add_category(category_hus)
# chance
category_chance = scoring.Category("Chance", lambda outcome, scoreboard: score_chance(outcome))
ruleset.add_category(category_chance)
# YATZY
category_yatzy = scoring.Category("YATZY!", lambda outcome, scoreboard: score_yatzy_gentleman(outcome, num_dice, 50, scoreboard))
ruleset.add_category(category_yatzy)
return ruleset
def eye_categories(num_dice):
categories = []
name = f"1'ere"
score_fnc = lambda outcome: score_type(outcome, 1, num_dice)
categories.append(scoring.Category(name, score_fnc))
name = f"2'ere"
score_fnc = lambda outcome: score_type(outcome, 2, num_dice)
categories.append(scoring.Category(name, score_fnc))
name = f"3'ere"
score_fnc = lambda outcome: score_type(outcome, 3, num_dice)
categories.append(scoring.Category(name, score_fnc))
name = f"4'ere"
score_fnc = lambda outcome: score_type(outcome, 4, num_dice)
categories.append(scoring.Category(name, score_fnc))
name = f"5'ere"
score_fnc = lambda outcome: score_type(outcome, 5, num_dice)
categories.append(scoring.Category(name, score_fnc))
name = f"6'ere"
score_fnc = lambda outcome: score_type(outcome, 6, num_dice)
categories.append(scoring.Category(name, score_fnc))
# print([category.score_fnc(outcome) for category in categories])
return categories
def generate_score_type_fnc(outcome, eye, num_dice):
f = score_type(outcome, eye, num_dice)
return f
def score_type(outcome, eye, num_dice):
return eye*outcome.cumlist[eye - 1] - (num_dice - 2)*eye
def score_pair(outcome):
return score_n_of_a_kind(outcome, 2)
def score_sets(outcome, set_size, num_sets):
score = 0
sets_found = 0
for i, x in reversed(list(enumerate(outcome.cumlist))):
if x >= set_size:
score += set_size*(i + 1)
sets_found += 1
if sets_found == num_sets:
break
if sets_found < num_sets:
score = 0
return score
def score_two_pairs(outcome):
return score_sets(outcome, set_size=2, num_sets=2)
def score_three_pairs(outcome):
return score_sets(outcome, set_size=2, num_sets=3)
def score_two_x_three(outcome):
return score_sets(outcome, set_size=3, num_sets=2)
def score_three_of_a_kind(outcome):
return score_n_of_a_kind(outcome, 3)
def score_four_of_a_kind(outcome):
return score_n_of_a_kind(outcome, 4)
def score_small_straight(outcome):
if all(np.array(outcome.cumlist[:5]) > 0):
return 15
else:
return 0
def score_large_straight(outcome):
if all(np.array(outcome.cumlist[1:6]) > 0):
return 20
else:
return 0
def score_royal_straight(outcome):
if all(np.array(outcome.cumlist) > 0):
return 30
else:
return 0
def score_house(outcome):
pair_found = False
triple_found = False
score = 0
score_pair = 0
score_triple = 0
for i, x in reversed(list(enumerate(outcome.cumlist))):
if x >= 3 and not triple_found:
if pair_found:
score = 3*(i + 1) + score_pair
break
else:
triple_found = True
score_triple = 3*(i + 1)
elif x >= 2 and not pair_found:
if triple_found:
score = 2*(i + 1) + score_triple
break
else:
pair_found = True
score_pair = 2*(i + 1)
return score
def score_chance(outcome):
score = 0
for i, x in enumerate(outcome.cumlist):
score += (i + 1)*x
return score
def score_yatzy(outcome, num_dice, yatzy_points):
if score_n_of_a_kind(outcome, num_dice) > 0:
return score_n_of_a_kind(outcome, num_dice) + yatzy_points
else:
return 0
def score_yatzy_blocked(outcome, num_dice, yatzy_points):
points = score_yatzy(outcome, num_dice, yatzy_points)
if points == 0:
points = -np.inf
return points
def score_n_of_a_kind(outcome, n):
score = 0
for i, x in reversed(list(enumerate(outcome.cumlist))):
if x >= n:
score = n*(i + 1)
break
return score
if __name__ == "__main__":
import dice
r = standard_5dice()