Workshop › 15 Puzzle › Step 1 of 27
Slide the numbered tiles back into order. Positions in a flat array, neighbours on a grid, why a shuffle must be made of real moves (and the math that proves which puzzles can be solved), and smooth sliding animation.
The 15 puzzle: fifteen numbered tiles and one gap on a 4 by 4 board; slide tiles into the gap until they are in order.
The board is kept in one flat array of 16 numbers, read row by row, with 0 for the gap.
```js
let tiles // tiles[position] is the number on that square, 0 for the gap; positions go row by row
function reset() {
tiles = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]
}
reset()
```let tiles makes a variable; the comment says what it holds.[1, 2, ..., 15, 0] is an array: an ordered list. tiles[0] is the first item (1), tiles[15] the last (0, the gap).reset() puts the board in order. Later it will also shuffle it. The last line calls it.Write the lines under the three comment lines, then press Run.
Skills: Arrays and objects