# Programming Project #1: Mazes and Cells

Originally posted 8 September 2011, due at 11:59 p.m. EDT on Monday 19 September 2011, by placing .java files in your cs187 directory on your edlab account. For more information on accessing the edlab (Question P1.2), and answers to other questions on this assignment, see the Q&A page.

Correction in orange added 10 September 2011.

Correction in green added 11 September 2011.

Corrections in blue added 12 September 2011. The purpose of these is so that whenever we do something with both dimensions of a two-dimensional array, we have the x-direction (horizontal, or east-west) first.

Correction in purple added 15 September 2011.

Goals of this project:

1. Submit a compilable and correct program to us through the EdLab.
2. Write a program using objects and classes.
3. Begin the code base for later projects involving mazes.
4. Learn (or review) arrays, including two-dimensional arrays.

Many computer games, such as Sid Meier's Civilization series, involve pieces moving on a square grid of cells. In this project you will write a Maze class, allowing you to create Maze objects that are rectangular arrays of Cell objects. You will also write the Cell class. In the future we will extend the Cell class to make it more interesting, but for this project a Cell has only three fields: `int x` and `int y` giving its position in its Maze, and `boolean open` telling whether it is open to be moved into.

A Maze has three fields in all. The first two are `int width` and `int height`, giving the number of columns and the number of rows respectively, and the third field is a two-dimensional array of Cell objects.

Each class should have the usual get and set methods, a toString method, and constructors as specified below. The toString method for Cell gives a string such as `(2, 3) open` if the Cell is at position x = 2 and y = 3 and is open, or `(2, 3) closed` if it is in that position and not open. The toString method for a Maze of width w and height h is a sequence of h binary strings, separated by line breaks, where each individual string has length w. Open Cells are represented by ones and closed ones by zeros. For example, if m is a Maze of width 4 and height 3, where exactly those Cells on the boundary are open, then `m.toString()` would return the String "1111\n1001\n1111", which is printed out as:

``````
1111
1001
1111``````

(This previously had \r's, "carriage returns", instead of the intended \n's, "newlines".)

The Cell class should have the following two constructors:

• `Cell (int x, int y)` -- gives an open Cell with those values for x and y
• `Cell (int x, int y, boolean isOpen)` -- gives a Cell with those x and y values, open or not according to isOpen

The Maze class should have the following two constructors:

• `Maze (int w, int h)` -- gives a Maze of width w and height h with a Cell in each place, the correct x and y for each Cell, and all cells open
• ```Maze (int w, int h, String [ ] init)``` -- gives a Maze of width w and height h with a Cell in each place as above, but the openness of the Cells is specified by the array `init`, which should be an array of h binary strings, each of length w. For example, we would create the Maze above by the constructor ```Maze m = new Maze (4, 3, {"1111", "1001", "1111"})```.

Actually you need to do this with two statements:

``````String [ ] s = {"1111", "1001", "1111"};
Maze m = new Maze (``````4, 3, s);
``````

For more see the Q&A page, question P1.4.

The final part of the assignment (necessary to get an A) is to add an instance method `moves` to the Maze class. This method takes two int arguments and returns an array of Cells, so its signature is ```Cell [ ] moves (int col, int row)```. The array returned contains from zero to four Cells, and these Cells are to be exactly the open Cells in the Maze that can be reached by one move up, down, right, or left from the Cell at (col, row). The method should throw an `ArrayIndexOutOfBoundsException` (this used to say `ArrayOutOfBoundsException` -- see Question P1.6 on the the Q&A page) if (col, row) is a position that does not exist in the Maze. But it should not throw an exception in any other case -- this means that you will have to be careful when (col, row) is on the boundary of the Maze.

If m is our example Maze above, the call ```m.moves(0, 0)``` should return an array of two Cells, the first one having toString "(1, 0) open" and the second one having toString "(0, 1) open". If we call `m.moves(1, 2)` we again get an array of two Cells, which have toStrings "(0, 2) open" and "(2, 2) open". We don't get the Cell at (1, 1) because it is closed and we don't get a Cell at (1, 3) because that it outside the Maze.