forked from taniarascia/sokoban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
47 lines (42 loc) · 1.26 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { EMPTY, WALL, BLOCK, SUCCESS_BLOCK, VOID, PLAYER, levelOneMap } from './constants.js'
// Helpers
export const isBlock = (cell) => [BLOCK, SUCCESS_BLOCK].includes(cell)
export const isPlayer = (cell) => [PLAYER].includes(cell)
export const isTraversible = (cell) => [EMPTY, VOID].includes(cell)
export const isWall = (cell) => [WALL].includes(cell)
export const isVoid = (cell) => [VOID, SUCCESS_BLOCK].includes(cell)
export const getX = (x, direction, spaces = 1) => {
if (direction === 'up' || direction === 'down') {
return x
}
if (direction === 'right') {
return x + spaces
}
if (direction === 'left') {
return x - spaces
}
}
export const getY = (y, direction, spaces = 1) => {
if (direction === 'left' || direction === 'right') {
return y
}
if (direction === 'down') {
return y + spaces
}
if (direction === 'up') {
return y - spaces
}
}
export function generateGameBoard({ level }) {
if (level === 1) {
return JSON.parse(JSON.stringify(levelOneMap)) // clone deep hack
}
}
export function countBlocks(blockCount, y, x, direction, board) {
if (isBlock(board[y][x])) {
blockCount++
return countBlocks(blockCount, getY(y, direction), getX(x, direction), direction, board)
} else {
return blockCount
}
}