Student challenge 3
Intermediate Python
Task 1: Direction control
Students will need to input this starter code:
def next_turn(world_state, avatar_state):
# scan_nearby() returns a list of the nearest artefacts
nearby = world_state.scan_nearby(avatar_state.location)
# Head towards the nearest artefact
nearest = nearby[0]
action = MoveTowardsAction(nearest)
return action
Solution
def next_turn(world_state, avatar_state):
# scan_nearby() returns a list of the nearest artefacts
nearby = world_state.scan_nearby(avatar_state.location)
nearest = nearby[0]
current_location = world_state.get_cell(avatar_state.location)
if current_location.has_artefact():
# Pick up the artefact
action = PickupAction()
else:
# Move towards the nearest artefact
action = MoveTowardsAction(nearest)
return action
Task 2: Pick up five artefacts
Starter code
def next_turn(world_state, avatar_state):
my_backpack = avatar_state.backpack
print("I have", len(my_backpack), "things in my backpack")
# scan_nearby() returns a list of the nearest artefacts
nearby = world_state.scan_nearby(avatar_state.location)
nearest = nearby[0]
current_location = world_state.get_cell(avatar_state.location)
if current_location.has_artefact():
# Pick up the artefact
action = PickupAction()
else:
# Move towards the nearest artefact
action = MoveTowardsAction(nearest)
return action
Solution
def next_turn(world_state, avatar_state):
my_backpack = avatar_state.backpack
artefacts = len(my_backpack)
if artefacts == 5:
print("I have", artefacts, "things in my backpack")
print("I win!")
action = WaitAction()
else:
# scan_nearby() returns a list of the nearest artefacts
nearby = world_state.scan_nearby(avatar_state.location)
nearest = nearby[0]
current_location = world_state.get_cell(avatar_state.location)
if current_location.has_artefact():
# Pick up the artefact
action = PickupAction()
else:
# Move towards the nearest artefact
action = MoveTowardsAction(nearest)
return action
Task 3: Types of artefact
def next_turn(world_state, avatar_state):
# Get my backpack and check how many artefacts are in it
my_backpack = avatar_state.backpack
# my_backpack is a list so len() tells you how many artefacts you are holding
artefacts = len(my_backpack)
if artefacts == 5:
print("I have", artefacts, "things in my backpack")
print("I win!")
# Output the type of each artefact held
count = 0
while count < artefacts:
artefact = my_backpack[count]
print("Artefact", count + 1, "is a", artefact.type)
count = count + 1
# You can't hold any more artefacts so just return a WaitAction
action = WaitAction()
else:
# scan_nearby() returns a list of the nearest artefacts
nearby = world_state.scan_nearby(avatar_state.location)
nearest = nearby[0]
current_location = world_state.get_cell(avatar_state.location)
if current_location.has_artefact():
# Pick up the artefact
action = PickupAction()
else:
# Move towards the nearest artefact
action = MoveTowardsAction(nearest)
return action
Summary of artefacts
def next_turn(world_state, avatar_state):
# Get my backpack and check how many artefacts are in it
my_backpack = avatar_state.backpack
# my_backpack is a list so len() tells you how many artefacts you are holding
artefacts = len(my_backpack)
if artefacts == 5:
print("I have", artefacts, "things in my backpack")
print("I win!")
# Count the number of keys and chests in the backpack
count = 0
key_count = 0
chest_count = 0
while count < 5:
artefact = my_backpack[count]
if artefact.type == world_state.artefact_types.KEY:
key_count = key_count + 1
elif artefact.type == world_state.artefact_types.CHEST:
chest_count = chest_count + 1
count = count + 1
print("I have", key_count, "keys and", chest_count, "chests")
# You can't hold any more artefacts so just return a WaitAction
action = WaitAction()
else:
# scan_nearby() returns a list of the nearest artefacts
nearby = world_state.scan_nearby(avatar_state.location)
nearest = nearby[0]
current_location = world_state.get_cell(avatar_state.location)
if current_location.has_artefact():
# Pick up the artefact
action = PickupAction()
else:
# Move towards the nearest artefact
action = MoveTowardsAction(nearest)
return action
Last updated