To learn about programming with inner classes, get experience working with “2-D lists”, and lay the groundwork for the maze-navigating program you'll write in the next assignment.
The code that you write for this assignment will build on top of the same List ADT and implementation as the People Sorter assignment. The three files you will need are:
Every source-code file you turn in must have your name in a comment at the top.
This is optionally a partner assignment. If you'd like to work in a pair, please figure out who you'd like to work with via Piazza or email, and then email me once your pair is decided. Keep in mind that there's a second half to this assignment, so if you work alone or in a pair on this assignment, you should do the same for the next one. Make sure to put both people's names at the top of each file. Write your code by pair-programming: code together, at the same computer, and switch off who's at the keyboard. Only one of you should ultimately submit your team's code via Moodle.
You'll write a class called Maze
. Each instance of Maze
will represent a single maze. Maze.java will also include a main()
method that acts as a test of the loading and printing of mazes. When you run java Maze maze.txt, your program should load the maze from maze.txt and print a human-readable representation of the maze to System.out
.
For example, if maze.txt is:
6 5 0 0 5 4 L-_|_- |L--|_ |-_-|_ |L|L|| L__L__
then the printed output should be:
+-----+-----+-----+-----+-----+-----+ | | | | S | | | | | +-----+ +-----+ +-----+ + | | | | | | | | | | | | + +-----+ + + +-----+ | | | | | | | | | + + +-----+ + +-----+ | | | | | | | | | | | | | | | | | | | | | + +-----+ +-----+ + + | | | | | F | | | | +-----+-----+-----+-----+-----+-----+
For this assignment, we'll assume that our mazes are rectangular, and that they have walls along the entire outside of the maze, with no gaps in these outer walls. We will also specify a “start square” and an “finish square” to indicate the goal of the maze-solver — to get from S to F.
Maze files will have the following structure:
<Number of columns> <Number of rows> <0-based column number of the start square> <0-based row number of the start square> <0-based column number of the finish square> <0-based row number of the finish square> <Row 0 description> <Row 1 description> ...
Each row description includes a single character for each square in that row, and each character describes the left and bottom walls for its square. Specifically:
L
means that the square has both a left wall and a bottom wall|
(vertical bar or pipe) means that the square has a left wall, but no bottom wall_
(underscore) means that the square has a bottom wall, but no left wall-
(hyphen) means that the square has neither a left wall nor a bottom wallYour program should print an error message and quit if:
L
, |
, _
, or -
L
or |
(since the left wall must be there by definition)L
or _
Putting this together in a small example, if the input file contains the following:
File | Interpretation |
3 2 0 0 2 0 L-| L__ | The maze has 3 columns and 2 rows The start square is at the upper left The finish square is at the upper right (0,0) has left and bottom walls; (0,1) has neither; (0,2) has left (1,0) has left and bottom walls; (1,1) has bottom; (1,2) has bottom |
then the resulting maze would be printed as follows:
+-----+-----+-----+ | | | | S | F | | | | +-----+ + + | | | | | | +-----+-----+-----+
Note that we specify only the left and bottom walls for each square, and not the top and right walls. This is sufficient to describe the whole maze (but why?).
The two examples above show the appropriate format of the printed output. The corner point of every maze square is a +
sign. Each pair of adjacent corners is separated by either 3 characters (vertically) or 5 characters (horizontally). If there's no wall between these corners, these characters are spaces; if there is a wall between them, these characters are either pipes (|
) or hyphens (-
), for vertical or horizontal walls, respectively. Therefore the interior of each maze square is 3x5 characters: either they're all spaces, or the center is an S
or F
and the rest are spaces.
Structure your code as shown below, stored in a file Maze.java. (Note that this is a requirement, not a suggestion.) By storing the Maze
as a 2-dimensional collection of Square
objects, you'll be well prepared to execute the maze-solving algorithm in the next assignment.
/** * A rectangular maze on a square grid. * @author <Your name here>, adapted from skeleton code by Jadrian Miles. */ public class Maze { // The 2-D list of maze squares. private List<List<Square>> rowList; //[Other instance variables if you need them] private class Square { //[Instance variables of your choosing] //[Whatever methods you need, like:] //public boolean hasTopWall() { // [...] //} } /** Default Maze constructor. */ public Maze() { //[...] } /** Loads a maze file, given its filename. */ public void load(String fileName) { //[...] } /** Prints out a graphical depiction of the maze. */ public void print() { //[... print the Maze to System.out ...] } //[... // More methods if you need them. // Remember that your methods should always be private unless // you have a very good reason to expose them publicly.] /** * This main program acts as a simple unit test for the * load-from-file and print-to-System.out Maze capabilities. */ public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage: java Maze <mazeFile>"); System.exit(1); } Maze maze = new Maze(); maze.load(args[0]); maze.print(); } }
Submit your Maze.java file on Moodle.
Start early, ask lots of questions, and have fun!
This is a partial list of the things that we'll be looking for when evaluating your work:
List
interface and accompanying “mystery” implementation.MysteryListImplementation
appears only in the initialization of List
variables, and then only on the right-hand side.This assignment originally designed by Jeff Ondich. Thanks for sharing, Jeff!