-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoring.py
178 lines (139 loc) · 4.42 KB
/
scoring.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
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 25 13:38:36 2022
@author: Jacob
"""
import numpy as np
import standard_rules
import copy
class Ruleset:
def __init__(self, name, num_dice, num_rolls, use_bonus, bonus_points=50, yatzy_points=50, block_yatzy=False):
self.categories = []
self.name = name
self.num_dice = num_dice
self.num_rolls = num_rolls
self.use_bonus = use_bonus
self.bonus_points = bonus_points
self.block_yatzy = block_yatzy
self.yatzy_points = yatzy_points
def add_category(self, category):
self.categories.append(category)
def add_categories(self, categories):
import dice
outcome = dice.Dice(eyelist=[1,2,3,4,5])
# print(categories)
self.categories.extend(categories)
def num_categories(self):
return len(self.categories)
def generate_yatzyblocked_categories(self):
categories = copy.deepcopy(self.categories)
categories[-1].score_fnc = lambda outcome: standard_rules.score_yatzy_blocked(outcome, self.num_dice, self.yatzy_points)
return categories
class Category:
def __init__(self, name, score_fnc):
self.name = name
self.score_fnc = score_fnc
def calculate_score(outcome, category):
if category == 0:
score = score_type(outcome, 1)
elif category == 1:
score = score_type(outcome, 2)
elif category == 2:
score = score_type(outcome, 3)
elif category == 3:
score = score_type(outcome, 4)
elif category == 4:
score = score_type(outcome, 5)
elif category == 5:
score = score_type(outcome, 6)
elif category == 6:
score = score_pair(outcome)
elif category == 7:
score = score_two_pairs(outcome)
elif category == 8:
score = score_three_of_a_kind(outcome)
elif category == 9:
score = score_four_of_a_kind(outcome)
elif category == 10:
score = score_small_straight(outcome)
elif category == 11:
score = score_large_straight(outcome)
elif category == 12:
score = score_house(outcome)
elif category == 13:
score = score_chance(outcome)
elif category == 14:
score = score_yatzy(outcome)
return score
def score_type(outcome, n):
return n*outcome.cumlist[n - 1] - 3*n
def score_pair(outcome):
return score_n_of_a_kind(outcome, 2)
def score_two_pairs(outcome):
score = 0
first_pair_found = False
score_first_pair = 0
for i, x in reversed(list(enumerate(outcome.cumlist))):
if x >= 2:
if first_pair_found:
score = 2*(i + 1) + score_first_pair
break
else:
first_pair_found = True
score_first_pair = 2*(i + 1)
return score
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_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):
if score_n_of_a_kind(outcome, 5) > 0:
return score_n_of_a_kind(outcome, 5) + 50
else:
return 0
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__":
cat = Category("1 par", lambda dice, category: score_pair(dice))