CS-151 Labs > Lab 5. Stack and Queue


Part 0. Representing the Maze

Create a package lab5 in your lab5 project. Download and unzip the following zip archive: lab5.zip. Now import all the files into your project. Make sure that Java files are under the lab5 package, and the mazes folder is in the project directory.

Let’s talk about how we can implement a basic maze. It has walls and pathways as well as a starting point and an ending point. Furthermore, one wall is just like another, and any open space (not including start and finish) is also identical. So, we can think of a maze as being made up of individual squares, each square either empty, a wall, the start, or the exit. Below is a graphical representation of the maze found in the file maze-2. The green box represents the start, the red box the exit, and the black squares the walls.

Maze 2

We represent such a maze with a text file of the following format:

The first line of the file contains two integers. The first indicates the number of rows (R), the second, the number of columns (C).

The rest of the file will be R rows of C integers.

The value of the integers can be one of:

In terms of coordinates, consider the upper left corner to be position (0, 0) and the lower right to be (R-1, C-1). For example, this is the text version of the maze above (start is at (6, 4) and exit at (6, 11)).

7 13
0 0 0 0 0 0 1 0 0 0 0 0 0
1 1 0 1 1 1 1 1 1 1 0 1 0
0 1 0 0 0 0 0 0 0 0 0 1 0
0 1 0 1 1 0 0 1 1 1 0 1 0
0 0 0 1 0 0 0 0 0 1 0 1 0
0 1 1 1 1 1 0 1 0 1 0 1 0
0 0 0 0 2 0 0 1 0 0 1 3 0

The Square Class

We have given you code for a class Square that represents a single square in the maze. Squares hold their coordinates and an int representing their type (empty - 0, wall - 1, start - 2 or exit - 3). Below are descriptions of the Square methods you will need to use—for other methods, see the code.

public Square(int type, int row, int col)
constructor to create a new Square object
public int getRow()
public int getColumn()
public int getType()
Getters for the various instance variables.
public static final int SPACE = 0, WALL = 1, START = 2, EXIT = 3;
These are possible Square types. You can use them for finding out the type of a given square sq – for example
if (sq.getType() == Square.SPACE)
public boolean isOnPath()
public void setOnPath()
public boolean isMarked()
public void mark()
public void reset()
These are getters/setters for attributes you will use to keep track of what you have done with the square while solving the maze. The setOnPath() and mark() methods set their respective values to true; the reset() method sets all of the values to false.
public void setPrevious(Square s)
public Square getPrevious()
Sets and gets the previous square you accessed. You will use this to build the path in MazeSolver.

The Maze Class

We have also provided you with a Maze class that stores the logical layout of a maze in the form of a two-dimensional array of Square objects. You will be interested in the following methods (for more, see the code):

ArrayList<Square> getNeighbors(Square sq)
Returns an ArrayList of the Square neighbors of the parameter Square sq.
Square getStart()
Returns the starting square of the maze.