-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfarklewithclasses.py
More file actions
324 lines (276 loc) · 8.34 KB
/
farklewithclasses.py
File metadata and controls
324 lines (276 loc) · 8.34 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
import random
import numpy as np
import matplotlib.pyplot as plt
STRATEGY = 3
class Farkle:
def __init__(self, cashout_threshold=300, score_target=10000, strategy=1):
self.cashout_threshold = cashout_threshold
self.score_target = score_target
self.bank = 0
self.total_score = 0
self.turns = 0
self.dice_left = 6
self.cashout_status = 0
self.busted = 0
self.hand = []
self.strategies = {
1:"Take-All",
2:"Minimum Fives",
3:"Minimum Fives and Ones"
}
self.strategy = strategy
# Points
self.four_of_a_kind_points = 1000
self.five_of_a_kind_points = 2000
self.six_of_a_kind_points = 3000
self.straight_points = 1500
self.three_pairs_points = 1500
self.two_triples_points = 2500
def reset_game(self):
self.total_score = 0
self.turns = 0
self.busted = False
self.dice_left = 6
self.bank = 0
self.cashout_status = 0
def roll_die(self):
return random.choice([1,2,3,4,5,6])
def roll_hand(self):
self.hand = [self.roll_die() for i in range(self.dice_left)]
def count_hand(self):
counts = {i: self.hand.count(i) for i in range(1,7)}
return counts
def check_roll_take_all(self):
counts = self.count_hand()
roll_score = 0
dice_used = 0
# straight
if list(counts.values()).count(1) == 6:
roll_score += (self.straight_points)
dice_used = 6
# six of a kind
elif list(counts.values()).count(6) == 1:
roll_score += (self.six_of_a_kind_points)
dice_used = 6
# 3 pairs
elif list(counts.values()).count(2) == 3:
roll_score += (self.three_pairs_points)
dice_used = 6
# 2 triples
elif list(counts.values()).count(3) == 2:
roll_score += (self.two_triples_points)
dice_used = 6
else:
for n in counts:
c = counts[n]
if c < 3:
if n == 1:
roll_score += c*100
dice_used += c
elif n == 5:
roll_score += c*50
dice_used += c
elif c == 3:
if n == 1:
roll_score += 300
dice_used += c
else:
roll_score += n*100
dice_used += c
elif c==4:
roll_score += self.four_of_a_kind_points
dice_used += c
elif c==5:
roll_score += self.five_of_a_kind_points
dice_used += c
if roll_score == 0:
self.busted = True
self.bank = 0
return
self.dice_left -= dice_used
if self.dice_left == 0:
self.dice_left = 6
self.bank += roll_score
if self.bank >= self.cashout_threshold or self.total_score + self.bank >= self.score_target:
self.cashout_status = 1
def check_roll_no_fives(self):
counts = self.count_hand()
roll_score = 0
dice_used = 0
points_taken = False
# straight
if list(counts.values()).count(1) == 6:
roll_score += (self.straight_points)
dice_used = 6
points_taken = True
# six of a kind
elif list(counts.values()).count(6) == 1:
roll_score += (self.six_of_a_kind_points)
dice_used = 6
points_taken = True
# 3 pairs
elif list(counts.values()).count(2) == 3:
roll_score += (self.three_pairs_points)
dice_used = 6
points_taken = True
# 2 triples
elif list(counts.values()).count(3) == 2:
roll_score += (self.two_triples_points)
dice_used = 6
points_taken = True
else:
for n in counts:
c = counts[n]
if c == 3:
if n == 1:
roll_score += 300
dice_used += c
points_taken = True
else:
roll_score += n*100
dice_used += c
points_taken = True
elif c==4:
roll_score += self.four_of_a_kind_points
dice_used += c
points_taken = True
elif c==5:
roll_score += self.five_of_a_kind_points
dice_used += c
points_taken = True
elif c < 3 and c > 0:
if n == 1:
roll_score += c*100
dice_used += c
points_taken = True
if points_taken == False:
if counts[5] > 0:
roll_score += 50
dice_used += 1
if roll_score == 0:
self.busted = True
self.bank = 0
return
self.dice_left -= dice_used
if self.dice_left == 0:
self.dice_left = 6
self.bank += roll_score
if self.bank >= self.cashout_threshold or self.total_score + self.bank >= self.score_target:
self.cashout_status = 1
def check_roll_no_fives_min_ones(self):
counts = self.count_hand()
roll_score = 0
dice_used = 0
points_taken = False
# straight
if list(counts.values()).count(1) == 6:
roll_score += (self.straight_points)
dice_used = 6
points_taken = True
# six of a kind
elif list(counts.values()).count(6) == 1:
roll_score += (self.six_of_a_kind_points)
dice_used = 6
points_taken = True
# 3 pairs
elif list(counts.values()).count(2) == 3:
roll_score += (self.three_pairs_points)
dice_used = 6
points_taken = True
# 2 triples
elif list(counts.values()).count(3) == 2:
roll_score += (self.two_triples_points)
dice_used = 6
points_taken = True
else:
for n in counts:
c = counts[n]
if c == 3:
if n == 1:
roll_score += 100
dice_used += 1
points_taken = True
else:
roll_score += n*100
dice_used += c
points_taken = True
elif c==4:
roll_score += self.four_of_a_kind_points
dice_used += c
points_taken = True
elif c==5:
roll_score += self.five_of_a_kind_points
dice_used += c
points_taken = True
elif c < 3 and c > 0:
if n == 1:
roll_score += 100
dice_used += 1
points_taken = True
if points_taken == False:
if counts[5] > 0:
roll_score += 50
dice_used += 1
if roll_score == 0:
self.busted = True
self.bank = 0
return
self.dice_left -= dice_used
if self.dice_left == 0:
self.dice_left = 6
self.bank += roll_score
if self.bank >= self.cashout_threshold or self.total_score + self.bank >= self.score_target:
self.cashout_status = 1
def play_round(self):
self.bank = 0
self.dice_left = 6
self.busted = False
self.cashout_status = 0
while not self.busted and not self.cashout_status:
self.roll_hand()
if self.strategy == 1:
self.check_roll_take_all()
elif self.strategy == 2:
self.check_roll_no_fives()
elif self.strategy == 3:
self.check_roll_no_fives_min_ones()
if not self.busted:
self.total_score += self.bank
self.turns += 1
def play_game(self):
self.reset_game()
while self.total_score < self.score_target:
self.play_round()
return self.turns, self.total_score
thresholds = range(100, 1050, 50) # example thresholds from 100 to 1800 by 200
num_games = 10000 # number of simulated games per threshold
results = []
for threshold in thresholds:
turns_list = []
for _ in range(num_games):
game = Farkle(cashout_threshold=threshold, strategy=STRATEGY)
turns, score = game.play_game()
turns_list.append(turns)
avg_turns = np.mean(turns_list)
results.append((threshold, avg_turns))
print(f"Threshold {threshold}: Average turns to reach {game.score_target} = {avg_turns:.2f}")
# Plot results
thresholds, avg_turns = zip(*results)
plt.plot(thresholds, avg_turns, marker='o')
plt.xlabel('Bank Threshold')
plt.ylabel(f'Average Turns to Reach {game.score_target} Points')
plt.title(f'Farkle Risk Threshold Effects with {game.strategies[game.strategy]} Strategy - (N={num_games})')
# Take All Plots
# plt.annotate("Chicken Valley",
# xy=(275, 23.5),
# xytext=(200, 28),
# arrowprops=dict(arrowstyle='->'),
# fontsize=12,
# color='black')
# plt.annotate("Mt. Bust",
# xy=(950, 30),
# xytext=(750, 26),
# arrowprops=dict(arrowstyle='->'),
# fontsize=12,
# color='black')
plt.show()