OCRBlocks is a game played on a 5 x 5 grid - OCR - GCSE Computer Science - Question 6 - 2021 - Paper 1
Question 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 ident... show full transcript
Worked Solution & Example Answer:OCRBlocks is a game played on a 5 x 5 grid - OCR - GCSE Computer Science - Question 6 - 2021 - Paper 1
Step 1
checkblock(2,1)
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
The returned value is "FREE" because there is no block placed by players A or B at that position.
Step 2
checkblock(3,0)
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 returned value is "B" since player B has placed a block at that coordinate.
Step 3
checkblock(2,3)
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 returned value is "FREE" as this position has not been filled by either player.
Step 4
State one feature of checkblock() that shows that it is a function and not a procedure.
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
One feature that indicates checkblock() is a function is that it returns a value to the caller. Procedures typically do not return values.
Step 5
State why this function call will produce an error.
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
The function call checkblock(-1, 6) will produce an error because the parameters -1 and 6 are outside the valid index range of the gamegrid array.
Step 6
Describe how validation could be added in to the checkblock() function to stop this error from occurring.
97%
121 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 if r (row) and c (column) are within the bounds of the array. For example:
if r < 0 or r >= 5 or c < 0 or c >= 5 then
return "INVALID"
endif
This checks if the coordinates are valid before accessing the array.
Step 7
Write an algorithm to allow player A to select a position for their next block on the game board.
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
Prompt the player for the position (r, c) of their block on the board.
Call checkblock(r, c) to check if the position is free.
If the return value is "FREE":
Update gamegrid[r][c] to "A".
If the return value is not "FREE":
Display a message indicating the position is taken.