CS302, UW Madison, Fall 2005

Lab 1

Navigating Mazes

What we'll cover today:

  • Writing Simple Programs
  • Using Methods and Objects

The Maze

In this lab you will use methods to navigate a character through a maze. Let's get familar with your character: meet Puss in Boots

Puss can turn right and left, move backward and forward. He can put on and take off his boots, he can also tiptoe and jump if need be. This is very helpful for you because there will be obstacles within the maze that you will have to help puss move around. Examples of these obstacles are mud, a sleeping dog, and a gully.

To run the maze program, go into the MazeLab folder in Eclipse and expand the default package list by clicking on the plus symbol. Then right click on MazeLab.java, go down to the run option and click on run Java Application. This will make a java frame pop up that will run your maze program. Here is an example of what the window will look like:

Here's a quick summary of the most important buttons:

  • Run -- runs the MazeLab program.
  • Reset -- returns Puss to the beginning of the maze. Reset must be hit before you can Run the program again.
  • Step -- Each press of Step executes one line of the program (one statment). Step cannot be used if the program has been Run unless Reset has been pushed.

  • Task 1: Run MazeLab.java and try out the different actions that Puss can take. Move him around the maze and see which methods get Puss around the obstacles. Pay attention to the console as well, because some of the methods and some of your actions may cause information to be printed to the console.
  • Task 2: Come up with a sequence of actions that will move Puss through the maze (dealing with any obstacles along the way) to the finish.

  • Exercise 1. Getting Puss through the Maze

    Now you're ready to write your first program! The goal is to write code that will navigate Puss through the maze.

    In today's lab, you are going to modify the run() method in the MazeLab.java file. Both the file and the method have already been created for you, all you need to do is add your code to run(). The file looks like this:

      import java.awt.*; /* CS302 Lab 1 -- Exercise 1 -- Fall 2005 Move Puss in Boots through the maze. Puss' actions should go in the run() method. */ public class MazeLab extends Maze { public void run () { /* Erase or comment out the following and add your own code here. */ puss.forward(2); puss.right(); } public static void main(String [] args) { MazeLab myMaze = new MazeLab(); } }

    Here are the methods that you have at your disposal:

  • forward() or forward(int n) - These methods both move Puss forward. The first moves him forward one step and the second moves him forward by n steps.
  • backward() or backward(int n) - These methods are the same as above only they move Puss backwards.
  • right() - Turns Puss to the right.
  • left() - Turns Puss to the left.
  • jump() - This method jumps Puss over one square.
  • startTipToe() - Puss starts to tip toe as he moves.
  • stopTipToe() - Puss stops tip toeing.
  • putOnBoots() - Puss puts on his boots.
  • takeOffBoots() - Puss takes off his boots.
  • Once you think you have a working solution, run MazeLab.java and click the run button on the bottom of the screen. Puss should move through the maze and a message should be printed when he reaches the finish square. If Puss mysteriously goes back to the start of the maze, check your console, you will get a message that tells you what is going wrong.

    Good Luck!