1
+ <script lang =" ts" >
2
+ export let blockInteraction: boolean = false ;
3
+ export let boardHeight: number = 0 ;
4
+ export let boardWidth: number = 0 ;
5
+ export let rowNum: number = 5 ;
6
+ export let colNum: number = 5 ;
7
+
8
+ type boardData = {
9
+ v: number
10
+ /*
11
+ -1: block
12
+ 0: empty
13
+ 1-4: number
14
+ */
15
+ w: boolean // confirmed wrong
16
+ }
17
+
18
+ let Val: boardData [][] = [[]];
19
+ rowNum = 5 ;
20
+ colNum = 5 ;
21
+ while (Val .length < rowNum ) {
22
+ let a: boardData [] = [];
23
+ while (a .length < colNum ) {
24
+ a .push ({v: 0 , w: false });
25
+ }
26
+ Val .push (a );
27
+ }
28
+
29
+ $ : {
30
+ while (Val .length > rowNum ) {
31
+ Val .pop ();
32
+ }
33
+
34
+ while (Val .length < rowNum ) {
35
+ let a: boardData [] = [];
36
+ while (a .length < colNum ) {
37
+ a .push ({v: 0 , w: false });
38
+ }
39
+ Val .push (a );
40
+ }
41
+
42
+ Val .forEach (e => {
43
+ while (e .length > colNum ) {
44
+ e .pop ();
45
+ }
46
+
47
+ while (e .length < colNum ) {
48
+ e .push ({v: 0 , w: false });
49
+ }
50
+ })
51
+
52
+ forceRefresh ();
53
+ }
54
+
55
+ $ : {
56
+ if (rowNum < 1 ) {
57
+ rowNum = 1 ;
58
+ } else if (Math .round (rowNum ) != rowNum ) {
59
+ rowNum = Math .round (rowNum );
60
+ }
61
+ }
62
+
63
+ $ : {
64
+ if (colNum < 1 ) {
65
+ colNum = 1 ;
66
+ } else if (Math .round (colNum ) != colNum ) {
67
+ colNum = Math .round (colNum );
68
+ }
69
+ }
70
+
71
+ function forceRefresh() {
72
+ Val = Val ;
73
+ }
74
+
75
+ function mainPanelRightClick(i : number , j : number ) {
76
+ if (blockInteraction ) {
77
+ blockInteraction = false ;
78
+ return ;
79
+ }
80
+
81
+ if (Val [i ][j ].v == 4 ) {
82
+ Val [i ][j ].v = - 1 ;
83
+ } else {
84
+ Val [i ][j ].v ++ ;
85
+ }
86
+ }
87
+
88
+ function mainPanelLeftClick(i : number , j : number ) {
89
+ if (blockInteraction ) {
90
+ blockInteraction = false ;
91
+ return ;
92
+ }
93
+
94
+ if (Val [i ][j ].v == - 1 ) {
95
+ Val [i ][j ].v = 4 ;
96
+ } else {
97
+ Val [i ][j ].v -- ;
98
+ }
99
+ }
100
+
101
+ export const boardController = {
102
+ preparePrinting(): Promise <void > {
103
+ return (Promise .resolve ());
104
+ },
105
+ cleanWrong(): void {
106
+ for (let i = 0 ; i < Val .length ; i ++ ) {
107
+ for (let j = 0 ; j < Val .length ; j ++ ) {
108
+ Val [i ][j ].w = false ;
109
+ }
110
+ }
111
+ },
112
+ initializeData(dt : any ): void {
113
+ Val = dt .map ((e : number []) => {
114
+ return (e .map (e2 => {
115
+ let a: boardData = {
116
+ v: e2 ,
117
+ w: false
118
+ }
119
+ return (a );
120
+ }))
121
+ });
122
+
123
+ colNum = Val .length ;
124
+ rowNum = Val [0 ].length ;
125
+ },
126
+ clearState(): void {
127
+ for (let i = 0 ; i < Val .length ; i ++ ) {
128
+ for (let j = 0 ; j < Val [i ].length ; j ++ ) {
129
+ Val [i ][j ].v = 0 ;
130
+ Val [i ][j ].w = false ;
131
+ }
132
+ }
133
+ },
134
+ getSavingData(): any {
135
+ return (Val .map (e => {
136
+ return e .map (e2 => {
137
+ return e2 .v ;
138
+ });
139
+ }));
140
+ },
141
+ checkPuzzle(): boolean {
142
+ let clear = true ;
143
+
144
+ for (let i = 0 ; i < Val .length ; i ++ ) {
145
+ for (let j = 0 ; j < Val [i ].length ; j ++ ) {
146
+ if (Val [i ][j ].v > 0 ) {
147
+ let emptyBox: number = 0 ;
148
+ if (i > 0 ) {
149
+ if (Val [i - 1 ][j ].v == 0 ) {
150
+ emptyBox ++ ;
151
+ }
152
+ }
153
+ if (j > 0 ) {
154
+ if (Val [i ][j - 1 ].v == 0 ) {
155
+ emptyBox ++ ;
156
+ }
157
+ }
158
+ if (i + 1 < colNum ) {
159
+ if (Val [i + 1 ][j ].v == 0 ) {
160
+ emptyBox ++ ;
161
+ }
162
+ }
163
+ if (j + 1 < rowNum ) {
164
+ if (Val [i ][j + 1 ].v == 0 ) {
165
+ emptyBox ++ ;
166
+ }
167
+ }
168
+ if (emptyBox < Val [i ][j ].v ) {
169
+ Val [i ][j ].w = true ;
170
+ clear = false ;
171
+ }
172
+ }
173
+ }
174
+ }
175
+
176
+ return clear ;
177
+ }
178
+ }
179
+ </script >
180
+
181
+ <div
182
+ id =" mainBoard"
183
+ style ="
184
+ display: grid;
185
+ grid-template-columns: repeat({colNum}, 1fr);
186
+ grid-template-rows: repeat({rowNum}, 1fr);
187
+ height: {boardHeight}px; width: {boardWidth}px;
188
+ font-size: inherit;
189
+ "
190
+ >
191
+ {#each Val as box , i }
192
+ {#each box as e , j }
193
+ <div id ={i .toString ()}
194
+ class ="boardCell" class:empty ={e .v == 0 } class:wrong ={e .w }
195
+ on:contextmenu |preventDefault ={() => mainPanelLeftClick (i , j )}
196
+ on:click ={() => mainPanelRightClick (i , j )}
197
+ >
198
+ { (e .v > 0 ) ? e .v : " " }
199
+ </div >
200
+ {/each }
201
+ {/each }
202
+ </div >
203
+
204
+ <style >
205
+ .boardCell {
206
+ cursor : pointer ;
207
+ display : flex ;
208
+ justify-content : center ;
209
+ align-items : center ;
210
+ font-size : inherit ;
211
+ border-width : 2px ;
212
+ border-style : solid ;
213
+ }
214
+ </style >
0 commit comments