Student challenge 1
These are possible solutions for challenge one.
Starter code
def next_turn(world_state, avatar_state):
new_dir = direction.NORTH
# Your code goes here
action = MoveAction(new_dir)
return action
Task 1: Change direction
def next_turn(world_state, avatar_state):
new_dir = direction.SOUTH # E.g. to move south instead
action = MoveAction(new_dir)
return action
Task 2: Move in all directions
import random
def next_turn(world_state, avatar_state):
# Choose a random direction to move in
number = random.randint(1,4)
if number == 1:
new_dir = direction.NORTH
elif number == 2:
new_dir = direction.EAST
elif number == 3:
new_dir = direction.SOUTH
else:
new_dir = direction.WEST
action = MoveAction(new_dir)
return action
Task 3: Investigate location
First part
import random
def next_turn(world_state, avatar_state):
# Get a random number between 1 and 4
number = random.randint(1,4)
# Use this number to pick a direction
if number == 1:
new_dir = direction.NORTH
elif number == 2:
new_dir = direction.EAST
elif number == 3:
new_dir = direction.SOUTH
else:
new_dir = direction.WEST
# Ask Kurono for details about the cell you would move into
next_location = avatar_state.location + new_dir
print("The co-ordinates of the next cell are", next_location)
if world_state.can_move_to(next_location):
print("Yes, I can move")
action = MoveAction(new_dir)
else:
print("I can't move there!")
action = WaitAction()
return action
Final step: shortening the code
import random
def next_turn(world_state, avatar_state):
# Get a random number between 1 and 4
number = random.randint(1,4)
# Use this number to pick a direction
if number == 1:
new_dir = direction.NORTH
elif number == 2:
new_dir = direction.EAST
elif number == 3:
new_dir = direction.SOUTH
else:
new_dir = direction.WEST
# Ask Kurono for details about the cell you would move into
next_location = avatar_state.location + new_dir
print("The co-ordinates of the next cell are", next_location)
if not world_state.can_move_to(next_location):
print("I can't move there!")
action = WaitAction()
else:
action = MoveAction(new_dir)
return action
Last updated