1
- import { INVALID_MOVE } from "boardgame.io/core" ;
2
1
import { Random } from "./Random" ;
3
2
4
3
function isValid ( data , size , x , y ) {
@@ -12,17 +11,17 @@ function isValid(data, size, x, y) {
12
11
}
13
12
14
13
function parseMove ( dx , dy , length , constraints , data , size , x , y ) {
15
- let moves = [ ] ;
16
- let orientation = [ ] ;
14
+ const moves = [ ] ;
15
+ const orientation = [ ] ;
17
16
const directions = [
18
17
[ - dx , - dy ] , // Forward
19
18
[ dx , dy ] , // Backward
20
19
[ - dy , dx ] , // Right
21
20
[ dy , - dx ] , // Left
22
21
] ;
23
- for ( let d of [ - 1 , 1 ] ) {
22
+ for ( const d of [ - 1 , 1 ] ) {
24
23
// For pieces like knights, we need to reverse the X for each direction
25
- for ( let rd in directions ) {
24
+ for ( const rd in directions ) {
26
25
if ( ( constraints & ( 2 ** rd ) ) === 0 ) {
27
26
continue ;
28
27
}
@@ -31,7 +30,7 @@ function parseMove(dx, dy, length, constraints, data, size, x, y) {
31
30
orientation . push ( nrd ) ;
32
31
}
33
32
}
34
- for ( let [ yi , xi ] of orientation ) {
33
+ for ( const [ yi , xi ] of orientation ) {
35
34
for ( let i = 1 ; i <= length ; i ++ ) {
36
35
if ( isValid ( data , size , x + i * xi , y + i * yi ) )
37
36
moves . push ( ( y + i * yi ) * size + ( x + i * xi ) ) ;
@@ -84,7 +83,7 @@ function parseNotation(notation, data, size, x, y) {
84
83
let length = 1 ; // Length we are doing
85
84
let moves = [ ] ;
86
85
let constraints = 15 ;
87
- for ( let s of notation ) {
86
+ for ( const s of notation ) {
88
87
if ( s === s . toLowerCase ( ) ) {
89
88
if ( dir !== null ) {
90
89
moves = moves . concat (
@@ -165,14 +164,14 @@ const pieceMovesCheck = {
165
164
} ;
166
165
167
166
export function fillPositions ( data ) {
168
- let size = Math . sqrt ( data . length ) ; // Boards are always squared
167
+ const size = Math . sqrt ( data . length ) ; // Boards are always squared
169
168
170
169
for ( let y = 0 ; y < size ; y ++ ) {
171
170
for ( let x = 0 ; x < size ; x ++ ) {
172
171
const value = data [ y * size + x ] ;
173
172
if ( ! Number . isInteger ( value ) ) {
174
- let moves = parseNotation ( pieceMovesCheck [ value ] , data , size , x , y ) ;
175
- for ( let move of moves ) {
173
+ const moves = parseNotation ( pieceMovesCheck [ value ] , data , size , x , y ) ;
174
+ for ( const move of moves ) {
176
175
data [ move ] ++ ;
177
176
}
178
177
}
@@ -183,18 +182,18 @@ export function fillPositions(data) {
183
182
}
184
183
185
184
export function generateBoard ( random , id , pieces , size , count ) {
186
- let piecesMdf = { } ;
187
- for ( let key in pieces ) {
185
+ const piecesMdf = { } ;
186
+ for ( const key in pieces ) {
188
187
piecesMdf [ key ] = pieces [ key ] ;
189
188
}
190
189
191
- let data = Array ( size * size ) . fill ( 0 ) ;
190
+ const data = Array ( size * size ) . fill ( 0 ) ;
192
191
let i = count ;
193
192
while ( i > 0 ) {
194
193
const rand = Math . floor ( random . next ( ) * ( size * size ) ) ;
195
194
if ( rand !== id && Number . isInteger ( data [ rand ] ) ) {
196
195
const value = Math . floor ( random . next ( ) * Object . keys ( piecesMdf ) . length ) ;
197
- let piece = Object . keys ( piecesMdf ) [ value ] ;
196
+ const piece = Object . keys ( piecesMdf ) [ value ] ;
198
197
199
198
if ( piecesMdf [ piece ] === 0 ) {
200
199
// We reached the amount of time we could spawn that piece
@@ -222,7 +221,7 @@ export function generateBoard(random, id, pieces, size, count) {
222
221
}
223
222
224
223
function validateBoard ( data , discovered , pieces , size ) {
225
- let thinkData = Array ( size * size ) . fill ( 0 ) ;
224
+ const thinkData = Array ( size * size ) . fill ( 0 ) ;
226
225
227
226
// For each tile...
228
227
for ( let i = 0 ; i < data . length ; i ++ ) {
@@ -232,10 +231,10 @@ function validateBoard(data, discovered, pieces, size) {
232
231
}
233
232
234
233
let str = "" ;
235
- for ( let piece of Object . keys ( pieces ) ) {
234
+ for ( const piece of Object . keys ( pieces ) ) {
236
235
// Check all pieces
237
236
// List of all moves for the current piece
238
- let moves = parseNotation (
237
+ const moves = parseNotation (
239
238
pieceMovesCheck [ piece ] ,
240
239
thinkData ,
241
240
size ,
@@ -245,7 +244,7 @@ function validateBoard(data, discovered, pieces, size) {
245
244
246
245
// If the piece have a move that is impossible, it means it can't be this one
247
246
let isValid = true ;
248
- for ( let move of moves ) {
247
+ for ( const move of moves ) {
249
248
if ( discovered [ move ] && data [ move ] === 0 ) {
250
249
isValid = false ;
251
250
break ;
@@ -299,8 +298,8 @@ export function generatePuzzleBoard(seed, pieces, size, count, difficulty) {
299
298
let giveup = false ;
300
299
while ( ! isSolved && ! giveup ) {
301
300
// Get a random position that is not a piece and wasn't already taken
302
- let possibilities = [ ] ;
303
- for ( let i in data ) {
301
+ const possibilities = [ ] ;
302
+ for ( const i in data ) {
304
303
if (
305
304
! discovered [ i ] &&
306
305
Number . isInteger ( data [ i ] ) &&
@@ -310,14 +309,14 @@ export function generatePuzzleBoard(seed, pieces, size, count, difficulty) {
310
309
}
311
310
}
312
311
if ( possibilities . length > 0 ) {
313
- let randPos = Math . floor ( random . next ( ) * possibilities . length ) ;
312
+ const randPos = Math . floor ( random . next ( ) * possibilities . length ) ;
314
313
discovered [ possibilities [ randPos ] ] = true ;
315
314
} else {
316
315
giveup = true ; // Algorithm failed with this generation, we give up
317
316
continue ;
318
317
}
319
318
320
- let validation = validateBoard ( data , discovered , pieces , size ) ;
319
+ const validation = validateBoard ( data , discovered , pieces , size ) ;
321
320
isSolved = validation [ "isSolved" ] ;
322
321
thinkData = validation [ "thinkData" ] ;
323
322
}
@@ -331,21 +330,21 @@ export function generatePuzzleBoard(seed, pieces, size, count, difficulty) {
331
330
}
332
331
333
332
discovered [ i ] = false ;
334
- let validation = validateBoard ( data , discovered , pieces , size ) ;
333
+ const validation = validateBoard ( data , discovered , pieces , size ) ;
335
334
if ( ! validation [ "isSolved" ] ) {
336
335
discovered [ i ] = true ;
337
336
}
338
337
}
339
338
340
- let emptyCasesAfter = discovered . filter ( ( x ) => x === false ) . length ;
339
+ const emptyCasesAfter = discovered . filter ( ( x ) => x === false ) . length ;
341
340
342
341
if ( difficulty !== - 1 && difficulty > emptyCasesAfter ) {
343
342
console . log ( `Skipping puzzle with ${ emptyCasesAfter } empty tiles` ) ;
344
343
} else {
345
344
if ( difficulty !== - 1 ) {
346
345
// Set tiles to adjust difficulty
347
346
348
- let possibleTarget = [ ] ;
347
+ const possibleTarget = [ ] ;
349
348
for ( let i = 0 ; i < data . length ; i ++ ) {
350
349
if ( ! discovered [ i ] && Number . isInteger ( data [ i ] ) ) {
351
350
possibleTarget . push ( i ) ;
@@ -372,7 +371,7 @@ export function generatePuzzleBoard(seed, pieces, size, count, difficulty) {
372
371
error = "Failed to generate puzzle" ;
373
372
} else {
374
373
knownCells = Array ( size * size ) . fill ( false ) ;
375
- for ( let i in discovered ) {
374
+ for ( const i in discovered ) {
376
375
if ( discovered [ i ] ) {
377
376
knownCells [ i ] = true ;
378
377
}
@@ -381,86 +380,3 @@ export function generatePuzzleBoard(seed, pieces, size, count, difficulty) {
381
380
382
381
return { cells : data , knownCells, error } ;
383
382
}
384
-
385
- function generateClassicBoard ( G , id ) {
386
- const random = new Random ( G . seed ) ;
387
- G . cells = fillPositions ( generateBoard ( random , id , G . pieces , G . size , G . count ) ) ;
388
- G . knownCells = Array ( G . size * G . size ) . fill ( false ) ;
389
- }
390
-
391
- function isWinCondition ( G , id ) {
392
- if ( G . cells === null ) {
393
- return false ;
394
- }
395
-
396
- for ( let i = 0 ; i < G . size * G . size ; i ++ ) {
397
- if ( ! Number . isInteger ( G . cells [ i ] ) ) {
398
- if ( G . cells [ i ] !== G . knownCells [ i ] && G . cells [ i ] !== id ) {
399
- return false ;
400
- }
401
- } else if ( G . knownCells [ i ] !== true && G . knownCells [ i ] !== false ) {
402
- return false ;
403
- }
404
- }
405
-
406
- return true ;
407
- }
408
-
409
- export const Game = ( setupData ) => ( {
410
- setup : ( ) => ( {
411
- ...setupData ,
412
- knownCells : setupData . knownCells ?? null ,
413
- cells : setupData . cells ?? null ,
414
- } ) ,
415
-
416
- moves : {
417
- discoverPiece : ( { G, events } , id ) => {
418
- if ( G . cells === null ) {
419
- generateClassicBoard ( G , id ) ;
420
- }
421
-
422
- if ( G . knownCells [ id ] !== false || G . gamemode === "p" ) {
423
- return INVALID_MOVE ;
424
- }
425
-
426
- if ( Number . isInteger ( G . cells [ id ] ) ) {
427
- G . knownCells [ id ] = true ;
428
- } else {
429
- events . endGame ( { isWin : false } ) ;
430
- }
431
- } ,
432
-
433
- placeHint : ( { G, events } , id , action ) => {
434
- if ( G . cells === null ) {
435
- generateClassicBoard ( G , id ) ;
436
- }
437
-
438
- if ( G . knownCells [ id ] === true ) {
439
- return INVALID_MOVE ;
440
- }
441
-
442
- G . knownCells [ id ] = action ;
443
-
444
- if ( isWinCondition ( G , id ) ) {
445
- events . endGame ( { isWin : true } ) ;
446
- }
447
- } ,
448
-
449
- removeHint : ( { G, events } , id ) => {
450
- if ( G . knownCells [ id ] === true ) {
451
- return INVALID_MOVE ;
452
- }
453
-
454
- G . knownCells [ id ] = false ;
455
-
456
- if ( isWinCondition ( G , id ) ) {
457
- events . endGame ( { isWin : true } ) ;
458
- }
459
- } ,
460
- } ,
461
-
462
- turn : {
463
- minMoves : 1 ,
464
- maxMoves : 1 ,
465
- } ,
466
- } ) ;
0 commit comments