-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathability_stone_calculator.py
254 lines (190 loc) · 7.64 KB
/
ability_stone_calculator.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
from pynput import keyboard
from os import system
def argmax(arr):
f = lambda i: arr[i]
return max(range(len(arr)), key=f)
def full(shape, val = 0):
if len(shape) == 1:
return [val] * shape[0]
else:
return [full(shape[1:]) for _ in range(shape[0])]
chances = [0.25, 0.35, 0.45, 0.55, 0.65, 0.75]
def score(s_1, s_2, s_3):
global w_1
global w_2
return w_1 * s_1 + w_2 * s_2 - s_3 - (abs(s_1 - s_2))
def make_lookup_table(facet_count):
facet_count += 1
dim = range(facet_count)
f = facet_count
# chance, remainingx3, succsessesx3
expected_facets = full([6, f, f, f, f, f, f], -500)
print("Gerenrating Table")
for s_1 in dim:
for s_2 in dim:
for s_3 in dim:
for chance in range(6):
expected_facets[chance][0][0][0][s_1][s_2][s_3] = score(s_1, s_2, s_3)
for r_1 in dim:
print(r_1 / facet_count * 100, " %")
for r_2 in dim:
for r_3 in dim:
if r_1 == r_2 == r_3 == 0:
continue
for s_1 in range(facet_count - r_1)[::-1]:
for s_2 in range(facet_count - r_2)[::-1]:
for s_3 in range(facet_count - r_3)[::-1]:
for chance in range(6):
suc_chance = chances[chance]
fail_chance = 1 - suc_chance
suc_chance_pointer = max(0, chance - 1)
fail_chance_pointer = min(5, chance + 1)
value = 0
if r_1 > 0:
value = max(value, suc_chance * expected_facets[suc_chance_pointer][r_1 - 1][r_2][r_3][s_1 + 1][s_2][s_3]
+ fail_chance * expected_facets[fail_chance_pointer][r_1 - 1][r_2][r_3][s_1][s_2][s_3])
if r_2 > 0:
value = max(value, suc_chance * expected_facets[suc_chance_pointer][r_1][r_2 - 1][r_3][s_1][s_2 + 1][s_3]
+ fail_chance * expected_facets[fail_chance_pointer][r_1][r_2 - 1][r_3][s_1][s_2][s_3])
if r_3 > 0:
value = max(value, suc_chance * expected_facets[suc_chance_pointer][r_1][r_2][r_3 - 1][s_1][s_2][s_3 + 1]
+ fail_chance * expected_facets[fail_chance_pointer][r_1][r_2][r_3 - 1][s_1][s_2][s_3])
expected_facets[chance][r_1][r_2][r_3][s_1][s_2][s_3] = value
return expected_facets
class Stone:
def __init__(self, num_facets: int):
self.num_facets = num_facets
self.chance = 5
self.row1 = []
self.row2 = []
self.row3 = []
def copy(self):
clone = Stone(self.num_facets)
clone.chance = self.chance
clone.row1 = self.row1.copy()
clone.row2 = self.row2.copy()
clone.row3 = self.row3.copy()
return clone
def to_array(self):
return [self.chance, self.num_facets - len(self.row1), self.num_facets - len(self.row2), self.num_facets - len(self.row3), sum(self.row1), sum(self.row2), sum(self.row3)]
def tap1(self, succ: bool):
if len(self.row1) >= self.num_facets:
return
else:
self.row1.append(succ)
self.change_chance(succ)
def tap2(self, succ: bool):
if len(self.row2) >= self.num_facets:
return
else:
self.row2.append(succ)
self.change_chance(succ)
def tap3(self, succ: bool):
if len(self.row3) >= self.num_facets:
return
else:
self.row3.append(succ)
self.change_chance(succ)
def change_chance(self, succ):
if succ:
self.chance = max(0, self.chance - 1)
else:
self.chance = min(5, self.chance + 1)
def string(self):
chances = [25, 35, 45, 55, 65, 75]
res = " success chance: " + str(chances[self.chance]) + " %\n\n "
for i in self.row1:
res += " * " if i else " _ "
res += " "*(self.num_facets - len(self.row1))
res += " " + str(sum(self.row1)) + " Z U\n\n "
for i in self.row2:
res += " * " if i else " _ "
res += " "*(self.num_facets - len(self.row2))
res += " " + str(sum(self.row2)) + " H J\n\n "
for i in self.row3:
res += " * " if i else " _ "
res += " "*(self.num_facets - len(self.row3))
res += " " + str(sum(self.row3)) + " N M\n\n "
res += "\n Backspace: Undo, Delete: Reset, Esc: Page Up"
return res
def get_score(stone, row):
global table
global chances
global num_facets
chance, r_1, r_2, r_3, s_1, s_2, s_3 = stone
suc_chance = chances[chance]
fail_chance = 1 - suc_chance
suc_chance_pointer = max(0, chance - 1)
fail_chance_pointer = min(5, chance + 1)
if r_1 == 0 and row == 0:
return -9
if r_2 == 0 and row == 1:
return -9
if r_3 == 0 and row == 2:
return -9
if row == 0:
return suc_chance * table[suc_chance_pointer][r_1 - 1][r_2][r_3][s_1 + 1][s_2][s_3] + fail_chance * table[fail_chance_pointer][r_1 - 1][r_2][r_3][s_1][s_2][s_3]
if row == 1:
return suc_chance * table[suc_chance_pointer][r_1][r_2 - 1][r_3][s_1][s_2 + 1][s_3] + fail_chance * table[fail_chance_pointer][r_1][r_2 - 1][r_3][s_1][s_2][s_3]
if row == 2:
return suc_chance * table[suc_chance_pointer][r_1][r_2][r_3 - 1][s_1][s_2][s_3 + 1] + fail_chance * table[fail_chance_pointer][r_1][r_2][r_3 - 1][s_1][s_2][s_3]
def on_press(key):
global listener
global stonestates
global num_facets
if key == keyboard.Key.page_up:
print("Exiting")
listener.stop()
return
elif key == keyboard.Key.backspace:
if len(stonestates) > 1:
stonestates = stonestates[:-1]
cur_stone = stonestates[-1].copy()
elif key == keyboard.Key.delete:
stonestates = [Stone(num_facets)]
cur_stone = stonestates[-1].copy()
else:
key = str(key)[1]
cur_stone = stonestates[-1].copy()
if key == "z": #suc 1
cur_stone.tap1(1)
if key == "h": #suc 2
cur_stone.tap2(1)
if key == "n": #suc 3
cur_stone.tap3(1)
if key == "u": #fail 1
cur_stone.tap1(0)
if key == "j": #fail 2
cur_stone.tap2(0)
if key == "m": #fail 3
cur_stone.tap3(0)
stonestates.append(cur_stone)
system("cls")
print(cur_stone.string())
arr = cur_stone.to_array()
scores = [get_score(arr, 0), get_score(arr, 1), get_score(arr, 2)]
print(" ", scores)
print(" Do row: ", argmax(scores) + 1)
w_1 = input("row 1 weight(default=1): ")
if len(w_1) == 0:
w_1 = 1
else:
w_1 = float(w_1)
w_2 = input("row 2 weight(default=1): ")
if len(w_2) == 0:
w_2 = 1
else:
w_2 = float(w_2)
num_facets = int(input("num facets: "))
table = make_lookup_table(num_facets)
stonestates = [Stone(num_facets)]
system("cls")
print(stonestates[-1].string())
arr = stonestates[-1].to_array()
scores = [get_score(arr, 0), get_score(arr, 1), get_score(arr, 2)]
print(scores)
print("Do row: ", argmax(scores) + 1)
listener = keyboard.Listener(on_press=on_press)
listener.start()
listener.join()
input("Press Enter to close the Window")