The game starts one move from the ordered goal: tile 15 can slide into the blank to win. New Puzzle performs a bounded legal walk from the goal, and Verify construction replays its inverse on a copy. It proves a path back without claiming shortest distance or difficulty. Shuffle actions never count as player moves.

Use one array without forgetting the rows
Carnegie Mellon's archived 15 Puzzle lab describes a four-by-four grid, fifteen numbers and one blank, adjacent horizontal/vertical swaps, and checking for the ordered goal. In this JavaScript example, the first board is one legal move from that goal. CMU: 15 Puzzle Lab
Store sixteen values: each integer 1–15 exactly once, plus 0 for the blank. The goal is row-major [1, 2, 3, 4] / [5, 6, 7, 8] / [9, 10, 11, 12] / [13, 14, 15, 0]. Coordinates are zero-based.
Validate length, integer range and uniqueness before accepting a board. Derive the blank coordinate from that array, or update any cached coordinate in the same transaction. A stale blank cache cannot be another source of truth.
The starting board keeps the first three goal rows and changes the last row to [13, 14, 0, 15]. It is one legal move from the goal. Reset returns to this starting arrangement even after you have generated a different puzzle.
Test geometric adjacency, not index difference
A numbered tile can swap with the blank only when the Manhattan distance between their row/column coordinates is exactly one: absolute row difference plus absolute column difference equals one.
| Proposed action | Result |
|---|---|
| Click tile 15 at (3, 3), blank (3, 2) | Legal swap; goal reached; player move count becomes 1 |
| Click tile 1 at (0, 0), blank (3, 2) | Reject; board and move count unchanged |
| Activate the blank itself | No swap or move increment |
| Treat index 11 and index 12 as neighbors | Reject: (2, 3) and (3, 0) are not adjacent |
Each successful player swap increments the count once and then checks every goal cell, including the blank. A rejected action cannot add a move. Reaching the goal freezes ordinary movement until Reset or New Puzzle.
Build reachability into the shuffle
For New Puzzle, begin at the goal and perform one hundred legal blank-neighbor swaps, recording each action. Every step is reversible. Applying those inverse swaps in reverse order gives a path from the generated board back to the goal.
That reasoning establishes reachability by construction. It does not establish a shortest solution, a uniform distribution over solvable positions, or a particular difficulty. One hundred shuffle steps can undo earlier steps and leave a board close to the goal.
Do not accept an arbitrary random permutation merely because it contains the right sixteen values. Structural validity and a reachable configuration are different requirements. This bounded design does not offer imported boards or claim a general solvability checker.
A legal shuffle can return to the goal. New Puzzle uses a known reachable fallback when necessary, so the new board is ready to play. The fallback below shows a path back to the ordered goal.
Make the fallback inspectable
From the goal, move the blank Left, then Up. The resulting board is [1, 2, 3, 4] / [5, 6, 7, 8] / [9, 10, 0, 12] / [13, 14, 11, 15]. The blank is at (2, 2).
Reverse it with Down, then Right: first swap 11 back into (2, 2), then swap 15 into (3, 2). The goal returns after two legal moves. The fallback is nonsolved and demonstrably reachable, even though it is intentionally easy.
Keep construction swaps separate from player moves. A new board begins with player count zero, whether it came from a hundred-step walk or the fallback. The construction message reports whether the new board used the hundred-step walk or the two-step fallback.
The reverse record is a verification path, not a hint proven shortest. It may contain moves that undo one another. Use Verify construction to replay the recorded inverse actions on a copy; the player’s board and move count stay unchanged.
Play the small baseline first
Activate tile 15 in the initial board and inspect the one-move result. Nonadjacent tile buttons report why they cannot move. Direction buttons move the blank toward the named neighbor; a focused board also accepts fresh arrow keys without wrapping a row. Reset always returns to the original one-move board, even after New Puzzle. The complete ordered array defines a win. All fifteen numbers and the final blank must occupy their goal cells.
Copy this prompt
Create a four-by-four sliding-number puzzle in one HTML file. Display fifteen numbered tile buttons and one noninteractive Blank cell. The goal is the numbers 1 through 15 in row order followed by Blank. Start with the first three goal rows unchanged and the last row 13, 14, Blank, 15. Clicking 15 should solve the board in one player move.
A tile may move only when its row-and-column distance from Blank is exactly one. Reject nonadjacent tiles and row wrapping without adding a move. After each legal swap, update the move count and check the complete goal, including Blank. A win stops ordinary moves until Reset or New Puzzle.
Provide Up, Down, Left and Right controls that move Blank toward the named neighboring cell. Fresh arrow presses work when the board has focus. Give each tile an accessible name containing its number and current zero-based coordinates.
New Puzzle starts at the goal and records legal neighbor swaps. Use a known reachable fallback when necessary: move Blank Left, then Up from the goal. This produces the last two rows 9, 10, Blank, 12 and 13, 14, 11, 15.
Verify construction reverses the recorded swaps on a copy of the generated starting board. Report whether it reaches the exact goal without altering the current board or player count. Explain that this proves reachability, not a shortest solution or difficulty rating. Reset restores the original one-move board. Style the grid with generous gaps, readable tile numbers and a layout that fits a phone screen.
Solve once, then inspect a new board
Click 15 to finish the starting puzzle, then generate a new board and use Verify construction. The reverse path belongs to the board’s construction; your player count still begins at zero. NOS can render the HTML puzzle for that comparison. Try a distant tile next and explain why its click changes neither the board nor the move count.