arcanumcube 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,686 @@
1
+ // src/core.ts
2
+ var CUBE_SIZE = 3;
3
+ var SIDE_MAX = CUBE_SIZE - 1;
4
+ var SIDE_MIN = 0;
5
+ var SIDE_MIDDLE = Math.floor(CUBE_SIZE / 2);
6
+ var STICKER_COLOR = {
7
+ UP: 0,
8
+ FRONT: 1,
9
+ RIGHT: 2,
10
+ DOWN: 3,
11
+ BACK: 4,
12
+ LEFT: 5,
13
+ PLAIN: 6
14
+ };
15
+ var CUBE = {
16
+ AXIS: "axis",
17
+ CENTER: "center",
18
+ EDGE: "edge",
19
+ CORNER: "corner",
20
+ STICKER: "sticker"
21
+ };
22
+ var CUBETYPE_LIST = [CUBE.AXIS, CUBE.CENTER, CUBE.EDGE, CUBE.CORNER];
23
+ var FACE_LIST = ["U", "F", "R", "D", "B", "L"];
24
+ var FACE = Object.assign(
25
+ {},
26
+ ...FACE_LIST.map((k, i) => ({ [k]: i }))
27
+ );
28
+ var TWIST = {
29
+ U: "U",
30
+ UR: "U'",
31
+ F: "F",
32
+ FR: "F'",
33
+ R: "R",
34
+ RR: "R'",
35
+ D: "D",
36
+ DR: "D'",
37
+ B: "B",
38
+ BR: "B'",
39
+ L: "L",
40
+ LR: "L'",
41
+ M: "M",
42
+ MR: "M'",
43
+ E: "E",
44
+ ER: "E'",
45
+ S: "S",
46
+ SR: "S'",
47
+ U2: "U2",
48
+ F2: "F2",
49
+ R2: "R2",
50
+ D2: "D2",
51
+ B2: "B2",
52
+ L2: "L2",
53
+ M2: "M2",
54
+ E2: "E2",
55
+ S2: "S2"
56
+ };
57
+ var SINGLE_TWIST_LIST = [
58
+ TWIST.U,
59
+ TWIST.UR,
60
+ TWIST.F,
61
+ TWIST.FR,
62
+ TWIST.R,
63
+ TWIST.RR,
64
+ TWIST.D,
65
+ TWIST.DR,
66
+ TWIST.B,
67
+ TWIST.BR,
68
+ TWIST.L,
69
+ TWIST.LR,
70
+ TWIST.M,
71
+ TWIST.MR,
72
+ TWIST.E,
73
+ TWIST.ER,
74
+ TWIST.S,
75
+ TWIST.SR
76
+ ];
77
+ var DOUBLE_TWIST_LIST = [
78
+ TWIST.U2,
79
+ TWIST.F2,
80
+ TWIST.R2,
81
+ TWIST.D2,
82
+ TWIST.B2,
83
+ TWIST.L2,
84
+ TWIST.M2,
85
+ TWIST.E2,
86
+ TWIST.S2
87
+ ];
88
+ var TWIST_LIST = [...SINGLE_TWIST_LIST, ...DOUBLE_TWIST_LIST];
89
+ var CUBE_ANGLES = [
90
+ [
91
+ // down 3x3 cubes
92
+ [
93
+ [TWIST.B],
94
+ // [0, 0, 90]
95
+ [TWIST.B2],
96
+ // [0, 0, 180]
97
+ [TWIST.B2]
98
+ // [0, 0, 180]
99
+ ],
100
+ [
101
+ [TWIST.UR, TWIST.SR],
102
+ // [0, 90, 90]
103
+ [TWIST.M2],
104
+ // [180, 0, 0]
105
+ [TWIST.U, TWIST.S]
106
+ // [0, -90, -90]
107
+ ],
108
+ [
109
+ [TWIST.L2],
110
+ // [180, 0, 0]
111
+ [TWIST.M2],
112
+ // [180, 0, 0]
113
+ [TWIST.L, TWIST.UR, TWIST.F]
114
+ // [90, 90, -90]
115
+ ]
116
+ ],
117
+ [
118
+ // middle 3x3 cubes
119
+ [
120
+ [TWIST.B],
121
+ // [0, 0, 90]
122
+ [TWIST.MR],
123
+ // [-90, 0, 0]
124
+ [TWIST.BR]
125
+ // [0, 0, -90]
126
+ ],
127
+ [
128
+ [TWIST.SR],
129
+ // [0, 0, 90]
130
+ [],
131
+ // [0, 0, 0] original axis
132
+ [TWIST.S]
133
+ // [0, 0, -90]
134
+ ],
135
+ [
136
+ [TWIST.M, TWIST.FR],
137
+ // [90, 0, 90]
138
+ [TWIST.M],
139
+ // [90, 0, 0]
140
+ [TWIST.M, TWIST.F]
141
+ // [90, 0, -90]
142
+ ]
143
+ ],
144
+ [
145
+ // up 3x3 cubes
146
+ [
147
+ [],
148
+ // [0, 0, 0] original corner
149
+ [],
150
+ // [0, 0, 0] original edge
151
+ [TWIST.U]
152
+ // [0, -90, 0]
153
+ ],
154
+ [
155
+ [TWIST.UR],
156
+ // [0, 90, 0]
157
+ [],
158
+ // [0, 0, 0] original center
159
+ [TWIST.U]
160
+ // [0, -90, 0]
161
+ ],
162
+ [
163
+ [TWIST.UR],
164
+ // [0, 90, 0]
165
+ [TWIST.U2],
166
+ // [0, 180, 0]
167
+ [TWIST.U2]
168
+ // [0, 180, 0]
169
+ ]
170
+ ]
171
+ ];
172
+ var TWIST_RULE = {
173
+ [TWIST.U]: { axis: [0, -1, 0], levels: [2], steps: 1 },
174
+ [TWIST.UR]: { axis: [0, 1, 0], levels: [2], steps: 1 },
175
+ [TWIST.F]: { axis: [0, 0, -1], levels: [2], steps: 1 },
176
+ [TWIST.FR]: { axis: [0, 0, 1], levels: [2], steps: 1 },
177
+ [TWIST.R]: { axis: [-1, 0, 0], levels: [2], steps: 1 },
178
+ [TWIST.RR]: { axis: [1, 0, 0], levels: [2], steps: 1 },
179
+ [TWIST.D]: { axis: [0, 1, 0], levels: [0], steps: 1 },
180
+ [TWIST.DR]: { axis: [0, -1, 0], levels: [0], steps: 1 },
181
+ [TWIST.B]: { axis: [0, 0, 1], levels: [0], steps: 1 },
182
+ [TWIST.BR]: { axis: [0, 0, -1], levels: [0], steps: 1 },
183
+ [TWIST.L]: { axis: [1, 0, 0], levels: [0], steps: 1 },
184
+ [TWIST.LR]: { axis: [-1, 0, 0], levels: [0], steps: 1 },
185
+ [TWIST.M]: { axis: [1, 0, 0], levels: [1], steps: 1 },
186
+ [TWIST.MR]: { axis: [-1, 0, 0], levels: [1], steps: 1 },
187
+ [TWIST.E]: { axis: [0, 1, 0], levels: [1], steps: 1 },
188
+ [TWIST.ER]: { axis: [0, -1, 0], levels: [1], steps: 1 },
189
+ [TWIST.S]: { axis: [0, 0, -1], levels: [1], steps: 1 },
190
+ [TWIST.SR]: { axis: [0, 0, 1], levels: [1], steps: 1 },
191
+ [TWIST.U2]: { axis: [0, -1, 0], levels: [2], steps: 2 },
192
+ [TWIST.F2]: { axis: [0, 0, -1], levels: [2], steps: 2 },
193
+ [TWIST.R2]: { axis: [-1, 0, 0], levels: [2], steps: 2 },
194
+ [TWIST.D2]: { axis: [0, 1, 0], levels: [0], steps: 2 },
195
+ [TWIST.B2]: { axis: [0, 0, 1], levels: [0], steps: 2 },
196
+ [TWIST.L2]: { axis: [1, 0, 0], levels: [0], steps: 2 },
197
+ [TWIST.M2]: { axis: [1, 0, 0], levels: [1], steps: 2 },
198
+ [TWIST.E2]: { axis: [0, 1, 0], levels: [1], steps: 2 },
199
+ [TWIST.S2]: { axis: [0, 0, -1], levels: [1], steps: 2 }
200
+ };
201
+ function getStickerIndex(x, y, z, face) {
202
+ let [px, py] = [0, 0];
203
+ if (face === FACE.U) {
204
+ px = x;
205
+ py = z;
206
+ } else if (face === FACE.D) {
207
+ px = x;
208
+ py = SIDE_MAX - z;
209
+ } else if (face === FACE.F) {
210
+ px = x;
211
+ py = SIDE_MAX - y;
212
+ } else if (face === FACE.B) {
213
+ px = SIDE_MAX - x;
214
+ py = SIDE_MAX - y;
215
+ } else if (face === FACE.R) {
216
+ px = SIDE_MAX - z;
217
+ py = SIDE_MAX - y;
218
+ } else if (face === FACE.L) {
219
+ px = z;
220
+ py = SIDE_MAX - y;
221
+ }
222
+ return face * CUBE_SIZE * CUBE_SIZE + py * CUBE_SIZE + px;
223
+ }
224
+ function getCubeFromStickerIndex(index) {
225
+ const face = Math.floor(index / (CUBE_SIZE * CUBE_SIZE));
226
+ const f = index - face * (CUBE_SIZE * CUBE_SIZE);
227
+ const py = Math.floor(f / CUBE_SIZE);
228
+ const px = f % CUBE_SIZE;
229
+ let [x, y, z] = [0, 0, 0];
230
+ if (face === FACE.U) {
231
+ x = px;
232
+ z = py;
233
+ } else if (face === FACE.D) {
234
+ x = px;
235
+ z = SIDE_MAX - py;
236
+ } else if (face === FACE.F) {
237
+ x = px;
238
+ y = SIDE_MAX - py;
239
+ } else if (face === FACE.B) {
240
+ x = SIDE_MAX - px;
241
+ y = SIDE_MAX - py;
242
+ } else if (face === FACE.R) {
243
+ z = SIDE_MAX - px;
244
+ y = SIDE_MAX - py;
245
+ } else if (face === FACE.L) {
246
+ z = px;
247
+ y = SIDE_MAX - py;
248
+ }
249
+ return [x, y, z, face];
250
+ }
251
+ function getRandomTwistList(steps = 0) {
252
+ const t = steps === 0 ? Math.floor(Math.random() * (30 - 15 + 1)) + 15 : steps;
253
+ const len = SINGLE_TWIST_LIST.length;
254
+ const isOffsetting = (a, b) => {
255
+ return a !== b && (a + "'").substring(0, 2) === (b + "'").substring(0, 2);
256
+ };
257
+ const list = [];
258
+ let prev = "";
259
+ for (let i = 0; i < t; i++) {
260
+ let s;
261
+ while (isOffsetting(s = SINGLE_TWIST_LIST[Math.floor(Math.random() * len)], prev)) ;
262
+ list.push(s);
263
+ prev = s;
264
+ }
265
+ return list;
266
+ }
267
+ var SIDE_FACES = Object.freeze([
268
+ [1, 2, 4, 5],
269
+ [2, 0, 5, 3],
270
+ [0, 1, 3, 4],
271
+ [2, 1, 5, 4],
272
+ [0, 2, 3, 5],
273
+ [1, 0, 4, 3]
274
+ ]);
275
+ function getInitialState(up, front) {
276
+ const down = (up + 3) % 6;
277
+ const side = SIDE_FACES[up];
278
+ const state = new Array(CUBE_SIZE * CUBE_SIZE).fill(up);
279
+ for (let i = 0; i < 4; i++) {
280
+ if (i == 2) {
281
+ const downs = [...Array(CUBE_SIZE * CUBE_SIZE)].fill(down);
282
+ state.push(...downs);
283
+ }
284
+ const sides = [...Array(CUBE_SIZE * CUBE_SIZE)].fill(side[(front + i) % 4]);
285
+ state.push(...sides);
286
+ }
287
+ return state;
288
+ }
289
+ function isSameArrays(arr1, arr2) {
290
+ if (arr1.length !== arr2.length) return false;
291
+ return !arr1.some((v, i) => v !== arr2[i]);
292
+ }
293
+ var GOAL_STATE_LIST = [];
294
+ for (let up = 0; up < 6; up++) {
295
+ for (let front = 0; front < 4; front++) {
296
+ GOAL_STATE_LIST.push(getInitialState(up, front));
297
+ }
298
+ }
299
+ Object.freeze(GOAL_STATE_LIST);
300
+ function MatchGoalState(state) {
301
+ return GOAL_STATE_LIST.some((s) => isSameArrays(state, s));
302
+ }
303
+ var Cube = class {
304
+ type;
305
+ position;
306
+ _stickers;
307
+ _initialPosition;
308
+ constructor(x, y, z) {
309
+ this._stickers = [];
310
+ this._initialPosition = { x, y, z };
311
+ this.position = { x, y, z };
312
+ let faces = 0;
313
+ if (x === SIDE_MAX) faces++;
314
+ if (x === SIDE_MIN) faces++;
315
+ if (y === SIDE_MAX) faces++;
316
+ if (y === SIDE_MIN) faces++;
317
+ if (z === SIDE_MAX) faces++;
318
+ if (z === SIDE_MIN) faces++;
319
+ this.type = CUBETYPE_LIST[faces];
320
+ }
321
+ getStickers() {
322
+ return this._stickers;
323
+ }
324
+ init() {
325
+ const { x, y, z } = this.position;
326
+ const angles = CUBE_ANGLES[y][z][x];
327
+ this._stickers = [];
328
+ if (this.type === CUBE.CENTER) {
329
+ this._stickers = [{ face: FACE.U, color: STICKER_COLOR.PLAIN }];
330
+ } else if (this.type === CUBE.EDGE) {
331
+ this._stickers = [
332
+ { face: FACE.U, color: STICKER_COLOR.PLAIN },
333
+ { face: FACE.B, color: STICKER_COLOR.PLAIN }
334
+ ];
335
+ } else if (this.type === CUBE.CORNER) {
336
+ this._stickers = [
337
+ { face: FACE.U, color: STICKER_COLOR.PLAIN },
338
+ { face: FACE.B, color: STICKER_COLOR.PLAIN },
339
+ { face: FACE.L, color: STICKER_COLOR.PLAIN }
340
+ ];
341
+ }
342
+ this.rotateStickerFace(angles, false, true);
343
+ }
344
+ reset() {
345
+ const Faces = [FACE.U, FACE.B, FACE.L];
346
+ const { x, y, z } = this._initialPosition;
347
+ this.position = { x, y, z };
348
+ const angles = CUBE_ANGLES[y][z][x];
349
+ for (let i = 0; i < this._stickers.length; i++) {
350
+ this._stickers[i].face = Faces[i];
351
+ this._stickers[i].color = STICKER_COLOR.PLAIN;
352
+ }
353
+ this.rotateStickerFace(angles, false, true);
354
+ }
355
+ rotateStickerFace(twists, reverse = false, init = false) {
356
+ const rotateMap = [
357
+ /* x */
358
+ [FACE.U, FACE.F, FACE.D, FACE.B],
359
+ /* y */
360
+ [FACE.F, FACE.R, FACE.B, FACE.L],
361
+ /* z */
362
+ [FACE.U, FACE.L, FACE.D, FACE.R]
363
+ ];
364
+ function getNext(face, axis_index, twist) {
365
+ const { axis, steps } = TWIST_RULE[twist];
366
+ const map = rotateMap[axis_index];
367
+ const i = map.indexOf(face);
368
+ if (i < 0) return face;
369
+ const angle = axis[axis_index];
370
+ const i2 = (i + 4 + (reverse ? -1 : 1) * angle * steps) % 4;
371
+ return map[i2];
372
+ }
373
+ for (const sticker of this._stickers) {
374
+ for (const r of twists) {
375
+ sticker.face = getNext(sticker.face, 0, r);
376
+ sticker.face = getNext(sticker.face, 1, r);
377
+ sticker.face = getNext(sticker.face, 2, r);
378
+ }
379
+ if (init && sticker.color === STICKER_COLOR.PLAIN) {
380
+ sticker.color = sticker.face;
381
+ }
382
+ }
383
+ }
384
+ };
385
+ var ArcanumCube = class {
386
+ /** output debug information to console */
387
+ debug;
388
+ /** history of twisting */
389
+ _history;
390
+ /** cube objects matrix */
391
+ _matrix;
392
+ constructor(options) {
393
+ this.debug = false;
394
+ this._history = [];
395
+ this._matrix = [];
396
+ if (options) {
397
+ if (options.debug != null) {
398
+ this.debug = options.debug;
399
+ }
400
+ }
401
+ }
402
+ init() {
403
+ this._matrix = [];
404
+ const yarray = [];
405
+ for (let y = SIDE_MIN; y <= SIDE_MAX; y++) {
406
+ const zarray = [];
407
+ for (let z = SIDE_MIN; z <= SIDE_MAX; z++) {
408
+ const xarray = [];
409
+ for (let x = SIDE_MIN; x <= SIDE_MAX; x++) {
410
+ const cube = new Cube(x, y, z);
411
+ cube.init();
412
+ xarray.push(cube);
413
+ }
414
+ zarray.push(xarray);
415
+ }
416
+ yarray.push(zarray);
417
+ }
418
+ this._matrix = yarray;
419
+ }
420
+ reset() {
421
+ const list = [];
422
+ for (let y = SIDE_MIN; y <= SIDE_MAX; y++) {
423
+ for (let z = SIDE_MIN; z <= SIDE_MAX; z++) {
424
+ for (let x = SIDE_MIN; x <= SIDE_MAX; x++) {
425
+ const cube = this._matrix[y][z][x];
426
+ cube.reset();
427
+ list.push(cube);
428
+ }
429
+ }
430
+ }
431
+ list.forEach((cube) => {
432
+ const n = cube.position;
433
+ this._matrix[n.y][n.z][n.x] = cube;
434
+ });
435
+ this._history = [];
436
+ }
437
+ // twist randomly several steps
438
+ scramble(steps) {
439
+ const list = getRandomTwistList(steps);
440
+ if (this.debug) console.log("Scramble: " + list.join(", "));
441
+ this.twist(list, false);
442
+ }
443
+ undo(steps = 1) {
444
+ const list = this.getUndoList(steps);
445
+ this.twist(list, true);
446
+ }
447
+ isSolved() {
448
+ return MatchGoalState(this.getStickerColors());
449
+ }
450
+ getHistory() {
451
+ return this._history;
452
+ }
453
+ getUndoList(steps = 1) {
454
+ let t = steps;
455
+ if (t < 0) t = 0;
456
+ if (t > this._history.length) t = this._history.length;
457
+ return this._history.slice(-t).reverse();
458
+ }
459
+ twist(twist, reverse = false) {
460
+ if (Array.isArray(twist)) {
461
+ if (twist.length == 0) return;
462
+ for (const c of twist) {
463
+ this._twist(c, reverse);
464
+ }
465
+ } else {
466
+ this._twist(twist, reverse);
467
+ }
468
+ }
469
+ _twist(twist, reverse) {
470
+ this.rotateMatrix(twist, reverse);
471
+ if (this.debug) {
472
+ this.dumpStickers();
473
+ }
474
+ if (reverse) {
475
+ this._history.pop();
476
+ } else {
477
+ this._history.push(twist);
478
+ }
479
+ if (this.debug) console.log(this._history.join(" "));
480
+ }
481
+ rotateMatrix(twist, reverse = false) {
482
+ const rule = TWIST_RULE[twist];
483
+ const axis = rule.axis;
484
+ const map = new Array(3);
485
+ for (const l of rule.levels) {
486
+ let i = axis.indexOf(-1);
487
+ if (i === -1) i = axis.indexOf(1);
488
+ const s = (reverse ? -1 : 1) * axis[i];
489
+ map[i] = Array(8).fill(l);
490
+ map[(i + s + 3) % 3] = [0, 1, 2, 2, 2, 1, 0, 0];
491
+ map[(i - s + 3) % 3] = [0, 0, 0, 1, 2, 2, 2, 1];
492
+ const [x, y, z] = map;
493
+ const tmp = [];
494
+ for (let i2 = 0; i2 < 8; i2++) {
495
+ const cube = this._matrix[y[i2]][z[i2]][x[i2]];
496
+ cube.rotateStickerFace([twist], reverse);
497
+ tmp.push(cube);
498
+ }
499
+ for (let i2 = 0; i2 < 8; i2++) {
500
+ const i22 = (i2 + rule.steps * 2) % 8;
501
+ tmp[i2].position = { x: x[i22], y: y[i22], z: z[i22] };
502
+ this._matrix[y[i22]][z[i22]][x[i22]] = tmp[i2];
503
+ }
504
+ }
505
+ }
506
+ getStickerColors() {
507
+ const list = new Array(CUBE_SIZE * CUBE_SIZE * 6);
508
+ for (let y = SIDE_MIN; y <= SIDE_MAX; y++) {
509
+ for (let z = SIDE_MIN; z <= SIDE_MAX; z++) {
510
+ for (let x = SIDE_MIN; x <= SIDE_MAX; x++) {
511
+ const cube = this._matrix[y][z][x];
512
+ const stickers = cube.getStickers();
513
+ for (const sticker of stickers) {
514
+ const index = getStickerIndex(x, y, z, sticker.face);
515
+ list[index] = sticker.color;
516
+ }
517
+ }
518
+ }
519
+ }
520
+ return list;
521
+ }
522
+ dumpStickers() {
523
+ const list = this.getStickerColors();
524
+ const STYLE = "background-color:black; padding:1px 1px;";
525
+ const Col2Str = {
526
+ [STICKER_COLOR.UP]: "color:white; " + STYLE,
527
+ [STICKER_COLOR.FRONT]: "color:#00d800; " + STYLE,
528
+ [STICKER_COLOR.RIGHT]: "color:#d80000; " + STYLE,
529
+ [STICKER_COLOR.DOWN]: "color:yellow; " + STYLE,
530
+ [STICKER_COLOR.BACK]: "color:#0000d8; " + STYLE,
531
+ [STICKER_COLOR.LEFT]: "color:#ff8000; " + STYLE
532
+ };
533
+ const RESET = "background-color:none; padding:0px 0px;";
534
+ const result = [];
535
+ const attrs = [];
536
+ const getLine = (faces) => {
537
+ const line = [];
538
+ for (let py = 0; py < 3; py++) {
539
+ for (const f of faces) {
540
+ if (f === -1) {
541
+ line.push(" ");
542
+ } else {
543
+ line.push("%c\u25A0 %c\u25A0 %c\u25A0%c ");
544
+ const index = f * CUBE_SIZE * CUBE_SIZE + py * CUBE_SIZE;
545
+ attrs.push(Col2Str[list[index + 0]]);
546
+ attrs.push(Col2Str[list[index + 1]]);
547
+ attrs.push(Col2Str[list[index + 2]]);
548
+ attrs.push(RESET);
549
+ }
550
+ }
551
+ line.push("\n");
552
+ }
553
+ line.push("\n");
554
+ result.push(line.join(""));
555
+ };
556
+ getLine([-1, FACE.U]);
557
+ getLine([FACE.L, FACE.F, FACE.R, FACE.B]);
558
+ getLine([-1, FACE.D]);
559
+ console.log(result.join("\n"), ...attrs);
560
+ }
561
+ };
562
+ var STICKER_PERMUTATION_GROUP_MAP = makeStickerPermutationGroupMap();
563
+ function makeStickerPermutationGroupMap() {
564
+ const result = {};
565
+ TWIST_LIST.forEach((twist) => {
566
+ const tmp = getCubePermutationGroup(twist).stickers;
567
+ result[twist] = [tmp.before, tmp.after];
568
+ });
569
+ return result;
570
+ }
571
+ function getNextStickerColors(stickers, twist) {
572
+ const result = [...stickers];
573
+ const [bef, aft] = STICKER_PERMUTATION_GROUP_MAP[twist];
574
+ for (let i = 0; i < bef.length; i++) {
575
+ result[aft[i]] = stickers[bef[i]];
576
+ }
577
+ return result;
578
+ }
579
+ function getArrayForTensor(stickers) {
580
+ const array = [...Array(3)].map(
581
+ () => [...Array(3)].map(() => Array(6 * 6).fill(0))
582
+ );
583
+ for (let i = 0; i < stickers.length; i++) {
584
+ const face = Math.floor(i / (CUBE_SIZE * CUBE_SIZE));
585
+ const c = i - face * (CUBE_SIZE * CUBE_SIZE);
586
+ const row = Math.floor(c / CUBE_SIZE);
587
+ const column = c - row * CUBE_SIZE;
588
+ const color = stickers[i];
589
+ array[row][column][color * 6 + face] = 1;
590
+ }
591
+ return array;
592
+ }
593
+ function getCubePermutationGroup(twist, reverse = false) {
594
+ const rule = TWIST_RULE[twist];
595
+ const axis = rule.axis;
596
+ const result = { before: [], after: [], stickers: { before: [], after: [] } };
597
+ const map = new Array(3);
598
+ for (const l of rule.levels) {
599
+ let i = axis.indexOf(-1);
600
+ if (i === -1) i = axis.indexOf(1);
601
+ const s = (reverse ? -1 : 1) * axis[i];
602
+ map[i] = Array(8).fill(l);
603
+ map[(i + s + 3) % 3] = [0, 1, 2, 2, 2, 1, 0, 0];
604
+ map[(i - s + 3) % 3] = [0, 0, 0, 1, 2, 2, 2, 1];
605
+ const [x, y, z] = map;
606
+ for (let i2 = 0; i2 < 8; i2++) {
607
+ const i22 = (i2 + rule.steps * 2) % 8;
608
+ result.before.push([x[i2], y[i2], z[i2]]);
609
+ result.after.push([x[i22], y[i22], z[i22]]);
610
+ const list = getStickerPermutationGroup(
611
+ [x[i2], y[i2], z[i2]],
612
+ [x[i22], y[i22], z[i22]],
613
+ [twist],
614
+ reverse
615
+ );
616
+ result.stickers.before.push(...list.before);
617
+ result.stickers.after.push(...list.after);
618
+ }
619
+ }
620
+ return result;
621
+ }
622
+ function getStickerPermutationGroup(position, position2, twists, reverse = false) {
623
+ const rotateMap = [
624
+ /* x */
625
+ [FACE.U, FACE.F, FACE.D, FACE.B],
626
+ /* y */
627
+ [FACE.F, FACE.R, FACE.B, FACE.L],
628
+ /* z */
629
+ [FACE.U, FACE.L, FACE.D, FACE.R]
630
+ ];
631
+ function getNext(face, axis_index, twist) {
632
+ const { axis, steps } = TWIST_RULE[twist];
633
+ const map = rotateMap[axis_index];
634
+ const i = map.indexOf(face);
635
+ if (i < 0) return face;
636
+ const angle = axis[axis_index];
637
+ const i2 = (i + 4 + (reverse ? -1 : 1) * angle * steps) % 4;
638
+ return map[i2];
639
+ }
640
+ const result = { before: [], after: [] };
641
+ const [x, y, z] = position;
642
+ const [x2, y2, z2] = position2;
643
+ const faces = [];
644
+ if (x === SIDE_MAX) faces.push(FACE.R);
645
+ if (x === SIDE_MIN) faces.push(FACE.L);
646
+ if (y === SIDE_MAX) faces.push(FACE.U);
647
+ if (y === SIDE_MIN) faces.push(FACE.D);
648
+ if (z === SIDE_MAX) faces.push(FACE.F);
649
+ if (z === SIDE_MIN) faces.push(FACE.B);
650
+ for (const face of faces) {
651
+ let f = face;
652
+ result.before.push(getStickerIndex(x, y, z, f));
653
+ for (const t of twists) {
654
+ f = getNext(f, 0, t);
655
+ f = getNext(f, 1, t);
656
+ f = getNext(f, 2, t);
657
+ }
658
+ result.after.push(getStickerIndex(x2, y2, z2, f));
659
+ }
660
+ return result;
661
+ }
662
+ export {
663
+ ArcanumCube,
664
+ CUBE,
665
+ CUBE_ANGLES,
666
+ CUBE_SIZE,
667
+ Cube,
668
+ DOUBLE_TWIST_LIST,
669
+ FACE,
670
+ MatchGoalState,
671
+ SIDE_MAX,
672
+ SIDE_MIDDLE,
673
+ SIDE_MIN,
674
+ SINGLE_TWIST_LIST,
675
+ STICKER_COLOR,
676
+ TWIST,
677
+ TWIST_LIST,
678
+ TWIST_RULE,
679
+ getArrayForTensor,
680
+ getCubeFromStickerIndex,
681
+ getCubePermutationGroup,
682
+ getNextStickerColors,
683
+ getRandomTwistList,
684
+ getStickerIndex,
685
+ getStickerPermutationGroup
686
+ };
@@ -0,0 +1,4 @@
1
+ var H=3,Y=2,q=0,J=Math.floor(1.5),h={UP:0,FRONT:1,RIGHT:2,DOWN:3,BACK:4,LEFT:5,PLAIN:6},x={AXIS:"axis",CENTER:"center",EDGE:"edge",CORNER:"corner",STICKER:"sticker"},d=[x.AXIS,x.CENTER,x.EDGE,x.CORNER],N=["U","F","R","D","B","L"],n=Object.assign({},...N.map((a,t)=>({[a]:t}))),s={U:"U",UR:"U'",F:"F",FR:"F'",R:"R",RR:"R'",D:"D",DR:"D'",B:"B",BR:"B'",L:"L",LR:"L'",M:"M",MR:"M'",E:"E",ER:"E'",S:"S",SR:"S'",U2:"U2",F2:"F2",R2:"R2",D2:"D2",B2:"B2",L2:"L2",M2:"M2",E2:"E2",S2:"S2"},M=[s.U,s.UR,s.F,s.FR,s.R,s.RR,s.D,s.DR,s.B,s.BR,s.L,s.LR,s.M,s.MR,s.E,s.ER,s.S,s.SR],w=[s.U2,s.F2,s.R2,s.D2,s.B2,s.L2,s.M2,s.E2,s.S2],X=[...M,...w],B=[[[[s.B],[s.B2],[s.B2]],[[s.UR,s.SR],[s.M2],[s.U,s.S]],[[s.L2],[s.M2],[s.L,s.UR,s.F]]],[[[s.B],[s.MR],[s.BR]],[[s.SR],[],[s.S]],[[s.M,s.FR],[s.M],[s.M,s.F]]],[[[],[],[s.U]],[[s.UR],[],[s.U]],[[s.UR],[s.U2],[s.U2]]]],m={[s.U]:{axis:[0,-1,0],levels:[2],steps:1},[s.UR]:{axis:[0,1,0],levels:[2],steps:1},[s.F]:{axis:[0,0,-1],levels:[2],steps:1},[s.FR]:{axis:[0,0,1],levels:[2],steps:1},[s.R]:{axis:[-1,0,0],levels:[2],steps:1},[s.RR]:{axis:[1,0,0],levels:[2],steps:1},[s.D]:{axis:[0,1,0],levels:[0],steps:1},[s.DR]:{axis:[0,-1,0],levels:[0],steps:1},[s.B]:{axis:[0,0,1],levels:[0],steps:1},[s.BR]:{axis:[0,0,-1],levels:[0],steps:1},[s.L]:{axis:[1,0,0],levels:[0],steps:1},[s.LR]:{axis:[-1,0,0],levels:[0],steps:1},[s.M]:{axis:[1,0,0],levels:[1],steps:1},[s.MR]:{axis:[-1,0,0],levels:[1],steps:1},[s.E]:{axis:[0,1,0],levels:[1],steps:1},[s.ER]:{axis:[0,-1,0],levels:[1],steps:1},[s.S]:{axis:[0,0,-1],levels:[1],steps:1},[s.SR]:{axis:[0,0,1],levels:[1],steps:1},[s.U2]:{axis:[0,-1,0],levels:[2],steps:2},[s.F2]:{axis:[0,0,-1],levels:[2],steps:2},[s.R2]:{axis:[-1,0,0],levels:[2],steps:2},[s.D2]:{axis:[0,1,0],levels:[0],steps:2},[s.B2]:{axis:[0,0,1],levels:[0],steps:2},[s.L2]:{axis:[1,0,0],levels:[0],steps:2},[s.M2]:{axis:[1,0,0],levels:[1],steps:2},[s.E2]:{axis:[0,1,0],levels:[1],steps:2},[s.S2]:{axis:[0,0,-1],levels:[1],steps:2}};function C(a,t,e,o){let[r,i]=[0,0];return o===n.U?(r=a,i=e):o===n.D?(r=a,i=2-e):o===n.F?(r=a,i=2-t):o===n.B?(r=2-a,i=2-t):o===n.R?(r=2-e,i=2-t):o===n.L&&(r=e,i=2-t),o*3*3+i*3+r}function Q(a){let t=Math.floor(a/9),e=a-t*(3*3),o=Math.floor(e/3),r=e%3,[i,c,l]=[0,0,0];return t===n.U?(i=r,l=o):t===n.D?(i=r,l=2-o):t===n.F?(i=r,c=2-o):t===n.B?(i=2-r,c=2-o):t===n.R?(l=2-r,c=2-o):t===n.L&&(l=r,c=2-o),[i,c,l,t]}function v(a=0){let t=a===0?Math.floor(Math.random()*16)+15:a,e=M.length,o=(c,l)=>c!==l&&(c+"'").substring(0,2)===(l+"'").substring(0,2),r=[],i="";for(let c=0;c<t;c++){let l;for(;o(l=M[Math.floor(Math.random()*e)],i););r.push(l),i=l}return r}var Z=Object.freeze([[1,2,4,5],[2,0,5,3],[0,1,3,4],[2,1,5,4],[0,2,3,5],[1,0,4,3]]);function O(a,t){let e=(a+3)%6,o=Z[a],r=new Array(3*3).fill(a);for(let i=0;i<4;i++){if(i==2){let l=[...Array(9)].fill(e);r.push(...l)}let c=[...Array(3*3)].fill(o[(t+i)%4]);r.push(...c)}return r}function P(a,t){return a.length!==t.length?!1:!a.some((e,o)=>e!==t[o])}var U=[];for(let a=0;a<6;a++)for(let t=0;t<4;t++)U.push(O(a,t));Object.freeze(U);function z(a){return U.some(t=>P(a,t))}var D=class{type;position;_stickers;_initialPosition;constructor(t,e,o){this._stickers=[],this._initialPosition={x:t,y:e,z:o},this.position={x:t,y:e,z:o};let r=0;t===2&&r++,t===0&&r++,e===2&&r++,e===0&&r++,o===2&&r++,o===0&&r++,this.type=d[r]}getStickers(){return this._stickers}init(){let{x:t,y:e,z:o}=this.position,r=B[e][o][t];this._stickers=[],this.type===x.CENTER?this._stickers=[{face:n.U,color:h.PLAIN}]:this.type===x.EDGE?this._stickers=[{face:n.U,color:h.PLAIN},{face:n.B,color:h.PLAIN}]:this.type===x.CORNER&&(this._stickers=[{face:n.U,color:h.PLAIN},{face:n.B,color:h.PLAIN},{face:n.L,color:h.PLAIN}]),this.rotateStickerFace(r,!1,!0)}reset(){let t=[n.U,n.B,n.L],{x:e,y:o,z:r}=this._initialPosition;this.position={x:e,y:o,z:r};let i=B[o][r][e];for(let c=0;c<this._stickers.length;c++)this._stickers[c].face=t[c],this._stickers[c].color=h.PLAIN;this.rotateStickerFace(i,!1,!0)}rotateStickerFace(t,e=!1,o=!1){let r=[[n.U,n.F,n.D,n.B],[n.F,n.R,n.B,n.L],[n.U,n.L,n.D,n.R]];function i(c,l,_){let{axis:E,steps:p}=m[_],I=r[l],u=I.indexOf(c);if(u<0)return c;let f=E[l],S=(u+4+(e?-1:1)*f*p)%4;return I[S]}for(let c of this._stickers){for(let l of t)c.face=i(c.face,0,l),c.face=i(c.face,1,l),c.face=i(c.face,2,l);o&&c.color===h.PLAIN&&(c.color=c.face)}}},T=class{debug;_history;_matrix;constructor(t){this.debug=!1,this._history=[],this._matrix=[],t&&t.debug!=null&&(this.debug=t.debug)}init(){this._matrix=[];let t=[];for(let e=0;e<=2;e++){let o=[];for(let r=0;r<=2;r++){let i=[];for(let c=0;c<=2;c++){let l=new D(c,e,r);l.init(),i.push(l)}o.push(i)}t.push(o)}this._matrix=t}reset(){let t=[];for(let e=0;e<=2;e++)for(let o=0;o<=2;o++)for(let r=0;r<=2;r++){let i=this._matrix[e][o][r];i.reset(),t.push(i)}t.forEach(e=>{let o=e.position;this._matrix[o.y][o.z][o.x]=e}),this._history=[]}scramble(t){let e=v(t);this.debug&&console.log("Scramble: "+e.join(", ")),this.twist(e,!1)}undo(t=1){let e=this.getUndoList(t);this.twist(e,!0)}isSolved(){return z(this.getStickerColors())}getHistory(){return this._history}getUndoList(t=1){let e=t;return e<0&&(e=0),e>this._history.length&&(e=this._history.length),this._history.slice(-e).reverse()}twist(t,e=!1){if(Array.isArray(t)){if(t.length==0)return;for(let o of t)this._twist(o,e)}else this._twist(t,e)}_twist(t,e){this.rotateMatrix(t,e),this.debug&&this.dumpStickers(),e?this._history.pop():this._history.push(t),this.debug&&console.log(this._history.join(" "))}rotateMatrix(t,e=!1){let o=m[t],r=o.axis,i=new Array(3);for(let c of o.levels){let l=r.indexOf(-1);l===-1&&(l=r.indexOf(1));let _=(e?-1:1)*r[l];i[l]=Array(8).fill(c),i[(l+_+3)%3]=[0,1,2,2,2,1,0,0],i[(l-_+3)%3]=[0,0,0,1,2,2,2,1];let[E,p,I]=i,u=[];for(let f=0;f<8;f++){let S=this._matrix[p[f]][I[f]][E[f]];S.rotateStickerFace([t],e),u.push(S)}for(let f=0;f<8;f++){let S=(f+o.steps*2)%8;u[f].position={x:E[S],y:p[S],z:I[S]},this._matrix[p[S]][I[S]][E[S]]=u[f]}}}getStickerColors(){let t=new Array(54);for(let e=0;e<=2;e++)for(let o=0;o<=2;o++)for(let r=0;r<=2;r++){let c=this._matrix[e][o][r].getStickers();for(let l of c){let _=C(r,e,o,l.face);t[_]=l.color}}return t}dumpStickers(){let t=this.getStickerColors(),e="background-color:black; padding:1px 1px;",o={[h.UP]:"color:white; "+e,[h.FRONT]:"color:#00d800; "+e,[h.RIGHT]:"color:#d80000; "+e,[h.DOWN]:"color:yellow; "+e,[h.BACK]:"color:#0000d8; "+e,[h.LEFT]:"color:#ff8000; "+e},r="background-color:none; padding:0px 0px;",i=[],c=[],l=_=>{let E=[];for(let p=0;p<3;p++){for(let I of _)if(I===-1)E.push(" ");else{E.push("%c\u25A0 %c\u25A0 %c\u25A0%c ");let u=I*3*3+p*3;c.push(o[t[u+0]]),c.push(o[t[u+1]]),c.push(o[t[u+2]]),c.push(r)}E.push(`
2
+ `)}E.push(`
3
+ `),i.push(E.join(""))};l([-1,n.U]),l([n.L,n.F,n.R,n.B]),l([-1,n.D]),console.log(i.join(`
4
+ `),...c)}},G=W();function W(){let a={};return X.forEach(t=>{let e=j(t).stickers;a[t]=[e.before,e.after]}),a}function V(a,t){let e=[...a],[o,r]=G[t];for(let i=0;i<o.length;i++)e[r[i]]=a[o[i]];return e}function $(a){let t=[...Array(3)].map(()=>[...Array(3)].map(()=>Array(36).fill(0)));for(let e=0;e<a.length;e++){let o=Math.floor(e/9),r=e-o*(3*3),i=Math.floor(r/3),c=r-i*3,l=a[e];t[i][c][l*6+o]=1}return t}function j(a,t=!1){let e=m[a],o=e.axis,r={before:[],after:[],stickers:{before:[],after:[]}},i=new Array(3);for(let c of e.levels){let l=o.indexOf(-1);l===-1&&(l=o.indexOf(1));let _=(t?-1:1)*o[l];i[l]=Array(8).fill(c),i[(l+_+3)%3]=[0,1,2,2,2,1,0,0],i[(l-_+3)%3]=[0,0,0,1,2,2,2,1];let[E,p,I]=i;for(let u=0;u<8;u++){let f=(u+e.steps*2)%8;r.before.push([E[u],p[u],I[u]]),r.after.push([E[f],p[f],I[f]]);let S=K([E[u],p[u],I[u]],[E[f],p[f],I[f]],[a],t);r.stickers.before.push(...S.before),r.stickers.after.push(...S.after)}}return r}function K(a,t,e,o=!1){let r=[[n.U,n.F,n.D,n.B],[n.F,n.R,n.B,n.L],[n.U,n.L,n.D,n.R]];function i(S,b,R){let{axis:g,steps:k}=m[R],y=r[b],A=y.indexOf(S);if(A<0)return S;let L=g[b],F=(A+4+(o?-1:1)*L*k)%4;return y[F]}let c={before:[],after:[]},[l,_,E]=a,[p,I,u]=t,f=[];l===2&&f.push(n.R),l===0&&f.push(n.L),_===2&&f.push(n.U),_===0&&f.push(n.D),E===2&&f.push(n.F),E===0&&f.push(n.B);for(let S of f){let b=S;c.before.push(C(l,_,E,b));for(let R of e)b=i(b,0,R),b=i(b,1,R),b=i(b,2,R);c.after.push(C(p,I,u,b))}return c}export{T as ArcanumCube,x as CUBE,B as CUBE_ANGLES,H as CUBE_SIZE,D as Cube,w as DOUBLE_TWIST_LIST,n as FACE,z as MatchGoalState,Y as SIDE_MAX,J as SIDE_MIDDLE,q as SIDE_MIN,M as SINGLE_TWIST_LIST,h as STICKER_COLOR,s as TWIST,X as TWIST_LIST,m as TWIST_RULE,$ as getArrayForTensor,Q as getCubeFromStickerIndex,j as getCubePermutationGroup,V as getNextStickerColors,v as getRandomTwistList,C as getStickerIndex,K as getStickerPermutationGroup};