AQA GCSE Computer Science June 2024 Paper 1, Question 12: the sliding puzzle programs

AQA 8525 June 2024 Paper 1, Questions 12.6 and 12.7: use given subroutines to check whether the first row of a sliding puzzle is in sequence, and to let the user move tiles until the puzzle is solved. Model answers, the marks explained, and a puzzle to play.

Past paper questionAQA 8525/1BJune 2024 Paper 110 marksWrite a program

Question 12 of the AQA GCSE Computer Science Paper 1 sat on 15 May 2024 (8525/1B, the Python paper) is the long question, 17 marks over seven parts, about a 3 by 3 sliding puzzle. This page works through the two programs you have to write: 12.6 (4 marks) and 12.7 (6 marks). Both are about using subroutines that someone else has written.

We do not copy the exam paper here. Open it beside this page: AQA June 2024 Paper 1B question paper (PDF). When you have finished, check the mark scheme too.

The subroutines you are given

  • getTile(row, column) returns the number of the tile at that position. The blank space is 0.
  • move(row, column) moves that tile into the blank space, if the blank is next to it.
  • checkSpace(row, column) returns True if the blank is next to that tile.
  • solved() returns True if the puzzle is solved.

You do not write these. You call them. Rows and columns are numbered from 0, and the row comes first.

Part 12.6: is the first row in sequence?

Display Yes if the second tile in the first row is one more than the first, and the third is one more than the second. Otherwise display No.

if getTile(0, 1) == getTile(0, 0) + 1 and getTile(0, 2) == getTile(0, 1) + 1:
    print("Yes")
else:
    print("No")

The four marks: selection with more than one condition; checking three tiles that are next to each other; the right index numbers for the first row, (0, 0), (0, 1) and (0, 2); and output that is Yes or No in every case, never both.

If you nest two ifs, every path needs an output. The mark scheme points at the outer else, which is the one people forget.

Part 12.7: play until solved

Get a row and a column. If that tile is next to the blank, move it. If not, output Invalid move. Repeat until the puzzle is solved.

while not solved():
    row = int(input("Row: "))
    column = int(input("Column: "))
    if checkSpace(row, column):
        move(row, column)
    else:
        print("Invalid move")

The six marks: an indefinite loop; selection to check for the blank space; two inputs stored in two variables; solved and checkSpace used in the right places; move called with the user's row and column on the True path; and Invalid move output, with the inputs repeated in the right place.

Where the marks are lost

  • Row 1 for the first row. The first row is row 0.
  • getTile(0, 0) + 1 == getTile(0, 1) == getTile(0, 2) - 1 and other clever chains. Write two plain comparisons joined with and.
  • solved without brackets. while not solved: tests the subroutine itself, which always counts as True. A call needs ().
  • Writing your own move. The question says you must use the subroutines given.
  • Inputs before the loop. The user has to be asked again on every turn.
  • Asking for another move after the puzzle is solved. The mark scheme rejects this. With the inputs at the top of a while not solved() loop it cannot happen.

Parts 12.2 to 12.5 in brief

The nested for loops with getTile(i, j) == 0 look at all nine positions (nine comparisons, whatever the board). The outer loop goes through the rows, the inner loop goes through the columns, and the purpose of the program is to find the position of the blank space.

Run it

A working puzzle. The four subroutines are written for you at the top, as they would be in the exam, and your two answers are at the bottom. The board starts two moves from solved.

It prints Yes for the first row. Then enter 2 and 1, then 2 and 2, to solve it. Try 0 and 0 first to see Invalid move.
The program
from bugbot import *
connect()

board = [[1, 2, 3], [4, 5, 6], [0, 7, 8]]

def getTile(row, column):
    return board[row][column]

def blank():
    for r in range(3):
        for c in range(3):
            if board[r][c] == 0:
                return r, c

def checkSpace(row, column):
    r, c = blank()
    return abs(r - row) + abs(c - column) == 1

def move(row, column):
    if checkSpace(row, column):
        r, c = blank()
        board[r][c] = board[row][column]
        board[row][column] = 0

def solved():
    return board == [[1, 2, 3], [4, 5, 6], [7, 8, 0]]

def displayBoard():
    for line in board:
        print(line)

# part 12.6
if getTile(0, 1) == getTile(0, 0) + 1 and getTile(0, 2) == getTile(0, 1) + 1:
    print("Yes")
else:
    print("No")

# part 12.7
displayBoard()
while not solved():
    row = int(input("Row: "))
    column = int(input("Column: "))
    if checkSpace(row, column):
        move(row, column)
        displayBoard()
    else:
        led("red")
        print("Invalid move")
led("green")
print("Solved")
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

What is the answer to AQA GCSE Computer Science 2024 Paper 1 Question 12.6?

if getTile(0, 1) == getTile(0, 0) + 1 and getTile(0, 2) == getTile(0, 1) + 1 then print Yes, else print No.

What is the answer to Question 12.7?

while not solved(): input the row and the column as integers. If checkSpace(row, column) is True, call move(row, column). Otherwise print Invalid move.

How do I use a subroutine that I have not written?

Read its description for three things: its name, the parameters it needs and their order, and what it returns. Then call it with values in that order, and use what it returns in a condition, a calculation or an assignment.

More from this paper

Every AQA 8525 question we have worked

Learn it step by step

  1. F4.2 Parameters and return values Functions and structured code
  2. F2.6 Condition-controlled loops: while Decisions and loops
  3. F13.4 Programming questions Exam preparation
Open the lessons

This is our own explanation of a published exam question. It is not written or endorsed by AQA, and the question paper and mark scheme remain AQA's copyright. Read them on AQA's site with the links on this page.