@@ -8,6 +8,30 @@ function _() {
8
8
return m + ":" + s ;
9
9
}
10
10
11
+ function resetBoard ( ) {
12
+ const tiles = [
13
+ "row1 col1" ,
14
+ "row1 col2" ,
15
+ "row1 col3" ,
16
+ "row2 col1" ,
17
+ "row2 col2" ,
18
+ "row2 col3" ,
19
+ "row3 col1" ,
20
+ "row3 col2" ,
21
+ "row3 col3" ,
22
+ ] ;
23
+ let boardTiles = document . getElementById ( "board" ) . children ;
24
+ for ( let row = 0 ; row < boardTiles . length ; row ++ ) {
25
+ for ( let col = 0 ; col < boardTiles [ row ] . children . length ; col ++ ) {
26
+ boardTiles [ row ] . children [ col ] . className = tiles [ row * 3 + col ] + " tile" ;
27
+ }
28
+ }
29
+
30
+ document . getElementById ( "moves" ) . innerHTML = "0" ;
31
+ document . getElementById ( "time" ) . innerHTML = "0:00" ;
32
+ document . getElementById ( "start" ) . innerHTML = "Play" ;
33
+ }
34
+
11
35
function shuffleBoard ( blank ) {
12
36
const tiles = [
13
37
"row1 col1" ,
@@ -121,7 +145,7 @@ function _() {
121
145
document . getElementById ( "time" ) . innerHTML = "0:00" ;
122
146
123
147
const blank = Math . floor ( Math . random ( ) * 9 ) ;
124
- document . getElementById ( "start" ) . innerHTML = "restart " ;
148
+ document . getElementById ( "start" ) . innerHTML = "Restart " ;
125
149
shuffleBoard ( blank ) ;
126
150
127
151
clearInterval ( interval ) ;
@@ -175,7 +199,6 @@ function _() {
175
199
} ) ;
176
200
} ) ;
177
201
}
178
-
179
202
document . getElementById ( "start" ) . addEventListener ( "click" , play ) ;
180
203
181
204
window . onload = ( ) => {
@@ -187,5 +210,32 @@ function _() {
187
210
bestTime = bestTime === null ? "NA" : displayTime ( bestTime ) ;
188
211
document . getElementById ( "bestTime" ) . innerHTML = bestTime ;
189
212
}
213
+
214
+ document . querySelectorAll ( "#images button" ) . forEach ( button => {
215
+ button . addEventListener ( "click" , ( ) => {
216
+ document . querySelectorAll ( ".tile" ) . forEach ( tile => {
217
+ tile . style . backgroundImage = "url(" + button . getAttribute ( "data-src" ) + ")" ;
218
+ } ) ;
219
+ clearInterval ( interval ) ;
220
+ resetBoard ( ) ;
221
+ } ) ;
222
+ } ) ;
223
+
224
+ document . getElementById ( "img-form" ) . onsubmit = function ( e ) {
225
+ const files = e . target . children [ 0 ] . files ;
226
+
227
+ var fr = new FileReader ( ) ;
228
+ fr . onload = function ( ) {
229
+ document . querySelectorAll ( ".tile" ) . forEach ( tile => {
230
+ tile . style . backgroundImage = "url(" + fr . result + ")" ;
231
+ } ) ;
232
+ }
233
+ fr . readAsDataURL ( files [ 0 ] ) ;
234
+
235
+ clearInterval ( interval ) ;
236
+ resetBoard ( ) ;
237
+
238
+ e . preventDefault ( ) ;
239
+ }
190
240
}
191
241
_ ( ) ;
0 commit comments