Workshop › 15 Puzzle › Step 12 of 27
Only a tile next to the gap can move. In the flat list the neighbours of i are i - N (above), i + N (below),
i - 1 and i + 1, but only when they are on the board: position 3 ends a row, and 4 starts the next one.
```js
// The squares next to position i (up, down, left, right), staying inside the board.
function neighbors(i) {
const list = []
if (rowOf(i) > 0) list.push(i - N)
if (rowOf(i) < N - 1) list.push(i + N)
if (colOf(i) > 0) list.push(i - 1)
if (colOf(i) < N - 1) list.push(i + 1)
return list
}
```list starts empty; push adds a neighbour at the end.return list gives the neighbours back.Under solvedTiles, leave an empty line and write the comment and neighbors. Press Run.
Skills: Arrays and objects
← The solved board from N Slide a tile →