6. OCRBlocks is a game played on a 5 x 5 grid - OCR - GCSE Computer Science - Question 6 - 2021 - Paper 1
Question 6
6. OCRBlocks is a game played on a 5 x 5 grid. Players take it in turns to place blocks on the board. The board is stored as a two-dimensional (2D) array with the id... show full transcript
Worked Solution & Example Answer:6. OCRBlocks is a game played on a 5 x 5 grid - OCR - GCSE Computer Science - Question 6 - 2021 - Paper 1
Step 1
Give the returned value when the following statements are called.
96%
114 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Function call
Returned value
checkblock(2,1)
FREE
checkblock(3,0)
FREE
checkblock(2,3)
FREE
Step 2
State one feature of checkblock() that shows that it is a function and not a procedure.
99%
104 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
The checkblock() returns a value (the outcome) which indicates that it is a function. Procedures typically do not return values.
Step 3
State why this function call will produce an error.
96%
101 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
The function call checkblock(-1,6) produces an error because the indices are out of the valid range for the 5 x 5 grid, which only allows values between 0 and 4.
Step 4
Describe how validation could be added in to the checkblock() function to stop this error from occurring.
98%
120 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Validation can be added by checking the values of r and c before accessing gamegrid. For instance:
if r < 0 or r >= 5 or c < 0 or c >= 5 then
return "ERROR: Invalid position"
endif
This ensures that only valid indices are used.
Step 5
Write an algorithm to allow player A to select a position for their next block on the game board.
97%
117 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Algorithm allowPlayerAToSelectPosition
repeat
ask player for position (r, c) of their block on the board
if checkblock(r, c) == "FREE" then
gamegrid[r,c] = "A"
else
print "Position is not free. Please choose another position."
endif
until checkblock(r, c) == "FREE"
endAlgorithm