Computer Science 136 Assignment 2|Mazesbailey/cs136/Labs_files/lab2-Mazes.pdf · Please, just turn...

6
Computer Science 136 Assignment 2—Mazes Due: Monday, at noon. This week we are going to develop some classes that allow us to experiment with mazes. All of these classes share a common interface: the participant in the maze moves in the compass directions north, south, east, and west. A traditional maze drawn on squared graph paper is one example, but this week we will look at a couple of other maze types that we will collectively call compass mazes . Introduction. Suppose want to build an abstract data type that models our interaction with a compass-like maze. We would want to be able to have the maze constructed , and then, standing at the starting location, observe the state of our enviroment by looking around. Based on what we observe, we would want to move in a direction (e.g. north). This process would continue until we find ourself at the goal . More technically, we can describe a Java interface—a contract between a class and its user. It would look something like the following: public interface CompassMaze // first approximation! { public static final int NORTH = 0; public static final int EAST = 1; public static final int SOUTH = 2; public static final int WEST = 3; public String toString(); // post: returns a string describing Maze’s state. public boolean move(int direction); // pre: direction is NORTH, EAST, SOUTH, or WEST // post: logically moves the traveller in the direction indicated. // Returns true if and only if the traveller actually moved. public boolean atGoal(); // post: returns true if (and only if) the traveller is at the goal. // Typically, we think of the maze as having been conquered if the // the traveller has reached the goal. } An interface is a public view of an abstract data type. This contract indicates the methods that are important to the user and must be fleshed out by the implementor. No code actually appears in the interface. Given this, we can build a simple program that exercises any maze that implements this interface. This ‘driver’ would read directions from the keyboard and move through the maze accordingly. The driver would stop when it reaches the goal. Here’s an outline of that procedure: CompassMaze m = new SojournerMaze(); // specific maze is currently unknown Scanner s = new Scanner(System.in); String direction; System.out.println(m); // prints the initial state of the maze while (!m.atGoal()) { barrier = 0; System.out.println("Which direction do you want to go?"); do { Assert.condition(s.hasNext(),"You must type a command."); direction = s.next(); direction = direction.toLowerCase(); } while (!(direction.startsWith("n") || direction.startsWith("e") || direction.startsWith("w") || direction.startsWith("s"); switch(direction.charAt(0)) {

Transcript of Computer Science 136 Assignment 2|Mazesbailey/cs136/Labs_files/lab2-Mazes.pdf · Please, just turn...

Page 1: Computer Science 136 Assignment 2|Mazesbailey/cs136/Labs_files/lab2-Mazes.pdf · Please, just turn in the Java code associated with your Maze. Having a Little Fun. When your maze

Computer Science 136Assignment 2—MazesDue: Monday, at noon.

This week we are going to develop some classes that allow us to experiment with mazes. All of these classes sharea common interface: the participant in the maze moves in the compass directions north, south, east, and west. Atraditional maze drawn on squared graph paper is one example, but this week we will look at a couple of other mazetypes that we will collectively call compass mazes.

Introduction.Suppose want to build an abstract data type that models our interaction with a compass-like maze. We wouldwant to be able to have the maze constructed , and then, standing at the starting location, observe the state of ourenviroment by looking around. Based on what we observe, we would want to move in a direction (e.g. north). Thisprocess would continue until we find ourself at the goal .

More technically, we can describe a Java interface—a contract between a class and its user. It would look somethinglike the following:

public interface CompassMaze // first approximation!

{

public static final int NORTH = 0;

public static final int EAST = 1;

public static final int SOUTH = 2;

public static final int WEST = 3;

public String toString();

// post: returns a string describing Maze’s state.

public boolean move(int direction);

// pre: direction is NORTH, EAST, SOUTH, or WEST

// post: logically moves the traveller in the direction indicated.

// Returns true if and only if the traveller actually moved.

public boolean atGoal();

// post: returns true if (and only if) the traveller is at the goal.

// Typically, we think of the maze as having been conquered if the

// the traveller has reached the goal.

}

