forked from super-aardvark/super-aardvark.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcactpot.html
executable file
·340 lines (321 loc) · 12.1 KB
/
cactpot.html
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
<html>
<head>
<style type="text/css">
td {
border: solid white 2px;
}
td.valueCell{
width: 40px;
height: 20px;
}
.recommended {
border: solid green 2px;
}
.error {
color: red;
}
</style>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js"></script>
<script type="text/javascript">
var Cact = {
setup: function() {
$('input').change(Cact.smartCalc);
},
getState: function() {
var revealedValues = [];
var hiddenIndices = [];
var board = new Array(9);
for (var row = 0; row < 3; row++) {
for (var col = 0; col < 3; col++) {
var index = col + row*3;
var val = parseInt($('input[row="' + row + '"][col="' + col + '"]').val());
if (val > 0) {
revealedValues.push(val);
board[index] = val;
} else {
hiddenIndices.push(index);
}
}
}
console.log(revealedValues);
revealedValues.sort();
var hiddenValues = [1,2,3,4,5,6,7,8,9];
for (var i = revealedValues.length-1; i >= 0; i--) {
hiddenValues.splice(revealedValues[i]-1, 1);
}
console.log(hiddenValues);
var state = new Cact.State();
state.board = board;
state.hiddenValues = hiddenValues;
state.hiddenIndices = hiddenIndices;
return state;
},
smartCalc: function() {
var state = Cact.getState();
if (state == null) {
$('#errorMessage').text("No duplicate values are allowed.");
return;
}
switch (state.hiddenIndices.length) {
case 9:
case 8:
case 7:
case 6:
Cact.recommend();
break;
case 5:
Cact.calc();
break;
default:
$('#errorMessage').text("Only enter values for the spaces you've revealed (Max 4)");
}
},
calc: function() {
Cact.clear();
var state = Cact.getState();
console.log(state);
var averages = state.getAveragePayouts();
var best = 0;
var bestElemId = null;
for (var i = 0; i < averages.length; i++) {
var val = averages[i];
var elemId = Cact.valueIds[i];
$('#' + elemId).text(Math.round(val));
if (val > best) {
best = val;
bestElemId = elemId;
}
}
$('#' + bestElemId).addClass('recommended');
},
recommend: function() {
Cact.clear();
var state = Cact.getState();
var newStates = Cact.permute(state);
var bestAvgScore = 0;
var bestIndex = [];
for (var i = 0; i < state.hiddenIndices.length; i++) {
var idx = state.hiddenIndices[i];
var childStates = newStates[idx];
var totalScore = 0;
for (var j = 0; j < childStates.length; j++) {
var childState = childStates[j];
var maxAvg = childState.getMaxAveragePayout();
totalScore += maxAvg;
}
var avgScore = totalScore / childStates.length;
console.log("Average score for index " + idx + " is " + avgScore);
if (avgScore > bestAvgScore) {
bestAvgScore = avgScore;
bestIndex = [idx];
} else if (avgScore == bestAvgScore) {
bestIndex.push(idx);
}
}
for (var i = 0; i < bestIndex.length; i++) {
idx = bestIndex[i];
var col = idx % 3;
var row = idx / 3 | 0;
$('input[name="input' + col + '' + row + '"]').parent().addClass("recommended");
}
},
getSum: function(array, i1, i2, i3) {
return array[i1] + array[i2] + array[i3];
},
allPayouts: function(array) {
var payouts = [];
payouts.push(payout(getSum(array, 0, 1, 2)));
payouts.push(payout(getSum(array, 3, 4, 5)));
payouts.push(payout(getSum(array, 6, 7, 8)));
payouts.push(payout(getSum(array, 0, 3, 6)));
payouts.push(payout(getSum(array, 1, 4, 7)));
payouts.push(payout(getSum(array, 2, 5, 8)));
payouts.push(payout(getSum(array, 0, 4, 8)));
payouts.push(payout(getSum(array, 2, 4, 6)));
return payouts;
},
payout: function (sum) {
if (sum == 6) return 10000;
if (sum == 7) return 36;
if (sum == 8) return 720;
if (sum == 9) return 360;
if (sum == 10) return 80;
if (sum == 11) return 252;
if (sum == 12) return 108;
if (sum == 13) return 72;
if (sum == 14) return 54;
if (sum == 15) return 180;
if (sum == 16) return 72;
if (sum == 17) return 180;
if (sum == 18) return 119;
if (sum == 19) return 36;
if (sum == 20) return 306;
if (sum == 21) return 1080;
if (sum == 22) return 144;
if (sum == 23) return 1800;
if (sum == 24) return 3600;
return 0;
},
permute: function(state, idx) {
var newStates = [];
if (idx >= 0) {
if (state.board[idx] > 0) {
alert("oops!");
return null;
}
for (var j = 0; j < state.hiddenValues.length; j++) {
var newState = new Cact.State();
newState.copy(state);
newState.board[idx] = state.hiddenValues[j];
newState.hiddenIndices.splice(state.hiddenIndices.indexOf(idx), 1);
newState.hiddenValues.splice(j, 1);
newStates.push(newState);
}
} else {
newStates = {};
for (var i = 0; i < state.hiddenIndices.length; i++) {
var index = state.hiddenIndices[i];
newStates[index] = Cact.permute(state, index);
}
}
return newStates;
},
clear: function() {
$('td').removeClass('recommended');
$('td.valueCell').text('');
$('#errorMessage').text('');
},
clearInput: function() {
$('input').val('0');
},
clearAll: function() {
Cact.clear();
Cact.clearInput();
},
State: function() {
this.board = [];
this.hiddenValues = [];
this.hiddenIndices = [];
this.copy = function(otherState) {
for (var i = 0; i < otherState.board.length; i++) {
this.board.push(otherState.board[i]);
}
for (var i = 0; i < otherState.hiddenValues.length; i++) {
this.hiddenValues.push(otherState.hiddenValues[i]);
}
for (var i = 0; i < otherState.hiddenIndices.length; i++) {
this.hiddenIndices.push(otherState.hiddenIndices[i]);
}
};
this.getPossibleSums = function(row) {
var hiddenSpaces = 0;
var staticSum = 0;
var result = [];
for (var i = 0; i < row.length; i++) {
if (row[i] > 0) {
staticSum += row[i];
} else {
hiddenSpaces++;
}
}
if (hiddenSpaces > 0) {
for (var i = 0; i < this.hiddenValues.length; i++) {
if (hiddenSpaces > 1) {
for (var j = i+1; j < this.hiddenValues.length; j++) {
if (hiddenSpaces > 2) {
for (var k = j+1; k < this.hiddenValues.length; k++) {
result.push(staticSum + this.hiddenValues[i] + this.hiddenValues[j] + this.hiddenValues[k]);
}
} else {
result.push(staticSum + this.hiddenValues[i] + this.hiddenValues[j]);
}
}
} else {
result.push(staticSum + this.hiddenValues[i]);
}
}
} else {
result.push(staticSum);
}
return result;
};
this.getRow = function(i1, i2, i3) {
return [this.board[i1], this.board[i2], this.board[i3]];
};
this.getAllRows = function() {
var rows = [];
rows.push(this.getRow(0, 1, 2));
rows.push(this.getRow(3, 4, 5));
rows.push(this.getRow(6, 7, 8));
rows.push(this.getRow(0, 3, 6));
rows.push(this.getRow(1, 4, 7));
rows.push(this.getRow(2, 5, 8));
rows.push(this.getRow(0, 4, 8));
rows.push(this.getRow(2, 4, 6));
return rows;
};
this.getAveragePayouts = function() {
var result = [];
var rows = this.getAllRows();
for (var i = 0; i < rows.length; i++) {
var sums = this.getPossibleSums(rows[i]);
var total = 0;
for (var j = 0; j < sums.length; j++) {
total += Cact.payout(sums[j]);
}
result.push(total / sums.length);
}
return result;
};
this.getMaxAveragePayout = function() {
var avgs = this.getAveragePayouts();
var max = 0;
for (var i = 0; i < avgs.length; i++) {
if (avgs[i] > max) max = avgs[i];
}
return max;
};
},
valueIds: ['row0Val',
'row1Val',
'row2Val',
'col0Val',
'col1Val',
'col2Val',
'diag0Val',
'diag1Val']
}
$(document).ready(Cact.setup);
</script>
</head>
<body>
<h1>Mini Cactpot Calculator</h1>
<div class="inscrutions">
<h2>Instructions</h2>
<p><ol>
<li>Begin by entering the revealed number on your Mini Cactpot card.</li>
<li>Press enter or tab, and we will recommend one or more numbers to reveal next.</li>
<li>Continue entering the revealed numbers.</li>
<li>Once you have revealed all four numbers, we will display the average payout for each row, and highlight the best row to choose.</li>
</ol>
</p>
</div>
<table>
<tr id="rowa" align="center"><td class="arrowCell"><i class="fa fa-arrows-alt"></i></td><td></td><td class="arrowCell"><i class="fa fa-arrow-down"></i></td><td class="arrowCell"><i class="fa fa-arrow-down"></i></td><td class="arrowCell"><i class="fa fa-arrow-down"></i></td><td></td><td class="arrowCell"><i class="fa fa-arrows-alt"></i></td></th>
<tr id="row0" align="center"><td></td><td id="diag0Val" class="valueCell"></td><td id="col0Val" class="valueCell"></td><td id="col1Val" class="valueCell"></td><td id="col2Val" class="valueCell"></td><td id="diag1Val" class="valueCell"></td></th>
<tr id="row1" align="center"><td class="arrowCell"><i class="fa fa-arrow-right"></i></td><td id="row0Val" class="valueCell"></td><td><input name="input00" row="0" col="0" type="number" max="9" min="0" value="0"/></td><td><input name="input10" row="0" col="1" type="number" max="9" min="0" value="0"/></td><td><input name="input20" row="0" col="2" type="number" max="9" min="0" value="0"/></td></tr>
<tr id="row2" align="center"><td class="arrowCell"><i class="fa fa-arrow-right"></i></td><td id="row1Val" class="valueCell"></td><td><input name="input01" row="1" col="0" type="number" max="9" min="0" value="0"/></td><td><input name="input11" row="1" col="1" type="number" max="9" min="0" value="0"/></td><td><input name="input21" row="1" col="2" type="number" max="9" min="0" value="0"/></td></tr>
<tr id="row3" align="center"><td class="arrowCell"><i class="fa fa-arrow-right"></i></td><td id="row2Val" class="valueCell"></td><td><input name="input02" row="2" col="0" type="number" max="9" min="0" value="0"/></td><td><input name="input12" row="2" col="1" type="number" max="9" min="0" value="0"/></td><td><input name="input22" row="2" col="2" type="number" max="9" min="0" value="0"/></td></tr>
</table>
<button onclick="Cact.clearAll()">Clear</button>
<div id="errorMessage" class="error"></div>
<div class="extra">
<p>You can use these buttons if you're curious about situations that aren't allowed in the game (e.g. revealing more than four numbers).
"Recommend" give a recommendation for which number to reveal next.
"Calculate" shows the expected payouts.
</p>
<button onclick="Cact.calc()">Calculate</button><button onclick="Cact.recommend()">Recommend</button>
</div>
</body>
</html>