ExamBank Scot · AH Computing Science question bank · All AH Computing Science questions

AH Computing Science 2022 Question 5

14 marks · Requirements

When the game is implemented, a 2‑D array of string values is used to store the positions of the player and the robots. State two additional functional requirements for this game. Using a programming language of your choice, write code to declare a 2‑D array to represent the game board. Using a programming language of your choice, write code to assign the value ‘Robot’ to six random, empty locations on the board. The grid below represents the contents of the 2‑D array at the start of a game. Robot Robot Robot Robot Player Robot Robot Movement of the player is controlled using several procedures. Partially completed code for the moveUp procedure is shown below. PROCEDURE moveUp() <find player row and column> IF <the player is not already on the top row> THEN <move the player up one row> END IF END PROCEDURE Using a programming language of your choice, write code to implement this procedure. Whenever a player moves up one row, all the robots will move closer to the player. The grid below shows the new position of the player after one move, and the existing position of the robots. Each robot will move to the cell indicated by the arrowhead. Robot Robot Robot Player Robot Robot Robot The movement of the two robots in the shaded cells will result in a collision. These robots will be destroyed and replaced with a pile of rubble. The grid below shows the state of the game after all of the robot moves have been completed. Player Robot Rubble Robot Robot Robot The procedure robotMove(robotX,robotY,playerX,playerY) is used to control the movement of a robot. The parameters identify the current position of the robot and the new position of the player. This procedure: • clears the current position of the robot • assigns the robot to a new position which is closer to player • checks for a collision between robots which destroys both robots and creates a pile of rubble in their place • checks for a collision with an existing pile of rubble which destroys the robot • checks for the robot capturing the player. Using pseudocode, design the procedure robotMove. A new computer game is being developed. During the game, a player must avoid being captured by a group of robots. The game will be played on a 10 by 10 grid. The player and robots can move horizontally, vertically or diagonally to an adjacent position on the grid. After each player move, the robots will move closer to the player. A player wins the game when all the robots are destroyed. Robots will be destroyed when two or more robots collide, leaving a pile of rubble in their place. Any robot that collides with a pile of rubble will also be destroyed. When any robot moves to the same position as the player, the player is captured, and the game is over.

Marking scheme

Player and robot movements must not go beyond the dimensions of the game board. The game much check for robot collisions and for capturing the player. The game must check for player win and game over. 1 mark each for any two additional functional requirements. Other answers possible. Note: functional requirements must relate to data structure and/or logic of the solution For example: DECLARE board AS ARRAY OF ARRAY OF STRING INITIALLY [[""]] <with 10 rows and 10 columns> 1 mark for correct dimensions and string type Accept valid declaration of 2D array with correct dimensions in any programming language REPEAT 6 TIMES DECLARE empty INITIALLY false REPEAT DECLATE randomX INITIALLY RANDOM(10)-1 DECLARE randomY INITIALLY RANDOM(10)-1 IF board[randomX, randomY] = "" THEN SET board[randomX, randomY] TO "Robot" SET empty TO true END IF UNTIL empty = true END REPEAT 1 mark for fixed loop to generate 6 robot positions 1 mark for checking randomly selected position is empty 1 mark for assignment to random position within the grid Note: candidates should not be penalised if they don’t subtract 1 to generate 0-9 grid if their subsequent answers indicate use of a 1-10 grid. PROCEDURE moveUp() FOR row = 0 TO 9 DO FOR column = 0 TO 9 DO If board[row, column] = "Player" AND row ≠ 0 THEN SET board[row - 1, column] TO "Player" SET board[row, column] TO "" END IF END FOR END FOR END PROCEDURE Alternative Soln (validator) PROCEDURE moveUp() FOR row = 1 TO 9 DO FOR column = 1 TO 9 DO If board[row, column]) = "Player" THEN SET board[row - 1, column] TO "Player" SET board[row, column] TO "" END IF END FOR END FOR END PROCEDURE 1 mark for getting location of player using nested loop 1 mark for checking that player is not already on the top row 1 mark for setting new location by changing row position by -1 and removing from previous position Note: if candidate is using a different grid layout, set board[row + 1, column] = "Player" would be appropriate 1. set board(robotX, robotY) to "" 2. if playerX < robotX then subtract 1 from robotRow 3. if playerX > robotX then add 1 to robotX 4. if playerY > robotY then add 1 to robotY 5. if playerY < robotY then subtract 1 from robotY 6. if board(robotX, robotY) = "" then 7. set board(robotX, robotY) to "Robot" 8. else if board(robotX, robotY) = "Robot" then 9. set board(robotX, robotY) to “Rubble" 10. else if board(robotX, robotY) = "Rubble" then 11. set board(robotX, robotY) to "Rubble" 12. else if board(robotRow, robotY) = “Player” then 13. display game over message 14. end if 1 mark for clearing current position of the robot 1 mark for calculating new robot position closer to the player 1 mark for assignment of robot to the new position 1 mark for checking for collision with another robot and creation of pile of rubble 1 mark for checking for player capture Note: steps 10 & 11 not required in solution

Previous question: AH Computing Science 2022 Question 4

Next question: AH Computing Science 2022 Question 6