There are multiple ways to solve this task. Your pupils' first attempts may have been to move in another random direction (Solution 1) or turn around (Solution 2) when encountering an obstacle. However, these solutions are incomplete as there's no guarantee that another obstacle won't be in the next location. A complete solution would ensure that their next location doesn't contain an obstacle (Solution 3).
Starter code
import randomdefnext_turn(world_state,avatar_state): # Choose a random direction to move in number = random.randint(1,4)if number ==1: new_dir = direction.NORTHelif number ==2: new_dir = direction.EASTelif number ==3: new_dir = direction.SOUTHelse: new_dir = direction.WEST# Get details about the location you would move onto next_location = avatar_state.location + new_dirif world_state.can_move_to(next_location):# This location is free action =MoveAction(new_dir)else:print("I can't move that way!") action =WaitAction()return action
Solution 1: Another random direction
In this solution, we try another random direction but don't actually check if we can move there.
import randomdefnext_turn(world_state,avatar_state): # Choose a random direction to move in number = random.randint(1,4)if number ==1: new_dir = direction.NORTHelif number ==2: new_dir = direction.EASTelif number ==3: new_dir = direction.SOUTHelse: new_dir = direction.WEST# Get details about the location you would move onto next_location = avatar_state.location + new_dirif world_state.can_move_to(next_location):# This location is free action =MoveAction(new_dir)else:print("I can't move that way!")# Choose another random direction to move in number = random.randint(1,4)if number ==1: new_dir = direction.NORTHelif number ==2: new_dir = direction.EASTelif number ==3: new_dir = direction.SOUTHelse: new_dir = direction.WEST action =MoveAction(new_dir)return action
Solution 2: Turn around
In this solution, we turn around (180 degrees) but don't actually check if we can move there.
import randomdefnext_turn(world_state,avatar_state):# Choose a random direction to move in number = random.randint(1,4)if number ==1: new_dir = direction.NORTHelif number ==2: new_dir = direction.EASTelif number ==3: new_dir = direction.SOUTHelse: new_dir = direction.WEST# Get details about the location you would move onto next_location = avatar_state.location + new_dir if world_state.can_move_to(next_location):# This location is free action =MoveAction(new_dir)else:print("I can't move that way! Turning around...")# Turn aroundif new_dir == direction.NORTH: new_dir = direction.SOUTHelif new_dir == direction.EAST: new_dir = direction.WESTelif new_dir == direction.SOUTH: new_dir = direction.NORTHelse: new_dir = direction.EAST action =MoveAction(new_dir)return action
Solution 3: Complete solution
In this solution, we keep trying a new random direction until we can move to our next location. This solution is more advanced and requires the use of a while loop.
defnext_turn(world_state,avatar_state):# Choose a random direction to move in number = random.randint(1,4)if number ==1: new_dir = direction.NORTHelif number ==2: new_dir = direction.EASTelif number ==3: new_dir = direction.SOUTHelse: new_dir = direction.WEST# While you cannot move to the next location, try another random directionwhilenot world_state.can_move_to(avatar_state.location + new_dir):# Choose another random direction to move in number = random.randint(1,4)if number ==1: new_dir = direction.NORTHelif number ==2: new_dir = direction.EASTelif number ==3: new_dir = direction.SOUTHelse: new_dir = direction.WESTreturnMoveAction(new_dir)
Solution Optimisation: Subroutines
You may have noticed that we repeat code when selecting another random direction to move in. To avoid this repetition, a subroutine named get_random_dir() may be created.
defget_random_dir():# Choose a random direction to move in number = random.randint(1,4)if number ==1:return direction.NORTHelif number ==2:return direction.EASTelif number ==3:return direction.SOUTHelse:return direction.WESTdefnext_turn(world_state,avatar_state): new_dir =get_random_dir()whilenot world_state.can_move_to(avatar_state.location + new_dir): new_dir =get_random_dir()returnMoveAction(new_dir)
Extra Challenge
Up until this point, we had equal probability of randomly selecting one of the 4 random directions. Challenge your pupils to increase the probability of randomly selecting a specific direction.
# Choose a random direction to move in.# This time we choose a random number between 1 and 10.number = random.randint(1,10)if number ==1:# 1 = NORTH (1/10) new_dir = direction.NORTHelif number ==2:# 2 = EAST (1/10) new_dir = direction.EASTelif number <7:# 3, 4, 5, 6 = SOUTH (4/10) new_dir = direction.SOUTHelse:# 7, 8, 9, 10 = WEST (4/10) new_dir = direction.WEST
Task 2: Pick up an artefact
Starter code
defnext_turn(world_state,avatar_state):# Check to see if there is an artefact on the current cell# Your code goes here# Pick it up if there isif ???:# Complete this# Your code goes hereelse:# Otherwise, move in a random direction number = random.randint(1,4)if number ==1: new_dir = direction.NORTHelif number ==2: new_dir = direction.EASTelif number ==3: new_dir = direction.SOUTHelse: new_dir = direction.WEST action =MoveAction(new_dir)return action
Solution
import randomdefnext_turn(world_state,avatar_state):# Check to see if there is an artefact on the current cell current_cell = world_state.get_cell(avatar_state.location)if current_cell.has_artefact():print("Found an artefact!") action =PickupAction()else:# Choose a random direction to move in number = random.randint(1,4)if number ==1: new_dir = direction.NORTHelif number ==2: new_dir = direction.EASTelif number ==3: new_dir = direction.SOUTHelse: new_dir = direction.WEST action =MoveAction(new_dir)return action