Workshop › Pac-Man-style Maze Chase › Step 7 of 42
Before anything moves, it must know where the walls are. isWall reads the map (the ghost house counts as a wall),
and canGo asks whether the tile one step in a direction is free. A direction is a pair [dx, dy]. Columns are
wrapped, so stepping off the left end of the tunnel lands on the right end.
```js
const wrap = (col) => (col + COLS) % COLS
function isWall(col, row) {
const ch = MAZE[row][wrap(col)]
return ch === '#' || ch === '-'
}
function canGo(e, dir) {
return !isWall(e.col + dir[0], e.row + dir[1])
}
```% is the remainder: (-1 + 19) % 19 is 18 and (19 + 19) % 19 is 0, so any column one step outside comes back
in on the other side.isWall reads the character of the tile; || means "or".canGo adds the direction to the position and asks isWall; ! turns the answer around.Under key, write wrap, isWall and canGo.
Skills: Collision detection
← The player What a mover remembers →