Mouse programming simulator

This mouse programming simulation introduces you to the fundamental concepts of computer programming. Programming involves putting simple instructions together, and in the right order, to accomplish a complex task.

The mouse simulator also introduces you to the typical way in which programs are made: source code is written in a text editor; the source code is converted to an executable program using a compiler; and, the compiled code is run in a simulator or directly on the target device. Based on the program's success, programmers repeat this process as the program code is debugged and refined.


:Write your code here!

1. Select a maze to try.


2. Write your program in the editor. Save your program.

3. Run and debug your code.


Speed up the simulation by decreasing the seconds count between steps.

The mouse programming challenge

Create a program that directs the mouse to find the cheese in any possible maze using only the simple mouse control commands, below:

Mouse command syntax

Command Action
Step Forward Moves the mouse one space forward (in the direction it is facing) and stops.
Turn Right Rotates the mouse 90° to the right.
Turn Left Rotates the mouse 90° to the left.
:Label Names a block of code in your program. Label can be any single word, but cannot be the same as a command or contain spaces.
Goto Label Jumps to the block of code named with :Label.
If Obstacle Goto Label Checks one step in front of the mouse and jumps to the block of code named with :Label if an obstacle is present.

The primary challenge

The difficulty of guiding your mouse to the cheese is that your program code should work under any possible conditions. This means that the mouse should be able to find the cheese in any maze, not just the example mazes used here. Think of these examples as some of the many possibilities the mouse might encounter in its world.

In other words, that means the real challenge of this activity is to write only one program that will allow the mouse to find the cheese in every possible maze, not just the ones shown here.

Given the mouse's limited instruction set, and the design of some of the mazes, your program may not be able to solve all of the mazes. If you can solve the first ten mazes using just one program, you're doing really well! (It can be done in 10 lines of code!)

A secondary challenge

Not only should your program solve any possible maze, it should solve the maze using the fewest possible instructions and/or program steps.

Why? Just like real computer programming, shorter programs take fewer computing resources and execute faster. Smaller programs are more memory and energy efficient, which is an important consideration in things like smartphone apps. How efficient can you make your code?

A simple example program

This simple program demonstrates some important mouse programming concepts:

if obstacle goto turn
step forward
goto walk

turn right
goto walk

Copy the program code into the editor, above. Then, click the Run button to watch the program run.

How does it work?

The example program makes the mouse walk forward until it encounters an obstacle. Then, the mouse turns and continues to walk in a new direction. This process of walking, then turning, repeats forever, or until the mouse finds the cheese.

Keep walking

The first four lines of the program create a function or subroutine known as a loop. A function/subroutine is a block of code that accomplishes a specific task; in this case that task is walking. The :walk label starts the subroutine and is simply a placeholder that names the code and provides a destination for the later goto command.

Before taking a step, it's important to check for an obstacle. In programming terms, the if obstacle goto command is known as a conditional branch. If the condition is true, the code branches to the :turn subroutine. If there is no obstacle in front of the mouse, the condition is false, and the code continues on the next line by having the mouse step forward.

The final goto walk command is an unconditional branch that causes the mouse to restart the program at the 'walk' label, forming a loop.

The result of the mouse executing this loop is that the mouse keeps moving forward until it encounters an obstacle, and it doesn't matter how long the path is before it has to turn.

Take a turn

The :turn subroutine routine is invoked when the :walk routine encounters an obstacle.

Turn simply turns 90° to the right and then loops back to the walk routine to try moving forward again.


Most real-world programs are written in a similar way, and contain a main loop (walk, in this case) that repeats over and over. The main loop calls other subroutines (like turn) to respond to specific inputs, after which the program continues running the main loop.



This simulator works in browsers supporting modern HTML 5 features. It does not work in older versions of Internet Explorer. If the maze doesn't show up at first, refresh the page or open it again.

I'm stuck!

The example code is reactive in design — that is, the mouse reacts by making a decision after an obstacle is encountered. To get past maze 6 your code will need to actively seek what it's looking for. How can you write a program to do that?

Go old school

You may also download the paper version.