AQA · GCSE · Computer Science · Higher
Updating a sliding-puzzle board
The sliding puzzle uses a 3 x 3 board containing eight numbered tiles and one blank space. A tile can move one position up, down, left or right, and can only move into the blank space when it is next to that space. The blank space is represented in the program as number 0.
| Subroutine | Purpose |
|---|---|
| getTile(row, column) | Returns the number of the tile on the board in position (row, column). |
| move(row, column) | Moves the tile in position (row, column) to the blank space if the blank space is next to that tile. If it is not next to the blank space, no move is made. |
| displayBoard() | Displays the board showing the current position of each tile. |
| if (getTile(1, 0) == 0) |
| { |
| move(2, 0); |
| } |
| if (getTile(2, 0) == 0) |
| { |
| move(2, 1); |
| } |
| displayBoard(); |
| row \ column | 0 | 1 | 2 |
|---|---|---|---|
| 0 | 1 | 8 | 3 |
| 1 | 0 (blank) | 7 | 5 |
| 2 | 4 | 2 | 6 |
Complete the board to show the new positions of the tiles after the program in Figure 14 is run on the board in Figure 15.
Practice unavailable
This question can be viewed, but its original response format is not available for practice yet.
Write your answer first. You can study the marking guidance whenever you need it.
Study the marking See what earns credit and compare it with a full-mark answer.
Marking points
- 1 Moves tile 4 into row 1, column 0.
- 2 Then moves tile 2 into row 2, column 0.
Why this answer loses marks
I applied only the final move to the original board.
The later move starts from the board produced by the earlier move, so skipping that state changes the new positions.