To further investigate the use of selection, random numbers and subroutines
To use computation to solve problems with code
To find out how to pick up artefacts in Kurono and check inventory status
Preparing for the Lesson
Timing and Prior Experience
Teacher Preparation
Read over the pupil worksheet.
Delivering the lesson
This worksheet picks up from the end of worksheet 1 and pupils should have working code that outputs a message about the next cell. If necessary, explain the starter code on the worksheet.
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 cell you would move onto next_cell = avatar_state.location + new_dirif world_state.can_move_to(next_location):# This cell is free action =MoveAction(new_dir)else:print("I can't move that way!") action =WaitAction()return action
Task 1 - Avoiding obstacles
The first challenge asks pupils to introduce a subroutine to help them choose another direction if they can't move in the direction chosen. You may need to recap on subroutines.
Here is a solution:
Choose another random direction
import randomdefnext_turn(world_state,avatar_state): new_dir =get_random_dir()# Get details about the cell you would move onto next_location = avatar_state.location + new_dir if world_state.can_move_to(next_location): action =MoveAction(new_dir)else:print("I can't move that way! Choosing another direction...") action =MoveAction(get_random_dir())return actiondefget_random_dir():# 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.WESTreturn new_dir
Extension task - favour a particular direction
This solution favours moving South or West by increasing the number of random numbers generated and allocating more of them to a South or West move. Your pupils might do this in a different way.
import randomdefnext_turn(world_state,avatar_state): new_dir =get_random_dir()# Get details about the cell you would move onto next_location = avatar_state.location + new_dir if world_state.can_move_to(next_location): action =MoveAction(new_dir)else:print("I can't move that way! Choosing another direction...") action =MoveAction(get_random_dir())return actiondefget_random_dir():# Choose a random direction to move in number = random.randint(1,10)if number ==1: new_dir = direction.NORTHelif number ==2: new_dir = direction.EASTelif number <7: new_dir = direction.SOUTHelse: new_dir = direction.WESTreturn new_dir
Task 2 - Pick up an artefact
Part of the story behind Kurono is that pupils travel through time to collect artefacts for a museum. This task allows pupils to start interacting with artefacts on the map. In later worksheets, pupils will find that there are different types of artefact but, for now, they're all the same.
Pupils can only pick up an artefact if their avatar is on the same cell on the map. They can use the location information to check if there is an artefact there. They might be able to see one, but the program needs to use a selection (if) statement to work it out!
They can then pick up any artefact they find by setting their turn action to a PickupAction instead of a MoveAction or WaitAction.
action =PickupAction()return action
You might need to remind pupils that a subroutine can only return one thing and should only ever have one return point. In terms of Kurono, what they return is their move and they can only make one move.
Pupils should output a message if they pick up an artefact.
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
Summary
These tasks have allowed pupils to further explore the use of selection and subroutines, as well as interacting with the Kurono API.
In the next worksheet, pupils will be able to scan areas of the map to look for artefacts and start to discover different types of artefact.
Timing
Pupils’ prior experience
One lesson of approximately 50 mins
Pupils should be familiar with Python programming, including the use of: