-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate.html
373 lines (356 loc) · 11.9 KB
/
rotate.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=600,height=650'>
<title>rot</title>
<style>
html{background-color:black;color:white;}
body{text-align:center;}
#board
{
position:relative;
background-color:#101020;
font-size:50px;
width:8em;
height:8em;
transition:width 0.2s,height 0.2s;
transition-timing-function:ease-in-out;
overflow:hidden;
margin-left:auto;
margin-right:auto;
-moz-user-select:none;-webkit-user-select:none;
}
#board .piece
{
transition:left 0.2s,top 0.2s,color 0.2s,transform 0.2s;
transition-timing-function:ease-in-out;
position:absolute;
width:1.52em;
height:1.52em;
line-height:1.52em;
font-family:"Ubuntu Mono";
font-weight:bold;
margin:0.2em;
border:0.04em solid #444;
border-radius:0.5em;
border-bottom-color:#6dd;
background-color:black;
z-index:0;
}
#board .piece[data-correct='2']{color:#9f9;}/*correct position and orientation*/
#board .piece[data-correct='1']{color:#9df;}/*correct position but not orientation*/
#board .piece[data-correct='0']{color:#fff;}/*wrong position*/
#board .piece[data-sessile='true']{border-color:black;color:#444;}/*sessile piece*/
#board .interface
{
position:absolute;
top:0;
left:0;
opacity:0;
width:100%;
height:100%;
border:none;
padding:0;
cursor:default;
z-index:1;
}
button{font-family:"Droid Sans";}
</style>
</head>
<body>
<div id='board'></div>
<div>
<button onclick='mouse_enabled = !mouse_enabled;I.focus();'>toggle mouse input</button>
<button onclick='touch_enabled = !touch_enabled;I.focus();'>toggle drag input</button>
<button onclick='B.style.fontSize = prompt("font size","50px");'>font size</button><br>
<button onclick='orient_enabled = !orient_enabled;redraw();I.focus();'>toggle orientation</button>
<button onclick='scramble();redraw();I.focus();'>scramble</button>
<button onclick='autosolve();autoplay(200);'>autosolve</button>
<button onclick='size=4;init();I.focus();'>4</button>
<button onclick='size=5;init();I.focus();'>5</button>
<button onclick='size=6;init();I.focus();'>6</button>
<button onclick='size=+prompt("size",5);init();'>n</button>
</div>
<p>Controls: num pad or {7, 8, 9, u, i, o, j, k, l} or mouse click or drag (size 4 only);<br>holding Shift (except if using num pad with num lock on) or right-clicking rotates anticlockwise.</p>
<script src='rotate.solver.js'></script>
<script>
'use strict';
// >>>>global variables
var B = document.getElementById('board');
var I;
var divs = [];
var size = 4;
var state = [];
var state_o = [];
var mouse_enabled = true;
var touch_enabled = true;
var orient_enabled = true; // this only controls whether the orientation is visible (and the autosolver)
if (!window.count) window.count = function (n) {
var a = [];
for (var i = 0; i < n; i++) a[i] = i;
return a;
}; // this should be loaded with the solver but in case we're on a lame browser that doesn't load that script correctly...
function init()
{
if (size < 4) {console.log('size='+size+' too small'); size = Math.sqrt(state.length); return;}
var n = divs.length;
state = count(size*size);
state_o = state.map(function () {return 0;});
var labels = size*size <= 25 ? 'abcdefghijklmnopqrstuvwxy' : size*size === 36 ? '0123456789abcdefghijklmnopqrstuvwxyz' : state;
while (n < size*size)
{
var div = document.createElement('div');
div.className = 'piece';
//div.textContent = labels[n];
B.appendChild(div);
divs.push(div);
n++;
}
while (n > size*size)
{
B.removeChild(divs.pop());
n--;
}
for (var i = 0; i < n; i++)
{
divs[i].textContent = labels[i];
divs[i].dataset.sessile = '' + (i%size >= 3 && i%size < size-3 && i >= 3*size && i < (size-3)*size);
}
redraw();
if(B.querySelector('button.interface') === null)
{
I = document.createElement('button');
I.className = 'interface';
B.appendChild(I);
I.addEventListener('click',click,false);
I.addEventListener('contextmenu',click,false);
//I.addEventListener('dblclick',function (e) {e.preventDefault();},false);
I.addEventListener('touchstart',touch,false);
I.addEventListener('touchend',touch,false);
I.addEventListener('touchmove',touch,false);
I.addEventListener('touchcancel',touch,false);
I.addEventListener('mousedown',touchemu,false);//touch emulation handlers
I.addEventListener('mouseup',touchemu,false);
I.addEventListener('mousemove',touchemu,false);
I.addEventListener('mouseout',touchemu,false);
I.addEventListener('keydown',key,false);
}
I.focus();
B.style.width = B.style.height = 2*size+'em';
}
function scramble()
{
state = count(size*size);
var indices = state.filter(function (n) {var x = n%size, y = (n-x)/size; return x < 3 || size-x <= 3 || y < 3 || size-y <= 3; });
for (var i = 1; i < indices.length; i++)
{
var r = Math.floor(Math.random()*(i+1));
state[indices[i]] = state[indices[r]];
state[indices[r]] = indices[i];
}
if (orient_enabled && size > 6) console.log('note: scrambling for oriented mode not yet proven to be solvable for size > 6');
function parity(n) {var x = n%size; return x+(n-x)/size;}
var s = 0;
for (var i = 0; i < indices.length; i++)
{
var j = indices[i], k = state[j];
if (i < indices.length-1)
{
state_o[k] = (parity(j) + parity(k) + 2*Math.floor(Math.random()*(2*size-6)))%(4*size-12);
s += state_o[k];
}
else {state_o[k] = ((-s)%(4*size-12)+4*size-12)%(4*size-12);}
}
}
function redraw()
{
for (var i = 0; i < size; i++) for (var j = 0; j < size; j++)
{
var x = state[i+j*size]%size, y = (state[i+j*size]-x)/size;
divs[x+y*size].style.left = i*2+'em';
divs[x+y*size].style.top = j*2+'em';
var angle = (state_o[x+y*size]/(4*size-12))*6.283185307179586;
var c = Math.cos(angle).toFixed(4), s = Math.sin(angle).toFixed(4);
// this needs to be rounded because Chrome is dumb and doesn't support scientific
// notation in CSS
var transform = (navigator.userAgent.indexOf('AppleWebKit') > 0) ?
'matrix('+[c,s,-s,c,0,0]+')' :
('rotate('+(state_o[x+y*size]*90/(size-3))+'deg)');
/* Chrome is also the only browser that currently handles matrix interpolation
correctly :V (and of course there's no way to check if a browser does this correctly
short of actually trying to animate it and monitoring with checkComputedStyle but
holy shit that sounds like effort; web dev sucks forever) */
divs[x+y*size].style.transform = orient_enabled ? transform : '';
divs[x+y*size].dataset.correct = (i === x && j === y)*(1+ +(state_o[x+y*size]%(4*size-12) === 0 || !orient_enabled));
}
}
function move(x,y,ccw)
{
var i = x+size*y;
var t = state[i];
for (var j = 0; j < 4; j++)
{
var d = [size,1,-size,-1][j^ccw];
for (var k = 0; k < size-3; k++)
{
state_o[state[i]] += ccw ? -1 : 1;
state[i] = state[i+d];
i += d;
}
}
state[i-d] = t;
}
function autoplay(delay)
{
if (autoqueue.length === 0) return;
else
{
var m = autoqueue.shift();
var x = m%3, y = (m-x)/3%3, ccw = Math.floor(m/9);
move(x,y,ccw);
redraw();
setTimeout(autoplay.bind(undefined,delay),delay || 200);
}
}
var autoqueue = [];
function autosolve()
{
if (autoqueue.length !== 0) {autoqueue = []; return;}
if (size === 4) autoqueue = solve4c(unconvert_state([state,state_o]),orient_enabled);
else if (size === 5 && !orient_enabled) autoqueue = solve5a(state);
autoqueue = simplify_move_sequence(autoqueue,4*size-12);
}
function click(e)
{
if (!mouse_enabled) return;
if (touch_enabled && size === 4) return;
// defer to touch emulation (bound to mouseup/down/move)
e.preventDefault();
var bcr = B.getBoundingClientRect();
var x = e.clientX-bcr.left, y = e.clientY-bcr.top;
var unit = 2*parseInt(getComputedStyle(B).fontSize);
x /= unit; y /= unit;
x -= (size-3)/2; y -= (size-3)/2;
if (x < 0 || x >= 3 || y < 0 || y >= 3) return false;
move(Math.floor(x),Math.floor(y),e.shiftKey^(e.type === 'contextmenu'));
redraw();
}
var touchhandler = [];
touchhandler.register = function (id,x,y) {
touchhandler.push(id);
touchhandler['touch'+id] = [-1,-1];
touchhandler.update(id,x,y);
console.log('registered id '+id);
};
touchhandler.deregister = function (id) {
var i = touchhandler.indexOf(id);
if (i !== -1) {touchhandler.splice(i,1); console.log('deregistered id '+id);}
else console.log('touchhandler.deregister: invalid id '+id);
};
touchhandler.update = function (id,x,y,ccw) {
var x0 = touchhandler['touch'+id][0];
var y0 = touchhandler['touch'+id][1];
x -= 0.5; y -= 0.5;
if (Math.max(Math.abs(x-Math.round(x)),Math.abs(y-Math.round(y))) < 0.4)
{ // XXX change this threshold to something based on Manhattan distance from x0,y0
x = Math.round(x); y = Math.round(y);
touchhandler['touch'+id] = [x,y];
//console.log('updated id '+id+' from '+[x0,y0]+' to '+[x,y]);
var sign = ccw ? -1 : 1;
var dx = sign*(x-x0), dy = sign*(y-y0);
if (dx === 0 && Math.abs(dy) === 1)
{
if (x-dy < 0 || x-dy > 3) move(Math.min(x,x+dy),Math.min(y,y0),!ccw);
else move(Math.min(x,x-dy),Math.min(y,y0),ccw);
redraw();
}
else if (dy === 0 && Math.abs(dx) === 1)
{
if (y+dx < 0 || y+dx > 3) move(Math.min(x,x0),Math.min(y,y-dx),!ccw);
else move(Math.min(x,x0),Math.min(y,y+dx),ccw);
redraw();
}
//XXX maybe handle the case where dx,dy = 0,+-2 or +-2,0 too
}
};
function touch(e)
{
e.preventDefault();
//always swallow touch events... though this might not be a good idea in general?
var bcr = B.getBoundingClientRect();
var unit = 2*parseInt(getComputedStyle(B).fontSize);
var changedTouches = e.changedTouches;
/* the touch events spec http://www.w3.org/TR/touch-events/#attributes-2 says that the
changedTouches list has only the changed touches corresponding to the currently-firing
event. */
if (!touch_enabled || size !== 4)
{
/* only use fancy touch input when size=4, otherwise emulate mouse input */
if (e.type !== 'touchstart') return;
[].forEach.call(changedTouches,function (touch) {
click({clientX: touch.clientX,
clientY: touch.clientY,
shiftKey: e.shiftKey,
preventDefault: function () {},
type: 'click'});
}); // dispatch a "fake" click event
return;
}
[].forEach.call(changedTouches,function (touch) {
var x = touch.clientX-bcr.left, y = touch.clientY-bcr.top;
x /= unit; y /= unit;
if (x < 0 || x > 4 || y < 0 || y > 4) return;
switch (e.type)
{
case 'touchstart':
touchhandler.register(touch.identifier,x,y); break;
case 'touchend': case 'touchcancel':
touchhandler.deregister(touch.identifier); break;
case 'touchmove':
touchhandler.update(touch.identifier,x,y,e.shiftKey); break;
}
});
}
function touchemu(e)
{
if (!mouse_enabled || !touch_enabled || size !== 4) return;
e.preventDefault();
var bcr = B.getBoundingClientRect();
var x = e.clientX-bcr.left, y = e.clientY-bcr.top;
var unit = 2*parseInt(getComputedStyle(B).fontSize);
x /= unit; y /= unit;
switch (e.type)
{
case 'mousedown': touchhandler.register(0,x,y); break;
case 'mouseup': touchhandler.deregister(0); break;
case 'mousemove':
if (touchhandler.length > 0) touchhandler.update(0,x,y,e.shiftKey);
break;
case 'mouseout':
if (touchhandler.length > 0) touchhandler.deregister(0);
break;
}
}
function key(e)
{
var keymap={
55:0,56:1,57:2,71:3,67:4,82:5,72:6,84:7,78:8,//dvorak
85:3,73:4,79:5,74:6,75:7,76:8,//qwerty
36:0,38:1,33:2,37:3,12:4,39:5,35:6,40:7,34:8,//numpad, numlock off
103:0,104:1,105:2,100:3,101:4,102:5,97:6,98:7,99:8//numpad, numlock on
};
if (e.keyCode in keymap && !e.ctrlKey && !e.altKey)
{
e.preventDefault();
var m = keymap[e.keyCode];
move(m%3,(m-m%3)/3,e.shiftKey);
redraw();
}
}
init();
// [15,14,6,5,11,14,15,5,6,2] gives a pair of 2-cycles on size>=6, might be useful
// [15,14,6,5,11,14,15,5,6,2,2,6,6,6,6,6,7,7,7,7,7,11,15,14,6,5,11,14,15,5,6,2,2,16,16,16,16,16,15,15,15,15,15,11] rotates two pieces for size=6
</script>
</body>
</html>