@mborecki/sliding-puzzle 0.2.0 → 0.3.0
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.
- package/dist/cjs/mb-sliding-puzzle.cjs.entry.js +3 -2
- package/dist/collection/components/sliding-puzzle/sliding-puzzle.js +3 -2
- package/dist/components/mb-sliding-puzzle.js +1 -1
- package/dist/esm/mb-sliding-puzzle.entry.js +3 -2
- package/dist/sliding-puzzle/{p-ee89c4f7.entry.js → p-a976746c.entry.js} +1 -1
- package/dist/sliding-puzzle/sliding-puzzle.esm.js +1 -1
- package/dist/types/components/sliding-puzzle/types.d.ts +1 -0
- package/package.json +3 -3
|
@@ -144,6 +144,8 @@ const MbSlidingPuzzle = class {
|
|
|
144
144
|
}
|
|
145
145
|
onTileClicked(id) {
|
|
146
146
|
const tile = this.getTileById(id);
|
|
147
|
+
if (tile.blocked)
|
|
148
|
+
return;
|
|
147
149
|
const v = Grid.getVecFromIndex(tile.position, this.size.width);
|
|
148
150
|
const target = this.getMoveTarget(v);
|
|
149
151
|
if (target) {
|
|
@@ -155,7 +157,6 @@ const MbSlidingPuzzle = class {
|
|
|
155
157
|
const cells = Grid.getAllCells(this.size);
|
|
156
158
|
const emptyCell = cells.find(c => {
|
|
157
159
|
const index = Grid.getIndexFromVec(c, this.size.width);
|
|
158
|
-
console.log(index);
|
|
159
160
|
return this.tiles.every(t => t.position !== index);
|
|
160
161
|
});
|
|
161
162
|
return emptyCell ?? null;
|
|
@@ -181,7 +182,7 @@ const MbSlidingPuzzle = class {
|
|
|
181
182
|
});
|
|
182
183
|
}
|
|
183
184
|
render() {
|
|
184
|
-
return index.h("div", { key: '
|
|
185
|
+
return index.h("div", { key: 'd6042863922edef06b51cf3897c4cdad5b39b3c7', class: "board", style: {
|
|
185
186
|
'--width': `${this.size.width}`,
|
|
186
187
|
'--height': `${this.size.height}`,
|
|
187
188
|
} }, this.tiles.map(tile => {
|
|
@@ -17,6 +17,8 @@ export class MbSlidingPuzzle {
|
|
|
17
17
|
}
|
|
18
18
|
onTileClicked(id) {
|
|
19
19
|
const tile = this.getTileById(id);
|
|
20
|
+
if (tile.blocked)
|
|
21
|
+
return;
|
|
20
22
|
const v = Grid.getVecFromIndex(tile.position, this.size.width);
|
|
21
23
|
const target = this.getMoveTarget(v);
|
|
22
24
|
if (target) {
|
|
@@ -28,7 +30,6 @@ export class MbSlidingPuzzle {
|
|
|
28
30
|
const cells = Grid.getAllCells(this.size);
|
|
29
31
|
const emptyCell = cells.find(c => {
|
|
30
32
|
const index = Grid.getIndexFromVec(c, this.size.width);
|
|
31
|
-
console.log(index);
|
|
32
33
|
return this.tiles.every(t => t.position !== index);
|
|
33
34
|
});
|
|
34
35
|
return emptyCell ?? null;
|
|
@@ -54,7 +55,7 @@ export class MbSlidingPuzzle {
|
|
|
54
55
|
});
|
|
55
56
|
}
|
|
56
57
|
render() {
|
|
57
|
-
return h("div", { key: '
|
|
58
|
+
return h("div", { key: 'd6042863922edef06b51cf3897c4cdad5b39b3c7', class: "board", style: {
|
|
58
59
|
'--width': `${this.size.width}`,
|
|
59
60
|
'--height': `${this.size.height}`,
|
|
60
61
|
} }, this.tiles.map(tile => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{t,p as e,H as i,h as s}from"./index.js";class r{x;y;get 0(){return this.x}set 0(t){this.x=t}get 1(){return this.y}set 1(t){this.y=t}get width(){return this.x}set width(t){this.x=t}get height(){return this.y}set height(t){this.y=t}get xy(){return[this.x,this.y]}*[Symbol.iterator](){yield this.x,yield this.y}constructor(t,e){this.x=t,this.y=e}add(t){return new r(this.x+t.x,this.y+t.y)}eq(t){return this.x===t.x&&this.y===t.y}clone(){return new r(this.x,this.y)}static from(t){if("number"==typeof t)return new r(t,t);if(Array.isArray(t))return new r(t[0]??0,t[1]??0);if("object"==typeof t&&"number"==typeof t.x&&"number"==typeof t.y)return new r(t.x,t.y);throw Error("Wrong Vec2 source")}}class n{size;constructor(t){if(t.x<=0||t.y<=0)throw Error(`Invalid grid size: width and height must be positive (got ${t.x}x${t.y})`);this.size=t}getIndexFromVec(t){return n.getIndexFromVec(t,this.size.width)}getVecFromIndex(t){return n.getVecFromIndex(t,this.size.width)}getAllIndexes(){return n.getAllIndexes(this.size)}getAllCells(){return n.getAllCells(this.size)}isBlack(t){return!!((t.x+t.y)%2)}isWhite(t){return!((t.x+t.y)%2)}isInGrid(t){return n.isInGrid(t,this.size)}static getAllIndexes(t){return Array(t.width*t.height).fill(!0).map(((t,e)=>e))}static getAllCells(t){return Array(t.width*t.height).fill(!0).map(((e,i)=>n.getVecFromIndex(i,t.width)))}static getVecFromIndex(t,e){const i=e instanceof r?e.width:e;return new r(t%i,Math.floor(t/i))}static getIndexFromVec(t,e){return t.y*(e instanceof r?e.width:e)+t.x}static isInGrid(t,e){return t.x>=0&&e.x>t.x&&t.y>=0&&e.y>t.y}static getOrtoSiblings(t,e){return[new r(-1,0),new r(1,0),new r(0,-1),new r(0,1)].map((e=>e.add(t))).filter((t=>n.isInGrid(t,e)))}}const h=e(class extends i{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow()}size=new r(3,3);tiles=[];freeMove=!1;async checkAnswer(){return this.tiles.every((t=>t.position===t.correctPosition))}getTileById(t){const e=this.tiles.find((e=>e.id===t));if(!e)throw Error("Tile not found");return e}onTileClicked(t){const e=this.getTileById(t)
|
|
1
|
+
import{t,p as e,H as i,h as s}from"./index.js";class r{x;y;get 0(){return this.x}set 0(t){this.x=t}get 1(){return this.y}set 1(t){this.y=t}get width(){return this.x}set width(t){this.x=t}get height(){return this.y}set height(t){this.y=t}get xy(){return[this.x,this.y]}*[Symbol.iterator](){yield this.x,yield this.y}constructor(t,e){this.x=t,this.y=e}add(t){return new r(this.x+t.x,this.y+t.y)}eq(t){return this.x===t.x&&this.y===t.y}clone(){return new r(this.x,this.y)}static from(t){if("number"==typeof t)return new r(t,t);if(Array.isArray(t))return new r(t[0]??0,t[1]??0);if("object"==typeof t&&"number"==typeof t.x&&"number"==typeof t.y)return new r(t.x,t.y);throw Error("Wrong Vec2 source")}}class n{size;constructor(t){if(t.x<=0||t.y<=0)throw Error(`Invalid grid size: width and height must be positive (got ${t.x}x${t.y})`);this.size=t}getIndexFromVec(t){return n.getIndexFromVec(t,this.size.width)}getVecFromIndex(t){return n.getVecFromIndex(t,this.size.width)}getAllIndexes(){return n.getAllIndexes(this.size)}getAllCells(){return n.getAllCells(this.size)}isBlack(t){return!!((t.x+t.y)%2)}isWhite(t){return!((t.x+t.y)%2)}isInGrid(t){return n.isInGrid(t,this.size)}static getAllIndexes(t){return Array(t.width*t.height).fill(!0).map(((t,e)=>e))}static getAllCells(t){return Array(t.width*t.height).fill(!0).map(((e,i)=>n.getVecFromIndex(i,t.width)))}static getVecFromIndex(t,e){const i=e instanceof r?e.width:e;return new r(t%i,Math.floor(t/i))}static getIndexFromVec(t,e){return t.y*(e instanceof r?e.width:e)+t.x}static isInGrid(t,e){return t.x>=0&&e.x>t.x&&t.y>=0&&e.y>t.y}static getOrtoSiblings(t,e){return[new r(-1,0),new r(1,0),new r(0,-1),new r(0,1)].map((e=>e.add(t))).filter((t=>n.isInGrid(t,e)))}}const h=e(class extends i{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow()}size=new r(3,3);tiles=[];freeMove=!1;async checkAnswer(){return this.tiles.every((t=>t.position===t.correctPosition))}getTileById(t){const e=this.tiles.find((e=>e.id===t));if(!e)throw Error("Tile not found");return e}onTileClicked(t){const e=this.getTileById(t);if(e.blocked)return;const i=n.getVecFromIndex(e.position,this.size.width),s=this.getMoveTarget(i);s&&this.setTilePosition(e,n.getIndexFromVec(s,this.size))}getMoveTarget(t){return this.freeMove?n.getAllCells(this.size).find((t=>{const e=n.getIndexFromVec(t,this.size.width);return this.tiles.every((t=>t.position!==e))}))??null:n.getOrtoSiblings(t,this.size).find((t=>{const e=n.getIndexFromVec(t,this.size.width);return this.tiles.every((t=>t.position!==e))}))??null}setTilePosition(t,e){const i="string"==typeof t?t:t.id;this.tiles=this.tiles.map((t=>t.id!==i?t:{...t,position:e}))}render(){return s("div",{key:"d6042863922edef06b51cf3897c4cdad5b39b3c7",class:"board",style:{"--width":""+this.size.width,"--height":""+this.size.height}},this.tiles.map((t=>{const e=n.getVecFromIndex(t.position,this.size.width);return s("button",{key:t.id,class:"tile",style:{"--x":""+e.x,"--y":""+e.y},onClick:this.onTileClicked.bind(this,t.id)},s("img",{src:t.imageSrc,alt:""}))})))}static get style(){return".board{width:100%;height:100%;display:inline-block;aspect-ratio:calc(var(--width, 1) / var(--height, 1));border:1px solid black;position:relative}.tile{position:absolute;width:calc(100% / var(--width, 1));height:calc(100% / var(--height, 1));padding:0;border:none;transform:translate(calc(100% * var(--x)), calc(100% * var(--y)));transition:transform 0.5s}.tile img{display:block;width:100%;height:100%;pointer-events:none}"}},[513,"mb-sliding-puzzle",{tiles:[1040],freeMove:[4,"free-move"],size:[32],checkAnswer:[64]}]);function o(){"undefined"!=typeof customElements&&["mb-sliding-puzzle"].forEach((e=>{"mb-sliding-puzzle"===e&&(customElements.get(t(e))||customElements.define(t(e),h))}))}o();const c=h,l=o;export{c as MbSlidingPuzzle,l as defineCustomElement}
|
|
@@ -142,6 +142,8 @@ const MbSlidingPuzzle = class {
|
|
|
142
142
|
}
|
|
143
143
|
onTileClicked(id) {
|
|
144
144
|
const tile = this.getTileById(id);
|
|
145
|
+
if (tile.blocked)
|
|
146
|
+
return;
|
|
145
147
|
const v = Grid.getVecFromIndex(tile.position, this.size.width);
|
|
146
148
|
const target = this.getMoveTarget(v);
|
|
147
149
|
if (target) {
|
|
@@ -153,7 +155,6 @@ const MbSlidingPuzzle = class {
|
|
|
153
155
|
const cells = Grid.getAllCells(this.size);
|
|
154
156
|
const emptyCell = cells.find(c => {
|
|
155
157
|
const index = Grid.getIndexFromVec(c, this.size.width);
|
|
156
|
-
console.log(index);
|
|
157
158
|
return this.tiles.every(t => t.position !== index);
|
|
158
159
|
});
|
|
159
160
|
return emptyCell ?? null;
|
|
@@ -179,7 +180,7 @@ const MbSlidingPuzzle = class {
|
|
|
179
180
|
});
|
|
180
181
|
}
|
|
181
182
|
render() {
|
|
182
|
-
return h("div", { key: '
|
|
183
|
+
return h("div", { key: 'd6042863922edef06b51cf3897c4cdad5b39b3c7', class: "board", style: {
|
|
183
184
|
'--width': `${this.size.width}`,
|
|
184
185
|
'--height': `${this.size.height}`,
|
|
185
186
|
} }, this.tiles.map(tile => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{r as t,h as e}from"./p-D-FhYfR4.js";class i{x;y;get 0(){return this.x}set 0(t){this.x=t}get 1(){return this.y}set 1(t){this.y=t}get width(){return this.x}set width(t){this.x=t}get height(){return this.y}set height(t){this.y=t}get xy(){return[this.x,this.y]}*[Symbol.iterator](){yield this.x,yield this.y}constructor(t,e){this.x=t,this.y=e}add(t){return new i(this.x+t.x,this.y+t.y)}eq(t){return this.x===t.x&&this.y===t.y}clone(){return new i(this.x,this.y)}static from(t){if("number"==typeof t)return new i(t,t);if(Array.isArray(t))return new i(t[0]??0,t[1]??0);if("object"==typeof t&&"number"==typeof t.x&&"number"==typeof t.y)return new i(t.x,t.y);throw new Error("Wrong Vec2 source")}}class r{size;constructor(t){if(t.x<=0||t.y<=0)throw new Error(`Invalid grid size: width and height must be positive (got ${t.x}x${t.y})`);this.size=t}getIndexFromVec(t){return r.getIndexFromVec(t,this.size.width)}getVecFromIndex(t){return r.getVecFromIndex(t,this.size.width)}getAllIndexes(){return r.getAllIndexes(this.size)}getAllCells(){return r.getAllCells(this.size)}isBlack(t){return Boolean((t.x+t.y)%2)}isWhite(t){return!Boolean((t.x+t.y)%2)}isInGrid(t){return r.isInGrid(t,this.size)}static getAllIndexes(t){return Array(t.width*t.height).fill(!0).map(((t,e)=>e))}static getAllCells(t){return Array(t.width*t.height).fill(!0).map(((e,i)=>r.getVecFromIndex(i,t.width)))}static getVecFromIndex(t,e){const r=e instanceof i?e.width:e,s=t%r,n=Math.floor(t/r);return new i(s,n)}static getIndexFromVec(t,e){return t.y*(e instanceof i?e.width:e)+t.x}static isInGrid(t,e){return t.x>=0&&e.x>t.x&&t.y>=0&&e.y>t.y}static getOrtoSiblings(t,e){return[new i(-1,0),new i(1,0),new i(0,-1),new i(0,1)].map((e=>e.add(t))).filter((t=>r.isInGrid(t,e)))}}const s=class{constructor(e){t(this,e)}size=new i(3,3);tiles=[];freeMove=!1;async checkAnswer(){return this.tiles.every((t=>t.position===t.correctPosition))}getTileById(t){const e=this.tiles.find((e=>e.id===t));if(!e)throw new Error("Tile not found");return e}onTileClicked(t){const e=this.getTileById(t)
|
|
1
|
+
import{r as t,h as e}from"./p-D-FhYfR4.js";class i{x;y;get 0(){return this.x}set 0(t){this.x=t}get 1(){return this.y}set 1(t){this.y=t}get width(){return this.x}set width(t){this.x=t}get height(){return this.y}set height(t){this.y=t}get xy(){return[this.x,this.y]}*[Symbol.iterator](){yield this.x,yield this.y}constructor(t,e){this.x=t,this.y=e}add(t){return new i(this.x+t.x,this.y+t.y)}eq(t){return this.x===t.x&&this.y===t.y}clone(){return new i(this.x,this.y)}static from(t){if("number"==typeof t)return new i(t,t);if(Array.isArray(t))return new i(t[0]??0,t[1]??0);if("object"==typeof t&&"number"==typeof t.x&&"number"==typeof t.y)return new i(t.x,t.y);throw new Error("Wrong Vec2 source")}}class r{size;constructor(t){if(t.x<=0||t.y<=0)throw new Error(`Invalid grid size: width and height must be positive (got ${t.x}x${t.y})`);this.size=t}getIndexFromVec(t){return r.getIndexFromVec(t,this.size.width)}getVecFromIndex(t){return r.getVecFromIndex(t,this.size.width)}getAllIndexes(){return r.getAllIndexes(this.size)}getAllCells(){return r.getAllCells(this.size)}isBlack(t){return Boolean((t.x+t.y)%2)}isWhite(t){return!Boolean((t.x+t.y)%2)}isInGrid(t){return r.isInGrid(t,this.size)}static getAllIndexes(t){return Array(t.width*t.height).fill(!0).map(((t,e)=>e))}static getAllCells(t){return Array(t.width*t.height).fill(!0).map(((e,i)=>r.getVecFromIndex(i,t.width)))}static getVecFromIndex(t,e){const r=e instanceof i?e.width:e,s=t%r,n=Math.floor(t/r);return new i(s,n)}static getIndexFromVec(t,e){return t.y*(e instanceof i?e.width:e)+t.x}static isInGrid(t,e){return t.x>=0&&e.x>t.x&&t.y>=0&&e.y>t.y}static getOrtoSiblings(t,e){return[new i(-1,0),new i(1,0),new i(0,-1),new i(0,1)].map((e=>e.add(t))).filter((t=>r.isInGrid(t,e)))}}const s=class{constructor(e){t(this,e)}size=new i(3,3);tiles=[];freeMove=!1;async checkAnswer(){return this.tiles.every((t=>t.position===t.correctPosition))}getTileById(t){const e=this.tiles.find((e=>e.id===t));if(!e)throw new Error("Tile not found");return e}onTileClicked(t){const e=this.getTileById(t);if(e.blocked)return;const i=r.getVecFromIndex(e.position,this.size.width),s=this.getMoveTarget(i);s&&this.setTilePosition(e,r.getIndexFromVec(s,this.size))}getMoveTarget(t){return this.freeMove?r.getAllCells(this.size).find((t=>{const e=r.getIndexFromVec(t,this.size.width);return this.tiles.every((t=>t.position!==e))}))??null:r.getOrtoSiblings(t,this.size).find((t=>{const e=r.getIndexFromVec(t,this.size.width);return this.tiles.every((t=>t.position!==e))}))??null}setTilePosition(t,e){const i="string"==typeof t?t:t.id;this.tiles=this.tiles.map((t=>t.id!==i?t:{...t,position:e}))}render(){return e("div",{key:"d6042863922edef06b51cf3897c4cdad5b39b3c7",class:"board",style:{"--width":`${this.size.width}`,"--height":`${this.size.height}`}},this.tiles.map((t=>{const i=r.getVecFromIndex(t.position,this.size.width);return e("button",{key:t.id,class:"tile",style:{"--x":`${i.x}`,"--y":`${i.y}`},onClick:this.onTileClicked.bind(this,t.id)},e("img",{src:t.imageSrc,alt:""}))})))}};s.style=".board{width:100%;height:100%;display:inline-block;aspect-ratio:calc(var(--width, 1) / var(--height, 1));border:1px solid black;position:relative}.tile{position:absolute;width:calc(100% / var(--width, 1));height:calc(100% / var(--height, 1));padding:0;border:none;transform:translate(calc(100% * var(--x)), calc(100% * var(--y)));transition:transform 0.5s}.tile img{display:block;width:100%;height:100%;pointer-events:none}";export{s as mb_sliding_puzzle}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{p as e,b as r}from"./p-D-FhYfR4.js";export{s as setNonce}from"./p-D-FhYfR4.js";import{g as o}from"./p-DQuL1Twl.js";(()=>{const s=import.meta.url,r={};return""!==s&&(r.resourcesUrl=new URL(".",s).href),e(r)})().then((async e=>(await o(),r([["p-
|
|
1
|
+
import{p as e,b as r}from"./p-D-FhYfR4.js";export{s as setNonce}from"./p-D-FhYfR4.js";import{g as o}from"./p-DQuL1Twl.js";(()=>{const s=import.meta.url,r={};return""!==s&&(r.resourcesUrl=new URL(".",s).href),e(r)})().then((async e=>(await o(),r([["p-a976746c",[[513,"mb-sliding-puzzle",{tiles:[1040],freeMove:[4,"free-move"],size:[32],checkAnswer:[64]}]]]],e))));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mborecki/sliding-puzzle",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Sliding Puzzle",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"tslib": "^2.8.1",
|
|
41
41
|
"typescript": "5.9.3",
|
|
42
42
|
"vitest": "4.0.16",
|
|
43
|
-
"@mb-puzzle/
|
|
44
|
-
"@mb-puzzle/
|
|
43
|
+
"@mb-puzzle/grid": "0.1.0",
|
|
44
|
+
"@mb-puzzle/vec2": "0.5.0"
|
|
45
45
|
},
|
|
46
46
|
"license": "MIT",
|
|
47
47
|
"scripts": {
|