UNPKG

arcanumcube

Version:
5 lines (4 loc) 8.67 kB
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(` `)}E.push(` `),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(` `),...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};