@@ -19,15 +19,25 @@ function match(block: Block) {
19
19
}
20
20
21
21
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 );
24
37
}
25
38
block .animate = false ;
26
39
pre .value .animate = false ;
27
40
pre .value = {};
28
- if (isWin ()) {
29
- alert (" You Win!" );
30
- }
31
41
}
32
42
33
43
function canEliminate(block : Block ) {
@@ -39,17 +49,13 @@ function bfs(block, target, path = new Set()) {
39
49
const queue = [block ];
40
50
while (queue .length ) {
41
51
const cur = queue .shift ();
42
- if (path .has (cur )) {
43
- continue ;
44
- }
45
- path .add (cur );
46
52
const y = cur .y ;
47
53
const x = cur .x ;
48
54
// 处理边界
49
55
if (y > 0 ) {
50
56
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 " ] ;
53
59
if (preReturn (top )) {
54
60
if (top .pos === target .pos ) {
55
61
console .log (" find" , top );
@@ -62,8 +68,8 @@ function bfs(block, target, path = new Set()) {
62
68
}
63
69
if (y < copyArray .length - 1 ) {
64
70
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 " ] ;
67
73
if (preReturn (bottom )) {
68
74
if (bottom .pos === target .pos ) {
69
75
console .log (" find" , bottom );
@@ -76,8 +82,8 @@ function bfs(block, target, path = new Set()) {
76
82
}
77
83
if (x > 0 ) {
78
84
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 " ] ;
81
87
if (preReturn (left )) {
82
88
if (left .pos === target .pos ) {
83
89
console .log (" find" , left );
@@ -90,8 +96,9 @@ function bfs(block, target, path = new Set()) {
90
96
}
91
97
if (x < copyArray [0 ].length - 1 ) {
92
98
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
+
95
102
if (preReturn (right )) {
96
103
if (right .pos === target .pos ) {
97
104
console .log (" find" , right );
@@ -109,23 +116,20 @@ function bfs(block, target, path = new Set()) {
109
116
function preReturn(block ) {
110
117
let count = 0 ;
111
118
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 ++ ;
124
129
}
125
- if (block . pre ) {
126
- queue . push ( block . pre ) ;
130
+ if (count > 2 ) {
131
+ return false ;
127
132
}
128
- if (count > 3 ) return false ;
129
133
}
130
134
return true ;
131
135
}
@@ -152,27 +156,38 @@ const sizeStyle = (block) => {
152
156
return result ;
153
157
};
154
158
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
+ }
157
170
}
171
+ newGame (" Easy" );
158
172
</script >
159
173
160
174
<template >
161
- <div m-y-5 flex =" ~ gap-5 " justify-center >
175
+ <div m-y-5 flex =" ~ gap-1 " justify-center >
162
176
<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 >
164
181
</div >
165
182
<div
166
183
v-for =" (row, y) in arrayPic"
167
184
:key =" y"
168
185
flex =" ~"
169
186
items-center
170
187
justify-center
171
- w-max
172
188
ma
173
- m-l--20
174
189
:style =" {
175
- 'margin-bottom': !loading && y === 2 * n + 1 && -height + 'rem',
190
+ 'margin-bottom': !loading && y === 2 * n + 1 ? -height + 'rem' : ' ',
176
191
'margin-top': !loading && y === 0 && -height + 'rem',
177
192
}"
178
193
>
@@ -188,8 +203,7 @@ function newGame() {
188
203
:class =" [
189
204
'bg-gray-500/10',
190
205
'hover:bg-gray-500/20',
191
- block?.animateY ? 'animate-shake-y' : '',
192
- block?.animateX ? 'animate-shake-x' : '',
206
+ block?.disappear ? 'animate-fade-out' : '',
193
207
]"
194
208
:src =" block?.url"
195
209
@click =" match(block)"
0 commit comments