Student challenge 4

Note: If students find that they get the console message that 'there aren't any nearby artefacts', they will need to move their avatar closer to an object by using previous learning from worksheet 1 before continuing with the tasks.

Task 1: Examine your backpack

def next_turn(world_state, avatar_state):
    # Find out where your avatar is
    avatar_location = avatar_state.location
    current_cell = world_state.get_cell(avatar_location)

    # Check if the current cell holds an artefact
    if current_cell.has_artefact():
        # It does! Pick it up.
        action = PickupAction()
    else:
        # The cell does not contain an artefact
        # Find the location of all artefacts nearby
        nearby = world_state.scan_nearby(avatar_location)
        
        # Get the nearest one
        nearest = nearby[0]
        
        # Move towards the nearest arteface
        action = MoveTowardsAction(nearest)
        
    my_backpack = avatar_state.backpack
    print("I have", len(my_backpack), "things in my backpack.")
    print("The first thing in my backpack is a", my_backpack[0].type)
    return action

Task 2: Examine all artefacts

def next_turn(world_state, avatar_state):
    # Find out where your avatar is
    avatar_location = avatar_state.location
    current_cell = world_state.get_cell(avatar_location)

    # Check if the current cell holds an artefact
    if current_cell.has_artefact():
        # It does! Pick it up.
        action = PickupAction()
    else:
        # The cell does not contain an artefact
        # Find the location of all artefacts nearby
        nearby = world_state.scan_nearby(avatar_location)
        
        # Get the nearest one
        nearest = nearby[0]
        
        # Move towards the nearest arteface
        action = MoveTowardsAction(nearest)
        
    my_backpack = avatar_state.backpack
    length = len(my_backpack)
    print("I have", length, "things in my backpack.")
    index = 0
    while index < length:
        print(f"The artefact at position {index} is a {my_backpack[index].type}")
        index = index + 1
    return action

Task 3: Count your loot

def next_turn(world_state, avatar_state):
    # Find out where your avatar is
    avatar_location = avatar_state.location
    current_cell = world_state.get_cell(avatar_location)

    # Check if the current cell holds an artefact
    if current_cell.has_artefact():
        # It does! Pick it up.
        action = PickupAction()
    else:
        # The cell does not contain an artefact
        # Find the location of all artefacts nearby
        nearby = world_state.scan_nearby(avatar_location)
        
        # Get the nearest one
        nearest = nearby[0]
        
        # Move towards the nearest arteface
        action = MoveTowardsAction(nearest)
        
    my_backpack = avatar_state.backpack
    length = len(my_backpack)
    print("I have", length, "things in my backpack.")
    index = 0
    phone_count = 0
    coins_count = 0
    keyboard_count = 0
    while index < length:
        artefact_type = my_backpack[index].type
        print(f"The artefact at position {index} is a {artefact_type}")
        if artefact_type == "phone":
            phone_count = phone_count + 1
        elif artefact_type == "coins":
            coins_count = coins_count + 1
        else:
            keyboard_count = keyboard_count + 1
        index = index + 1
    print(f"I have {phone_count} phones, {coins_count} coins and {keyboard_count} keyboards")
    
    return action

Task 4: Drop artefacts by type

def next_turn(world_state, avatar_state):
    # Find out where your avatar is
    avatar_location = avatar_state.location
    current_cell = world_state.get_cell(avatar_location)

    # Check if the current cell holds an artefact
    if current_cell.has_artefact():
        # It does! Pick it up.
        action = PickupAction()
    else:
        # The cell does not contain an artefact
        # Find the location of all artefacts nearby
        nearby = world_state.scan_nearby(avatar_location)
        
        # Get the nearest one
        nearest = nearby[0]
        
        # Move towards the nearest arteface
        action = MoveTowardsAction(nearest)
        
    my_backpack = avatar_state.backpack
    length = len(my_backpack)
    print("I have", length, "things in my backpack.")
    index = 0
    phone_count = 0
    coins_count = 0
    keyboard_count = 0
    while index < length:
        artefact_type = my_backpack[index].type
        print(f"The artefact at position {index} is a {artefact_type}")
        if artefact_type == "phone":
            phone_count = phone_count + 1
        elif artefact_type == "coins":
            coins_count = coins_count + 1
        else:
            keyboard_count = keyboard_count + 1
        index = index + 1
    print(f"I have {phone_count} phones, {coins_count} coins and {keyboard_count} keyboards")
    
    if coins_count > 0:
        index = my_backpack.find("coins")
        action = DropAction(index)
        
    # OR
    index = my_backpack.find("coins")
    if index >= 0:
        action = DropAction(index)
    
    return action

Last updated

Logo

Rapid Router resources

KS1KS2KS3

Kurono resources

TeacherStudent

© Copyright 2023 Ocado Group plc. All rights reserved