-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
206 lines (164 loc) · 4.54 KB
/
main.js
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
// n = rows, m = columns
let n = null;
let m = null;
let ok = false;
// Prompt for input
while(!ok) {
n = parseInt(prompt("Enter number of rows: "))
while (!Number.isInteger(n)) {
alert("That was not an integer!")
n = parseInt(prompt("Enter number of rows: "))
}
m = parseInt(prompt("Enter number of columns: "))
while (!Number.isInteger(m)) {
alert("That was not an integer!")
m = parseInt(prompt("Enter number of columns: "))
}
if(Number.isInteger(n) && Number.isInteger(m) && Math.pow(m, n+1) <= 4e8) ok = true;
if(Math.pow(m, n+1) > 4e8 || n > 100 || m > 100) {
alert("Board size too big :(")
}
}
let init = Array(m).fill(n);
console.log(init);
// bool -> winning state (dynamic programming)
// Caching states makes sure we don't iterate over the same state multiple times
let dp = {};
// Probability of picking a losing state
let prob = {};
// Top left is bad
let base = Array(m).fill(0);
base[0] = 1;
dp[base] = false;
prob[base] = 0;
let traversed = 0;
// Recursive depth first search function
dfs = (state) => {
traversed++;
console.assert(traversed < Math.pow(m, n+1));
if(dp.hasOwnProperty(state)) return dp[state];
let res = false;
let tot = 0, win = 0;
for(let i = 0; i < m; i++) {
for(let j = 0; j < state[i]; j++) {
let to = []
for(let k = 0; k < i; k++) {
to.push(state[k]);
}
for(let k = i; k < m; k++) {
to.push(Math.min(state[k], j));
}
let sum = 0;
for(let k = 0; k < m; k++) {
sum += to[k];
}
if(sum === 0) continue
if(!dp.hasOwnProperty(to)) {
dfs(to.slice());
}
tot++;
// if can go to losing
if(dp[to] === false) {
win++;
res = true;
}
}
}
prob[state] = win/tot;
dp[state] = res;
}
dfs(init);
console.log("done precomputing all states")
// Current state
let cur = init;
const sqsz = 50;
let width = sqsz*m;
let height = sqsz*n;
setup = () => {
let cnv = createCanvas(width, height);
cnv.parent('sketch-holder');
}
draw = () => {
background(220);
let r = Math.floor(mouseY/sqsz);
let c = Math.floor(mouseX / sqsz);
// Draw brown
fill(188, 143, 143);
for(let i = 0, j = 0; j < m; i += sqsz, j++) {
rect(i, 0, sqsz, cur[j]*sqsz);
}
// Draw red to indicate what the user will remove
fill('red');
for(let i = 0, j = 0; j < m; i += sqsz, j++) {
if(j >= c) {
rect(i, sqsz*(r), sqsz, (min(n)-r+1)*sqsz);
}
}
fill(188, 143, 143);
// Draw grid
stroke(0);
strokeWeight(1);
for(let i = sqsz; i < width; i += sqsz) {
line(i, 0, i, height);
}
for(let i = sqsz; i < height; i += sqsz) {
line(0, i, width, i);
}
}
mouseClicked = () => {
let r = Math.floor(mouseY/sqsz);
let c = Math.floor(mouseX / sqsz);
// User loses if they take top left
if(r == c && r == 0) {
alert("You lose! Press f5 or reload the page to try again.");
}
// Update state
let sum = 0;
let rem = false;
for(let i = c; i < m; i++) {
if(r < cur[i]) rem = true;
cur[i] = Math.min(cur[i], r);
sum += cur[i];
}
// If the user's "move" didn't actually do anything then return
if(!rem) return;
// Check if user has won
let winningState = Array(m).fill(0);
winningState[0] = 1;
if(JSON.stringify(cur) == JSON.stringify(winningState)) {
alert("You win! Press f5 or reload the page to try again.");
}
// Find best state
let mi = 1e18;
best = [];
// found case with guaranteed win
let foundg = false;
for(let i = 0; i < m; i++) {
for(let j = 0; j < cur[i]; j++) {
let to = []
for(let k = 0; k < i; k++) {
to.push(cur[k]);
}
for(let k = i; k < m; k++) {
to.push(Math.min(cur[k], j));
}
let sum = 0;
for(let k = 0; k < to.length; k++) {
sum += to[k];
}
if(sum == 0) continue;
if(!dp[to]) {
best = to;
foundg = true;
break;
}
if(prob[to] < mi) {
mi = prob[to];
best = to;
}
}
if(foundg) break;
}
cur = best;
draw();
}