ST EK List:
1.2.2A Computing tools and techniques can enhance the process of finding a solution to a problem.
4.1.1H Different algorithms can be developed to solve the same problem.
4.1.1I Developing a new algorithm to solve a problem can yield insight into the problem.

Robot in a Maze

In this project, you will program a robot to get through several mazes.
Robot in A Simple Maze

Five mazes have been created for you. The code you write to escape each maze should be as simple, concise, and elegant as possible.

For example, this solution works for Maze 1:
Escape Maze 1{Rotate Left; repeat(7){Move Forward}; Rotate Right;repeat(7){Move Forward};} Maze 1 path

But nesting one repeat block inside another makes the code more concise and, most programmers would agree, more elegant:

This works because once the Robot reaches the destination, we don’t care which direction it's facing.
Rotate Left; repeat(2){repeat(7){Move Forward}; Rotate Right}

The AP CSP Exam Reference Sheet uses a text based code for these blocks: MOVE_FORWARD(), ROTATE_RIGHT() and ROTATE_LEFT().

  1. Click here to load this file. Then save it to your Snap! account.
    Examine both the Robot and the Board sprites. For each of the given mazes, write the shortest and most elegant code to help the robot escape. Use only these four custom Motion blocks (along with whatever Control blocks you need):

    Go To Start, Move Forward, Rotate Right, Rotate Left


    Maze 1 path Maze 2 path Maze 3 path Maze 4 path Maze 5 path
  2. Analyze this code. On graph paper, draw the maze this code was written for. Do not use Snap!.
    Escape Your Maze{repeat(2){repeat(7){Move Forward};Rotate Left; Move Forward; Move Forward; Rotate Left; repeat(7){Move Forward};Rotate Right; Move Forward; Move Forward; Rotate Right}}
  3. Talk with Another Pair Share your drawing with other students, and discuss how you made it.
  4. Now Is a Good Time to Save

Self Check: Robot

  1. This question is similar to those you will see on the AP CSP exam.
    The robot in the image below can move into a white square and must end up at the gray square. It cannot move into a black area.
    image of robot in a maze
    Below is a procedure TurnAndMove:
    PROCEDURE TurnAndMove (numberOfTurns, numberOfMoves)	
      {
        REPEAT numberOfTurns TIMES
        {
          ROTATE_LEFT ()
        }
        REPEAT numberOfMoves TIMES
        {
          MOVE_FORWARD ()
        }
      }

    Which of the following code will move the robot to the gray square?
    TurnAndMove(1,3)
    TurnAndMove(1,5)
    TurnAndMove(1,2)
    TurnAndMove(1,3)
    TurnAndMove(3,5)
    TurnAndMove(1,2)
    TurnAndMove(3,1)
    TurnAndMove(5,3)
    TurnAndMove(2,1)
    TurnAndMove(1,4)
    TurnAndMove(3,6)
    TurnAndMove(1,3)
 
  1. Create your own maze and a robot escape code for that maze.
    • Analyze the Draw Your Maze code in the Board sprite (shown below).
    • Experiment. If you change some of the zeros to ones, you can create a path for the robot to travel.
    • The board matrix variable contains a list of lists. Each separate list of zeros and ones will correspond to a row of squares in the maze.
    • Edit the board matrix to make a maze you like, then write the Escape Your Maze code for the robot.
    Draw Your Maze code