An interface is a public view of an abstract data type. This contract indicates the methods that are important tothe user and must be fleshed out by the implementor. No code actually appears in the interface.

Given this, we can build a simple program that exercises any maze that implements this interface. This ‘driver’would read directions from the keyboard and move through the maze accordingly. The driver would stop when itreaches the goal. Here’s an outline of that procedure:

CompassMaze m = new SojournerMaze(); // specific maze is currently unknown

Scanner s = new Scanner(System.in);

String direction;

System.out.println(m); // prints the initial state of the maze

while (!m.atGoal()) {

barrier = 0;

System.out.println("Which direction do you want to go?");

do {

Assert.condition(s.hasNext(),"You must type a command.");

direction = s.next();

direction = direction.toLowerCase();

} while (!(direction.startsWith("n") || direction.startsWith("e") ||

direction.startsWith("w") || direction.startsWith("s");

switch(direction.charAt(0)) {

Page 2: Computer Science 136 Assignment 2|Mazesbailey/cs136/Labs_files/lab2-Mazes.pdf · Please, just turn in the Java code associated with your Maze. Having a Little Fun. When your maze

case ’n’: m.move(CompassMaze.NORTH); break;

case ’e’: m.move(CompassMaze.EAST); break;

case ’s’: m.move(CompassMaze.SOUTH); break;

case ’w’: m.move(CompassMaze.WEST); break;

}

System.out.println(m); // remember: calls toString

}

System.out.println("You made it!");

Notice that we can write this code before we actually write any maze class (represented here by the hypotheticalname, SojournerMaze) that implements the methods of the the interface.

The Objective.Our job this week is to implement a class that meets the specifications of the CompassMaze interface. We indicatethis with the Java implements keyword:

public class SojournerMaze implements CompassMaze

{ ... }

Internally, the class stores a representation of a maze. It also keeps track of where the movable object—the traveller—is located. As the driver attempts to walk the traveller through the maze, the maze object updates its stateappropriately. Of course sometimes the request to move in a particular direction might be inappropriate, in whichcase the move does nothing (the maze might signal this fact by returning a boolean that indicates whether “the moveworked”).

Getting Started.On the web side is a “starter kit” for this lab, called CompassMaze.tar.gz. You should download this file into yourcs136 folder and, from within the terminal window, unpack its contents:

tar xvfz CompassMaze.tar.gz

This will create a subdirectory (CompassMaze) that contains

1. This writeup.

2. The source for the CompassMaze interface. This interface includes a number of methods not discussed here.Read through the class carefully to see the intent of each promised method.

3. The source for the driver program, MazeDriver. When you create your class, you should put the name of yourclass where the constructor is called.

You should implement any maze that can be traversed by moving in any of the four compass directions. A traditionalmaze is satisfactory, but I’ve attached descriptions of some interesting alternatives. Read through these suggestionsbefore you make a decision. When you name your maze class, try to include your name in the class name, so thatothers can use your maze without name conflicts.

Finishing Up.Once you have constructed your maze class, you should make sure it runs under the control of the MazeDriver

program. If it does, you’ve finished the core of the lab, and you can turn in your lab work with a command similarto

turnin -c 136 BaileyMaze.java

Please, just turn in the Java code associated with your Maze.

Having a Little Fun.When your maze is fully tested, you can mail your class file (not the java file) to your classmates at [email protected], send only one message to this account! When you receive a class file in the mail, you can drop it into yourfolder and force your MazeDriver application to construct objects of that type. In this way, we can run our friends’code, without sharing the source.

2

Page 3: Computer Science 136 Assignment 2|Mazesbailey/cs136/Labs_files/lab2-Mazes.pdf · Please, just turn in the Java code associated with your Maze. Having a Little Fun. When your maze

Robert Abbott’s maze, Jumping Jim

Robert Abbott is arguably the most important maze innovator over the past two or three decades. His efforts havebeen published in two books, Mad Mazes and Super Mazes. These two books introduce new ways of building mazes,including some that change form, based on the traveller’s interaction with the maze itself.

In Mad Mazes he introduced Jumping Jim, a puzzle that is played using a two dimensional grid of integers. Thetraveller starts in the upper left, and seeks to get to the lower right. At each move, he looks at the number in hissquare and jumps horizontally or vertically that many squares, always making sure that he stays within the boundsof the maze. In a series of jumps, the hope is to make it to the lower right.

If you opt to implement this type of maze, it is sufficient to implement the particular configuration of Jumping Jim,which uses the grid:

3 6 4 3 2 4 32 1 2 3 2 5 22 3 4 3 4 2 32 4 4 3 4 2 24 5 1 3 2 5 44 3 2 2 4 5 62 5 2 5 6 1 ?

For more credit, you might construct (or discuss how you might automatically construct) an alternative to thisparticular configuration.

I have attached a facsimile of his artwork in pages that follow. Robert Abbott has an interesting web site atwww.logicmazes.com.

ThinkFun’s Lunar LockoutThe Japanese master of maze and puzzle design is (the late) Nob Yoshigahara. Nob is responsible for the conceptsbehind a large number of mechanical puzzles you would probably recognize, including Rush Hour manufactured byThinkFun.

Another puzzle developed by Nob and his friend, Hiroshi Yamamoto, spawned ThinkFun’s Lunar Lockout . Five orsix robots reside in a 5-by-5 square grid. They are numbered starting at zero (in the ThinkFun puzzle, this is thered robot). Their collective goal is to get Robot Zero to the center square where (as it says on the box) “it lands onthe Emergency Entry Port in the Mothership’s Landing Grid.” Whatever.

It works in the following manner:

1. Each robot is placed in its own cell as part of an initial configuration. For example, the first challenge involvesthe following setup:

— — — — 1— — 2 — —— 3 ? — —— — — 4 —— — — — 0

2. One of the five robots is moved. A robot can move horizontally or vertically, sliding along the grid until itbumps into another robot. It must bump into a robot; if it doesn’t, it slides into outer space, to be lost forever,thus reducing the capability of the Mothership (“Bad robot!”).

3. Play continues with robots sliding and bumping, sliding and bumping.

4. The puzzle is solved when Robot Zero (“The Spacepod Xanadu”) slides and bumps, stopping on the centersquare.

This style of maze involves potentially more than one traveller, or movable. To support this type of puzzle, twomethods in the CompassMaze interface—numMoveables and a version of move that takes the robot id—directlysupport this type of maze. You will also notice that the MazeDriver program will accept a small integer before amove in case there is more than one piece in the puzzle.

I have attached, at the end, a page that includes a number of puzzles from Lunar Lockout. These were developedby ThinkFun, of course, and they should be credited if you use one in your solution. You can find Lunar Lockout atWhere’d You Get That? , or visit ThinkFun’s web site at http://www.puzzles.com/products/lunarlockout.html.

Page 4: Computer Science 136 Assignment 2|Mazesbailey/cs136/Labs_files/lab2-Mazes.pdf · Please, just turn in the Java code associated with your Maze. Having a Little Fun. When your maze

Robert Abbott’s introduction to Jumping Jim

Page 5: Computer Science 136 Assignment 2|Mazesbailey/cs136/Labs_files/lab2-Mazes.pdf · Please, just turn in the Java code associated with your Maze. Having a Little Fun. When your maze

Robert Abbott’s Jumping Jim configuration.

Page 6: Computer Science 136 Assignment 2|Mazesbailey/cs136/Labs_files/lab2-Mazes.pdf · Please, just turn in the Java code associated with your Maze. Having a Little Fun. When your maze

Several puzzles from Lunar Lockout by ThinkFun.