forked from super-aardvark/super-aardvark.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcact.js
More file actions
executable file
·285 lines (269 loc) · 8.95 KB
/
cact.js
File metadata and controls
executable file
·285 lines (269 loc) · 8.95 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
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);