Note AcademyNote Academy

Computer Science · May / June 2024 · Paper 22 · Question 11

0478

As printed in the paper

The question as text

A one-player game uses the two-dimensional (2D) array Grid[] to store the location of a secret cell to be found by the player in 10 moves. Each row and column has 5 cells. Start Grid[1,1] At the start of the game: • The program places an ‘X’ in a random cell (not in Grid[1,1]) and empties all the other cells in the grid. • The player starts at the top left of the grid. • The player has 10 moves. During the game: • The player can move left, right, up or down by one cell and the move must be within the grid. • “You Win” is displayed if the player moves to the cell with ‘X’ and has played 10 moves or less. • “You Lose” is displayed if the player has made 10 moves without finding the ‘X’. Write a program that meets these requirements. You must use pseudocode or program code and add comments to explain how your code works. You do not need to declare any arrays or variables; you may assume that this has already been done. All inputs and outputs must contain suitable messages.

Read automatically from the paper; the printed page above is exact.

Show the answer and mark scheme

Data Structures required with names as given in the scenario:

Arrays or lists Grid

Requirements (techniques)

R1 Set up game – generate random cell, clear all other cells in array, set player start position and start player moves counter (iteration, use of arrays and library routines (round and random))

R2 Input and check move – is it valid? (input, output, iteration and selection)

R3 Decide outcome – has move found the X? If so, give appropriate output. If not increment counter and continue. If 10 moves exceeded, give appropriate output (use of arrays, iteration, selection and output).

Example 15-mark answer in pseudocode

// Set up game

FOR Row ← 1 TO 5

FOR Column ← 1 TO 5

Grid[Row, Column] ← ''// set grid cells to be empty

NEXT Column

NEXT Row

REPEAT // not in cell 1,1

XRow ← ROUND ((RANDOM() * 4) + 1, 0) // Random row position between 1 and 5 in GRID XColumn ← ROUND ((RANDOM() * 4) + 1, 0) // Random column position between 1 and 5 in GRID

UNTIL XRow <> 1 and XColumn <> 1 // not in cell 1,1

Grid [XRow, XColumn] ← 'X'

MaxMove ← 10

NumberMoves ← 0

PlayerRow ← 1

PlayerColumn ← 1

Win ← FALSE

// during game

WHILE NumberMoves < MaxMove AND NOT Win

MoveError ← FALSE

OUTPUT "Please enter your move, L – Left, R – Right, U – Up or D - Down"

INPUT UPPER(PlayerMove)

REPEAT

CASE OF PlayerMove

'L' : TempColumn ← PlayerColumn – 1

'R' : TempColumn ← PlayerColumn + 1

'U' : TempRow ← PlayerRow – 1

'D' : TempRow ← PlayerRow + 1

OTHERWISE MoveError ← TRUE

ENDCASE

// check for out-of-range moves

IF TempColumn < 1 or TempColumn > 5

THEN

MoveError ← TRUE

ELSE

PlayerColumn ← TempColumn

ENDIF

IF TempRow < 1 or TempRow > 5

THEN

MoveError ← TRUE

ELSE

PlayerRow ← TempRow

ENDIF

// check win if X Found

IF Grid [PlayerRow, PlayerColumn] = 'X'

THEN

OUTPUT "You Win"

Win ← TRUE

ELSE

IF NOT MoveError

THEN

NumberMoves ← NumberMoves + 1

ENDIF

ENDIF

UNTIL NOT MoveError

ENDWHILE

IF NOT Win

THEN

OUTPUT "You Lose"

ENDIF

AO2: Apply knowledge and understanding of the principles and concepts of computer science to a given context, including the analysis and design of computational or programming problems

Make a free account to save your answers and see your weak topics and ask the AI why a wrong answer is wrong.