03 / Think like a computer

Code a Rover

Plan a path. Give clear instructions. See where your ideas take you.

0 / 6 discoveries
Expedition 010 instructions

▲ Rover · ◆ Crystal · ▧ Rock

Your program

    Try picturing the route before adding instructions.

    Rover instructions

    Plan your route

    Reach the crystal without hitting rocks or leaving the grid. Forward moves one square; left and right turn the rover without moving it. Directions are from the rover’s point of view.

    Using blocks

    Press Forward, Left, or Right to add an instruction. Your program runs in numbered order.

    Drag instructions to change their order. Tap to remove. With a keyboard, hold Alt and press an arrow key to move a focused instruction.

    In Creative trail mode, choose a trail colour and press Colour block to add a colour change to your program. Try drawing a staircase or a spiral.

    Run and learn

    Press Run to try your program, Stop to pause execution, or Clear to reset the rover and program. After solving an expedition, Show optimal solution opens a Python example. Back to my code restores your own work.

    Advanced coding

    Write Python for your rover. Use four spaces inside a loop or decision. Enter continues the indentation and Tab adds four spaces; Shift+Tab moves focus out of the editor.

    Rover command guide

    The setup line creates rover for you. col is the column (1–6 from left to right), row is the row (1–6 from top to bottom), and facing="east" means it starts pointing right. The starting square is fixed by the expedition; you do not need to type the setup line again.

    Every Vehicle command

    Movement and turn commands change the rover and return None. Each call counts as one instruction. Turning keeps the rover on the same square; moving into a rock or off the grid stops the program with an error.

    # Move one square in the direction you face.
    rover.forward()
    
    # Move one square behind you; keep facing the same way.
    rover.backward()
    
    # Rotate 90 degrees anticlockwise from your current heading.
    rover.turnLeft()
    
    # Rotate 90 degrees clockwise from your current heading.
    rover.turnRight()
    
    # Face a compass direction without moving.
    # turn(direction: str) accepts these four lowercase names:
    rover.turn("north")  # Up the grid.
    rover.turn("east")   # Right across the grid.
    rover.turn("south")  # Down the grid.
    rover.turn("west")   # Left across the grid.
    
    # Return True if the square ahead is inside the grid
    # and has no rock; otherwise return False. Does not move.
    rover.isClear()
    
    # Return True if you are on the target square;
    # otherwise return False. Does not move.
    rover.atGoal()
    
    # Change the rover and trail colour (creative mode only).
    # colour(name: str): "purple", "blue", "green", "orange", "pink".
    rover.colour("blue")

    The two questions, isClear() and atGoal(), do not count as movement instructions. isClear()checks ahead, not behind. In creative mode, atGoal() checks the top-right square even though no crystal is shown. Invalid directions or colours produce an error.

    Loops and decisions

    Indent the instructions inside each block by four spaces. Text after # is a comment for you; Python ignores it. These are separate examples to adapt to your route.

    # Repeat an instruction three times.
    for _ in range(3):
        rover.forward()
    
    # Keep moving until a rock or the edge is ahead.
    while rover.isClear():
        rover.forward()
    
    # Choose what to do based on the square ahead.
    if rover.isClear():
        rover.forward()
    else:
        rover.turn("north")
    
    # Print an answer below the editor without moving.
    print("At the target?", rover.atGoal())

    You can also use variables, functions, and standard Python expressions. not reverses a True/False answer, and pass leaves a block empty.

    Python runs in an isolated browser sandbox. You can import bundled standard-library modules such as math and random. Network access and package installation are disabled. Files are temporary and reset each run. Programs stop after 5 seconds or 512 rover instructions.