Back to the tutorial

Explore a maze

The maze challenge

Guide the turtle from E10 (X=4, Y=9), facing up, to the goal flag at I8 (X=8, Y=7). Walls block movement; the edge of the board also counts as blocked. This is the complexMaze2 board used by the original maze example.

Starting maze · E10 → I8
Goal reached · I8

Follow the left-hand rule

  1. If the path on the left is open, turn left.
  2. Otherwise, if the path ahead is blocked, turn right.
  3. After choosing a direction, move one square only if the path ahead is open.
  4. Check for the goal after each turn of the loop. Stop at the flag; otherwise repeat.

At a dead end, one right turn may still leave a wall ahead. The turtle stays put and tries again on the next loop. This left-hand rule can take detours: it does not search for the shortest route, and it is not guaranteed to solve every possible maze.

Try the step-through debugger ↓

TLAC instruction cards

Read the numbered cards, then try Next below. Function definitions describe reusable instructions: their bodies run only when called. The loop runs its body before checking its stop condition: no means continue, yes means stop.

0001

Define function: Check whether the goal is still ahead · Run these cards only when called.



0002

Return to the caller with result: not (the turtle is on the goal flag)



0003

Define function: Check the path ahead · Run these cards only when called.



0004

Return to the caller with result: the square ahead is inside the board and has no wall



0005

Define function: Check the path on the left · Run these cards only when called.



0006

Turn left



0007

Set result to Call “Check the path ahead”



0008

Turn right



0009

Return to the caller with result: result



0010

Define function: Turn left · Run these cards only when called.



0011

Turn left



0012

Define function: Turn right · Run these cards only when called.



0013

Turn right



0014

Define function: Move forward · Run these cards only when called.



0015

Move forward one square



0016

Define function: One turn of the maze loop · Inputs: current · Run these cards only when called.



0017

If Call “Check the path on the left”, then



0018

Call “Turn left”



0019

If not (Call “Check the path ahead”), then



0020

Call “Turn right”



0021

If Call “Check the path ahead”, then



0022

Call “Move forward”



0023

Define function: Check whether to stop the loop · Inputs: current, unused · Run these cards only when called.



0024

If Call “Check whether the goal is still ahead”, then



0025

Return to the caller with result: no



0026

Otherwise



0027

Return to the caller with result: yes



0028

Use “One turn of the maze loop” as the repeated action



0029

Use “Check whether to stop the loop” as the stop check



0030

Run one turn, then check whether to stop; repeat until the stop check says yes


Simplified Python source

Show every step and its board