Past-paper question · 2015 May-June · Paper 22 · Q3(e)

Question 3(e) 2015 · May-June · Paper 22 · 10 marks

Question 3

Part (e)

Question 3

A board game is designed for two players, O and X.

At the beginning, all cells of a 3 x 3 grid are empty.

The players take turns in placing their marker in an empty cell of the grid; player O always starts.

The game ends when one player completes a row, column or diagonal or the grid is full.

Here is one example after three turns:

O

O X

Ali wants to write a program to play the game.

Part (e)

The current player is a winner if they have placed their markers in each cell of a row or a

column or a diagonal. Ali’s solution checks for a winner after every turn.

Complete the pseudocode for the subroutine HasPlayerWon:

.............................................................. HasPlayerWon() ....................................................

DECLARE WinningLine : ...................................................................................................

DECLARE i : .........................................................................................................................

WinningLine ← .....................................................................................................................

// check both diagonals

IF Grid[1,1] = Grid[2,2] AND Grid[1,1] = Grid[3,3]

OR Grid[..............................................] = Grid[..............................................]

AND Grid[..............................................] = Grid[..............................................]

THEN WinningLine ← TRUE

ELSE

i ← 0

........................................................................................................................................

i ← i + 1

// check a row

IF Grid[i,1] = Grid[i,2] AND Grid[i,1] = Grid[i,3]

// check a column

OR (Grid[...........................................] = Grid[...........................................]

AND Grid[.....................................] = Grid[...........................................])

THEN WinningLine ← TRUE

ENDIF

UNTIL WinningLine = TRUE OR ......................................................................... .

.........................................................................

RETURN WinningLine

ENDFUNCTION

[10]

Question 11

Reveal official mark-scheme answer Official mark-scheme answer for 3(E)Official mark-scheme answer for 3(E)

Official answer for Question 3

Part (e)

Part (e)

FUNCTION HasPlayerWon() RETURNS BOOLEAN accept AS or : or just BOOLEAN

✓
DECLARE WinningLine : BOOLEAN
✓
DECLARE i : INTEGER
✓
WinningLine FALSE
✓
// check both diagonals
✓
IF
✓
Grid[1,1] = Grid[2,2] AND Grid[1,1] = Grid[3,3]
✓
OR Grid[1,3] = Grid[2,2]
✓
1 mark
✓
AND Grid[1,3] = Grid[3,1]
✓
Ensure the following
✓
THEN WinningLine TRUE
✓
elements have been
✓
checked:
✓
ELSE
✓
i 0
✓
Grid[1,3]
✓
Grid[3,1]
✓
REPEAT accept LOOP / DO / / DO LOOP
✓
i i + 1
✓
Grid[2,2]
✓
Grid[3,1] =
✓
// check a row
✓
IF
✓
Grid[i,1] = Grid[i,2] AND Grid[i,1] = Grid[i,3]
✓
// check a column
✓
OR Grid[1,i] = Grid[2,i]
✓
Ensure the following

Question 1

✓
mark
✓
AND Grid[1,i] = Grid[3,i]
✓
elements have been
✓
THEN WinningLine TRUE
✓
checked:
✓
Grid[1,i]
✓
ENDIF
✓
UNTIL WinningLine = TRUE OR i=3
✓
Grid[2,i]
✓
ENDIF
✓
Grid[3,i]
✓
RETURN WinningLine
✓
Grid[3,1] =
✓
ENDFUNCTION
✓
[max 10]
✓
Mark Scheme
✓
Syllabus
✓
Paper
✓
Cambridge International AS/A Level – May/June 2015
✓
9691

Question 22

Mark scheme 9691_s15_ms_22.pdf →

Topic relevance

Why this question appears here

82% assigned-topic match · Programming › Constructs

Topic-match percentages estimate how closely each question fits the selected syllabus topic. They are a guide for choosing stricter or broader practice.

Permanent source page: Educate A Change · From 9691_s15_qp_22.pdf

Keep practising this topic