Skip to content

Commit b9e6e19

Browse files
author
Simon he
committed
fix: can't match
1 parent 8b33dfe commit b9e6e19

File tree

6 files changed

+76
-51
lines changed

6 files changed

+76
-51
lines changed

src/App.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
:infinity="true"
1010
justify-center
1111
:stable="true"
12-
m-y-10
12+
m-y-15
1313
>
1414
</vivid-typing>
1515
<matching-picture></matching-picture>
@@ -36,7 +36,6 @@
3636
<script setup lang="ts">
3737
import { baseImage } from "./request";
3838
import { loading } from "./config";
39-
baseImage();
4039
const Animals = [
4140
'<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" style="vertical-align: -0.125em;" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 64 64"><path fill="currentColor" d="M56 13.641C56 7.222 50.644 2 44.062 2c-5.25 0-9.835 3.388-11.376 8.14a9.768 9.768 0 0 0-1.371 0C29.772 5.388 25.187 2 19.938 2C13.355 2 8 7.222 8 13.641c0 5.455 3.885 10.057 9.1 11.305c-1.628 3.754-4.579 11.734-4.579 20.273c0 .158.007.305.009.457c-.379.316-.75.641-1.095.998c-.703.73-1.344 1.547-1.853 2.486a8.282 8.282 0 0 0-.633 1.508a6.496 6.496 0 0 0-.277 1.68c-.016.582.057 1.17.216 1.729a5.99 5.99 0 0 0 .694 1.553c.589.943 1.363 1.67 2.164 2.264c.401.297.819.566 1.232.803a15.954 15.954 0 0 0 3.994 1.637c1.851.48 3.707.635 5.518.717c1.813.082 3.602.094 5.389.146c1.788.047 3.57.123 5.352.256a92.14 92.14 0 0 1 5.34.549a94.415 94.415 0 0 0-5.275-1.053a116.259 116.259 0 0 0-5.335-.766c-1.784-.221-3.57-.402-5.322-.646c-1.748-.248-3.466-.566-5.033-1.139a13.71 13.71 0 0 1-2.236-1.047c-.35-.207-.698-.432-1.025-.666a9.985 9.985 0 0 1-.928-.742c-.573-.516-1.042-1.084-1.323-1.668c-.282-.582-.373-1.156-.292-1.732s.336-1.168.724-1.736c.132-.193.294-.379.455-.564c.887 4.176 3.093 6.426 6.834 7.053c.739 1.016 1.965 1.656 3.319 1.656h1.475c1.42 0 2.7-.723 3.423-1.818c1.248-.098 2.593-.184 3.97-.184c1.265 0 2.513.074 3.772.17c.72 1.105 2.003 1.832 3.433 1.832h1.476a4.107 4.107 0 0 0 3.294-1.625c5.179-.775 7.505-4.561 7.505-12.105c0-8.547-2.951-16.523-4.579-20.274C52.115 23.697 56 19.096 56 13.641m-44.161 1.165c0-3.863 3.233-7.006 7.207-7.006c3.09 0 5.794 1.914 6.793 4.678c-2.635 2.011-5.06 5.139-7.228 9.322c-3.772-.219-6.772-3.273-6.772-6.994M49.56 45.219c0 8.066-2.672 10.072-6.818 10.346c-.233.861-1.063 1.502-2.061 1.502h-1.476c-1.082 0-1.967-.756-2.104-1.732c-1.615-.133-3.327-.268-5.102-.268c-1.844 0-3.621.145-5.291.283c-.144.969-1.025 1.717-2.103 1.717H23.13c-1.001 0-1.837-.646-2.064-1.516c-4.037-.322-6.626-2.391-6.626-10.332c0-9.559 2.548-16.237 5.092-21.003c1.708-3.199 5.745-9.833 9.042-11.342c2.075-.949 4.616-1.033 6.849-.001c3.293 1.522 7.337 8.146 9.044 11.346c2.544 4.766 5.093 11.445 5.093 21M45.388 21.8c-2.19-4.24-4.568-7.31-7.228-9.327c1-2.761 3.704-4.673 6.792-4.673c3.975 0 7.208 3.143 7.208 7.006c0 3.721-2.999 6.775-6.772 6.994"/><path fill="currentColor" d="M32.001 33.152c-.51 0-2.214.045-2.77.619c-.395.408-.089 1.422.962 2.295c.665.553 1.298.727 1.808.727s1.144-.174 1.807-.727c1.052-.873 1.357-1.887.962-2.295c-.555-.574-2.26-.619-2.769-.619"/><ellipse cx="25.429" cy="27.143" fill="currentColor" rx="2.881" ry="2.823"/><ellipse cx="38.57" cy="27.143" fill="currentColor" rx="2.88" ry="2.823"/><path fill="currentColor" d="M32.975 37.67a2.51 2.51 0 0 0-.792.189c-.242.111-.476.24-.679.42c-.198.172-.405.395-.461.688c.259-.102.51-.127.741-.213c.236-.068.459-.16.683-.246c.219-.1.436-.193.646-.303c.219-.09.4-.248.678-.279c-.197-.242-.552-.272-.816-.256"/></svg>',
4241
'<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" style="vertical-align: -0.125em;" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 64 64"><path fill="currentColor" d="m61.989 16.547l-.027-.933l-.919-.021c-2.791-.073-4.992.396-6.369 1.333l-.172.117l-.11.179c-.735 1.195-1.156 3.005-1.228 5.255a25.705 25.705 0 0 1-3.791 2.65c-1.272-.799-2.77-1.407-4.342-1.797c.029-.018.064-.035.094-.053l.178-.113l.115-.177c1.376-2.108 2.035-5.487 1.904-9.769l-.028-.932l-.919-.021a31.742 31.742 0 0 0-3.061.072C45.303 10.241 46.688 6.981 46.525 2c0 0-2.908 8.135-9.72 8.844a39.004 39.004 0 0 0-.671-.756L36 9.939c-1.298-1.464-2.868-2.501-11.29-2.501c-8.136 0-9.742 1.233-10.995 2.844a8.558 8.558 0 0 1-.254.316c-.082.096-.155.191-.236.287C5.98 10.616 2.897 2 2.897 2c-.164 5.015 1.243 8.283 3.252 10.378a32.364 32.364 0 0 0-3.199-.063l-.919.026l-.021.933c-.102 4.217.598 7.672 1.97 9.727l.117.175l.178.111c.799.499 1.806.906 3.001 1.212c.087 1.508 1.065 3.018 2.099 4.614c1.166 1.8 2.358 3.66 2.444 5.614c-.863 1.624-.868 3.398-.868 4.718c0 .704.099 1.331.28 1.896v14.775c0 3.244 2.559 5.884 5.703 5.884c3.14 0 5.693-2.64 5.693-5.884v-7.082h4.157v7.072c0 3.25 2.56 5.894 5.706 5.894c2.966 0 5.409-2.347 5.682-5.337c2.314-.023 4.189-1.968 4.189-4.359v-3.322a14.718 14.718 0 0 0 2.578-.359v4.05c0 3.007 2.369 5.453 5.281 5.453c2.914 0 5.285-2.446 5.285-5.453V35.948c0-3.152-1.178-6.122-3.34-8.488a27.54 27.54 0 0 0 3.581-2.69c2.019-.119 3.649-.566 4.731-1.3l.174-.118l.109-.18c1.125-1.826 1.289-4.61 1.229-6.625M9.208 24.283c0-.175.006-.351.013-.527l.034-.836l-.808-.167c-1.221-.253-2.221-.6-2.976-1.032c-.979-1.595-1.514-4.207-1.532-7.453c3.086-.008 5.569.409 7.275 1.214l.743.351l.194-.286c2.944.587 5.52.083 5.799-.392c.902-1.531.3-4.877-1.705-4.591c1.053-.63 3.183-1.158 8.466-1.158c6.48 0 8.407.639 9.293 1.314a11.465 11.465 0 0 1-.643-.131c-2.154-.527-2.816 2.989-1.887 4.566c.297.503 3.178 1.039 6.342.269l.102.152l.725-.293c1.693-.684 4.016-1.046 6.758-1.059c.002 3.253-.516 5.881-1.485 7.497c-.74.431-1.729.781-2.94 1.042l-.784.169l.026 1.352c0 1.013-.855 2.433-1.761 3.937c-.87 1.445-1.811 3.026-2.308 4.71c-.05-.042-.096-.087-.148-.128c-.354-1.492-1.156-2.612-2.286-3.179c-1.464-.731-3.314-.421-4.985.83a41.229 41.229 0 0 0-4.014-.181c-1.45 0-2.797.061-4.012.18c-1.67-1.25-3.519-1.563-4.986-.828c-1.124.563-1.921 1.676-2.278 3.152c-.507-1.735-1.515-3.311-2.445-4.745c-.92-1.418-1.787-2.758-1.787-3.749m3.681 15.161c0-2.195.145-3.978 1.968-5.268l.328-.232l.071-.4c.188-1.059.657-1.824 1.319-2.156c.256-.129.54-.193.845-.193c.73 0 1.576.369 2.383 1.039l.314.262l.404-.045c1.244-.14 2.655-.21 4.193-.21c1.536 0 2.947.07 4.195.21l.404.045l.315-.262c1.143-.949 2.362-1.277 3.227-.846c.662.332 1.13 1.097 1.318 2.156l.071.4l.328.232c1.824 1.29 1.969 3.072 1.969 5.268c0 2.293-.913 4.529-11.828 4.529c-10.911.001-11.824-2.236-11.824-4.529m27.535 12.86h-2.225v-3.27h2.225v3.27m13.142-16.356v16.724h-6.688v-6.71l-1.272.426c-1.347.451-2.734.68-4.122.68h-5.222v9.039h-7.539v-9.039H20.69v9.049h-7.521V43.896c2.345 1.617 6.411 2.046 11.546 2.046c8.264 0 13.767-1.104 13.767-6.497c0-1.248-.005-2.906-.737-4.456c.074-1.922 1.233-3.863 2.365-5.743c.928-1.539 1.801-3.003 1.996-4.402c2.434.168 4.873.977 6.678 2.242l.491.345l.024-.013l.648.587c2.334 2.113 3.619 4.934 3.619 7.943m5.659-14.002c-.854.5-2.229.81-3.905.875l-.225.009c.03-1.886.325-3.426.835-4.381c.896-.527 2.358-.839 4.13-.888c-.028 1.889-.318 3.416-.835 4.385"/><path fill="currentColor" d="M42.93 20.804c.713-1.093 1.043-2.917.975-5.138c-1.846-.043-3.395.177-4.497.618a15.69 15.69 0 0 1 1.612 5.234c.771-.167 1.423-.404 1.91-.714M5.424 15.667c-.053 2.267.309 4.095 1.018 5.157c.532.332 1.235.576 2.065.737c.314-1.597.929-3.271 1.944-5.083c-1.146-.598-2.907-.874-5.027-.811"/><ellipse cx="33.193" cy="22.569" fill="currentColor" rx="3.611" ry="3.909"/><ellipse cx="16.231" cy="22.569" fill="currentColor" rx="3.612" ry="3.909"/><path fill="currentColor" d="M28.275 41.747c1.572.788 3.797-.633 4.965-3.173c1.172-2.541.846-5.24-.727-6.028c-1.573-.787-3.795.635-4.967 3.174c-1.173 2.542-.845 5.24.729 6.027m-7.117 0c1.571-.787 1.898-3.485.727-6.025c-1.167-2.541-3.393-3.963-4.964-3.176c-1.575.789-1.9 3.487-.729 6.028c1.17 2.54 3.394 3.961 4.966 3.173"/></svg>',

src/components/MatchingPicture.vue

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,25 @@ function match(block: Block) {
1919
}
2020
2121
if (canEliminate(block)) {
22-
block.url = emptyFlag;
23-
pre.value.url = emptyFlag;
22+
block.disappear = true;
23+
pre.value.disappear = true;
24+
const _pre = pre.value;
25+
setTimeout(() => {
26+
block.url = emptyFlag;
27+
_pre.url = emptyFlag;
28+
block.disappear = false;
29+
_pre.disappear = false;
30+
if (isWin()) {
31+
setTimeout(() => {
32+
alert("You Win!");
33+
baseImage();
34+
});
35+
}
36+
}, 200);
2437
}
2538
block.animate = false;
2639
pre.value.animate = false;
2740
pre.value = {};
28-
if (isWin()) {
29-
alert("You Win!");
30-
}
3141
}
3242
3343
function canEliminate(block: Block) {
@@ -39,17 +49,13 @@ function bfs(block, target, path = new Set()) {
3949
const queue = [block];
4050
while (queue.length) {
4151
const cur = queue.shift();
42-
if (path.has(cur)) {
43-
continue;
44-
}
45-
path.add(cur);
4652
const y = cur.y;
4753
const x = cur.x;
4854
// 处理边界
4955
if (y > 0) {
5056
const top = copyArray[y - 1][x];
51-
top.type = "down";
52-
top.pre = JSON.parse(JSON.stringify(cur));
57+
const types: string[] = JSON.parse(JSON.stringify(cur.types || []));
58+
top.types = types ? ["down", ...types] : ["down"];
5359
if (preReturn(top)) {
5460
if (top.pos === target.pos) {
5561
console.log("find", top);
@@ -62,8 +68,8 @@ function bfs(block, target, path = new Set()) {
6268
}
6369
if (y < copyArray.length - 1) {
6470
const bottom = copyArray[y + 1][x];
65-
bottom.type = "up";
66-
bottom.pre = JSON.parse(JSON.stringify(cur));
71+
const types: string[] = JSON.parse(JSON.stringify(cur.types || []));
72+
bottom.types = types ? ["up", ...types] : ["up"];
6773
if (preReturn(bottom)) {
6874
if (bottom.pos === target.pos) {
6975
console.log("find", bottom);
@@ -76,8 +82,8 @@ function bfs(block, target, path = new Set()) {
7682
}
7783
if (x > 0) {
7884
const left = copyArray[y][x - 1];
79-
left.type = "right";
80-
left.pre = JSON.parse(JSON.stringify(cur));
85+
const types: string[] = JSON.parse(JSON.stringify(cur.types || []));
86+
left.types = types ? ["right", ...types] : ["right"];
8187
if (preReturn(left)) {
8288
if (left.pos === target.pos) {
8389
console.log("find", left);
@@ -90,8 +96,9 @@ function bfs(block, target, path = new Set()) {
9096
}
9197
if (x < copyArray[0].length - 1) {
9298
const right = copyArray[y][x + 1];
93-
right.type = "left";
94-
right.pre = JSON.parse(JSON.stringify(cur));
99+
const types: string[] = JSON.parse(JSON.stringify(cur.types || []));
100+
right.types = types ? ["left", ...types] : ["left"];
101+
95102
if (preReturn(right)) {
96103
if (right.pos === target.pos) {
97104
console.log("find", right);
@@ -109,23 +116,20 @@ function bfs(block, target, path = new Set()) {
109116
function preReturn(block) {
110117
let count = 0;
111118
let pre = null,
112-
current = null;
113-
const queue = [block];
114-
while (queue.length) {
115-
const block = queue.shift();
116-
current = block.type;
117-
if (pre === null) {
118-
pre = current;
119-
} else {
120-
if (pre !== current) {
121-
count++;
122-
pre = current;
123-
}
119+
cur = null;
120+
if (block.types.length < 4) {
121+
return true;
122+
}
123+
124+
for (let i = 1; i < block.types.length; i++) {
125+
cur = block.types[i];
126+
pre = block.types[i - 1];
127+
if (pre !== cur) {
128+
count++;
124129
}
125-
if (block.pre) {
126-
queue.push(block.pre);
130+
if (count > 2) {
131+
return false;
127132
}
128-
if (count > 3) return false;
129133
}
130134
return true;
131135
}
@@ -152,27 +156,38 @@ const sizeStyle = (block) => {
152156
return result;
153157
};
154158
155-
function newGame() {
156-
baseImage();
159+
function newGame(type: "Easy" | "Normal" | "Hard") {
160+
if (type === "Easy" && n.value !== 2) {
161+
n.value = 2;
162+
baseImage();
163+
} else if (type === "Normal" && n.value !== 3) {
164+
n.value = 3;
165+
baseImage();
166+
} else if (type === "Hard" && n.value !== 4) {
167+
n.value = 4;
168+
baseImage();
169+
}
157170
}
171+
newGame("Easy");
158172
</script>
159173

160174
<template>
161-
<div m-y-5 flex="~ gap-5" justify-center>
175+
<div m-y-5 flex="~ gap-1" justify-center>
162176
<button btn @click="reset">Rest</button>
163-
<button btn @click="newGame">New Game</button>
177+
<button btn @click="baseImage">New Game</button>
178+
<button btn @click="newGame('Easy')">Easy</button>
179+
<button btn @click="newGame('Normal')">Normal</button>
180+
<button btn @click="newGame('Hard')">Hard</button>
164181
</div>
165182
<div
166183
v-for="(row, y) in arrayPic"
167184
:key="y"
168185
flex="~"
169186
items-center
170187
justify-center
171-
w-max
172188
ma
173-
m-l--20
174189
:style="{
175-
'margin-bottom': !loading && y === 2 * n + 1 && -height + 'rem',
190+
'margin-bottom': !loading && y === 2 * n + 1 ? -height + 'rem' : '',
176191
'margin-top': !loading && y === 0 && -height + 'rem',
177192
}"
178193
>
@@ -188,8 +203,7 @@ function newGame() {
188203
:class="[
189204
'bg-gray-500/10',
190205
'hover:bg-gray-500/20',
191-
block?.animateY ? 'animate-shake-y' : '',
192-
block?.animateX ? 'animate-shake-x' : '',
206+
block?.disappear ? 'animate-fade-out' : '',
193207
]"
194208
:src="block?.url"
195209
@click="match(block)"

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const currentImage = ref<number>(0)
44
export const base64 = ref<string>('')
55
export const arrayPic = ref<Array<Block[]>>([])
66
export const ratio = ref<number>(0)
7-
export const n = ref<number>(4)
7+
export const n = ref<number>(0)
88
export const loading = ref<boolean>(true)
99
export const emptyFlag = "./empty.png";
1010
export const height = computed(() => 1 / ratio.value * 5);

src/pic.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { emptyFlag, arrayPic } from './config';
2+
import type { Block } from './type';
23

34
const pictureMap = new Map<string, any>()
45

@@ -27,7 +28,7 @@ async function generateImage(image: any, n: number) {
2728
const w = image.width / n;
2829
const h = image.height / n;
2930
let count = 0
30-
31+
const cacheUrl = new Map<string, string>()
3132
for (let j = 0; j <= 2 * n + 1; j++) {
3233
const col = [];
3334
for (let i = 0; i <= n + 1; i++) {
@@ -39,11 +40,21 @@ async function generateImage(image: any, n: number) {
3940
})
4041
continue
4142
}
43+
let pos = ''
44+
4245
if (j > n)
43-
ctx.drawImage(image, (i - 1) * w, (j - n - 1) * h, w, h, 0, 0, w, h);
46+
pos = JSON.stringify([i - 1, j - n - 1])
4447
else
48+
pos = JSON.stringify([i - 1, j - 1])
49+
50+
let url: string = ''
51+
if (cacheUrl.has(pos)) {
52+
url = cacheUrl.get(pos)!
53+
} else {
4554
ctx.drawImage(image, (i - 1) * w, (j - 1) * h, w, h, 0, 0, w, h);
46-
const url = await PicSpace(canvas.toDataURL("image/png"))
55+
url = await PicSpace(canvas.toDataURL("image/png")) as string
56+
cacheUrl.set(pos, url)
57+
}
4758
const block = {
4859
url,
4960
x: i,
@@ -119,15 +130,16 @@ export async function initData(n: number, src: string) {
119130

120131
export function reset() {
121132
const numbers: any[] = []
122-
const copyArrayPic = JSON.parse(JSON.stringify(arrayPic.value))
133+
const copyArrayPic: Array<Block[]> = JSON.parse(JSON.stringify(arrayPic.value))
134+
135+
console.log(arrayPic.value)
123136
arrayPic.value.forEach(row => {
124137
row.forEach(block => {
125138
if (block.url !== emptyFlag) {
126139
numbers.push(block)
127140
}
128141
})
129142
})
130-
debugger
131143
copyArrayPic.forEach(row => {
132144
row.forEach(block => {
133145
if (block.url !== emptyFlag) {
@@ -137,7 +149,6 @@ export function reset() {
137149
}
138150
})
139151
})
140-
141152
console.log(copyArrayPic)
142153
arrayPic.value = copyArrayPic
143154
}

src/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async function dealPicture(baseUrl: string, resolve: any) {
5454
const image = new Image();
5555
image.src = base64.value;
5656
image.onload = () => {
57-
ratio.value = (image.width / image.height).toFixed(1);
57+
ratio.value = +(image.width / image.height).toFixed(1);
5858
};
5959
loading.value = false
6060
resolve('success')

src/type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export type Block = {
33
y: number,
44
pos: number,
55
animate: boolean | undefined
6+
disappear: boolean | undefined
67
url: string,
78
}

0 commit comments

Comments
 (0)