Workshop › Sokoban › Step 16 of 29
The whole game is one rule: you can push a box, but only one, and never into a wall. If the target tile has a box, we look one tile further: if it is free, the box moves there and the player follows.
```js
const box = boxAt(x, y)
if (box) {
const bx = x + dx
const by = y + dy
if (walls.has(key(bx, by)) || boxAt(bx, by)) return // a box cannot push into a wall or another box
box.x = bx
box.y = by
}
```box is the box on the target tile, or undefined; if (box) enters only when there is one.bx, by is the tile beyond it, one more step in the same direction.box is the object inside boxes, so the array changes too. Then the player steps in.In move, replace the line if (boxAt(x, y)) return // pushing comes in the next step with the push block.
Skills: Game states, Collision detection
← Boxes are in the way A box on a goal turns green →