CS1027: Assignment 2


5/5 - (2 votes)

CS1027: Assignment 2

To gain experience with
 Stacks
 Working with complex classes
 Exceptions
Mazes can be solved by computers using a variety of algorithms. A simple approach is to start at the beginning and search
through all open spaces one at a time until the end is found. You will create a program that searches for the end of a maze
made up of hexagonal tiles using a stack to keep track of tiles yet to be checked. Here is a link to an incomplete example:

View post on

Functional Specifications:
For this assignment you are given a number of classes to use, and you will also use some of the classes from Lecture. You
will create a class MazeSolver that has a main method only, which uses these classes to implement the maze solving
algorithm given below.
Classes from Lecture/Website you will need:
Provided Classes for this Assignment:
 – A class representing a Maze made up of Hexagon tiles. Opens in a graphical window.
 – A class representing the Hexagon tiles in a Maze window.
 – A superclass of Hexagon representing the graphical element
 – A class allowing the Maze to lay out the Hexagon tiles correctly
 – An Exception to do with reading in the Maze from a file.
 – An Exception to do with accessing the neighbor of a Hexagon
that cannot exist.
The main classes you will be working with are Maze and Hexagon. The Maze class is initialized with a file representing the
layout of the maze: maze0.txt, maze1.txt, maze2.txt, maze3.txt, maze4.txt
The API for the provided classes will orient you to their methods. Look for the javadocs. In other words, BEFORE YOU START,
familiarize yourself with the classes, specifically the Maze and Hexagon classes. Be sure to read the description of the class
near the top of the page and the available methods provided by the class.
Each Hexagon tile can be one of a Wall (black), Start (green), End (yellow) or Unvisited (cyan) when we build the maze at
the beginning, and as we visit tiles during they solving process, they can become pushed (magenta) or processed (blue). The
tiles will display in different colours so you can track your MazeSolver progress. The start tile will turn pink when it is
processed and the end tile will turn red when it is found (processed).
High Level Algorithm for MazeSolver:
 Create maze object
 Try to open maze file
 Reference starting Hexagon from the maze
 Create Stack and push starting Hexagon
 Make sure bookkeeping variables are created (stepCounter and hexagonOnStack counter)
 While stack is not empty
o Pop
o Increase step counter, decrease hex counter
o If Hexagon is end tile, we need to do special stuff
o Otherwise
 For each of the six possible neighbours of current Hexagon
 If neighbour exists, is not a wall, was not visited
o Push neighbor on the stack and do bookkeeping (counters)
o Set current Hexagon as processed
o Update maze window with repaint()
 Once we are done
o Say if we found the end
o Number of steps that it took to finish
o Number of tiles still on the stack
You must run your code on all 5, however, you will notice that maze1 and maze3 have interesting things happening. Write a
brief description in a separate .txt file describing the peculiarities in these two mazes. This can be in point form, and very
brief, however it does need to be readable/understandable.
Your code must correctly handle thrown exceptions. To handle the exceptions you need only to inform the user through a
console print statement what specifically has happened. These handlers must be specific (rather than one catch block for
Exception itself).
Command Line Argument
You must read the Maze file from the command line. The command line is what computers used to use to run programs
rather than having graphical windows, and all systems still have the functionality available (like through the Terminal
application on Macs, or the Command Prompt (cmd) application on Windows). The user could run the MazeSolver program
with the following command from a command line (for example): java MazeSolver maze1.txt
If you have ever wondered what the “String[] args” thing meant in the main method header, this is what it is for; It
allows us to read in the text supplied on the command line. The args array of Strings will store any of the text supplied by
the user, that is, any tokens following the application name (MazeSolver in the above example).
To read a single argument, we look in args[0], but first we want to check that the user has entered something. The following
code example could be the beginning of your file. It will check the length of the args array and thrown an
exception if there is not an argument provided. Then it will store a reference to the String using the reference variable
public class MazeSolver {
public static void main (String[] args) {
throw new IllegalArgumentException(“No Maze Provided”);
String mazeFileName = args[0];
Setting up a Command Line Argument when running your program in Eclipse
To get Eclipse to supply a command line argument to your program when it runs, you will need to modify the “Run
Configurations”. On my computer it’s located in “Run”->”Run Configurations”. Something like the following should pop
Be sure the “Java Application->MazeSolver” is the active selection on the left-hand side.
Select the “Arguments” tab.
Enter the filename and location in the “Program arguments” text box. In my case I have it in
“C:\Users\Jason\CS1027\fall2017\assignment2\maze0.txt”, however you may have it somewhere completely different and
that is to be expected.
If you are on Mac/Linux you will likely have something like “/Users/SomeName/Desktop/Ass2/maze2.txt”.
Code similarities are both violations of Academic Dishonesty for those sharing their code as for those
using other’s code. As was explained in class, never use or share code! If you wish to help another
student, explain concepts in English, draw diagrams, or refer the student to specific reference material.
Similarity detection software will be used on all assignments. Any students who achieve a high score for
improbable code similarities will receive a mark of 0. In addition, further Academic Dishonesty reporting
may be performed.
o Do NOT zip your files, or submit any additional files! You will lose marks!
 A .txt file with your brief analysis for maze1 and maze3.
 Q: I don’t know where to start.
o A: Did you read the whole assignment details?
 Q: I still don’t know where to start.
o A: Go over the high level description of the algorithm. It really does go into the details of solving the
problem. You should also make sure you’re comfortable with the provided classes – read the
documentation provided.
 Q: Does my .txt give enough details?
o A: Probably, it’s supposed to be brief.
 Q: Should I comment X?
o A: Probably.
 Q: There is a bug in my code!
o A: Look at the debugging lecture slides. There are a lot of tips in there.
 Q: Did I catch enough exceptions?
o A: No idea. Keep trying to figure out how to break your code. Make sure you have at least 4 being dealt
 Q: Can I change the specifications, even if it’s to make it ‘better’?
o A: NO! Not even if you invent the best maze solver in the world.
 Q: When catching exceptions, does order matter?
o A: Yes, it does actually. Remember subclasses are also the same type of parent classes.
 Q: I swear I did everything right, but for some reason my files won’t open!
o A: This isn’t uncommon.
 If you’re using eclipse, try putting the .txt files in the project directory (the same folder
containing the bin folder, src folder.
 If you’re running from command line, try putting the files in the src folder.
 Q: I don’t know how to do X?
o A: Try going to my favorite website: and then typing X into the big text box.
 Q: Can I email the TA or professor with questions?
o A: Yes, but you should really check out my favorite website (above).
Non-functional Specifications:
1. Include brief comments in your code identifying yourself, describing the program, and describing key portions of the
2. Assignments are to be done individually and must be your own work. Software may be used to detect cheating.
3. Use Java coding conventions and good programming techniques, for example:
i. Meaningful variable names
ii. Conventions for naming variables and constants
iii. Use of constants where appropriate
iv. Readability: indentation, white space, consistency
v. private vs public
Make sure you attach your files to your assignment; DO NOT put the code inline in the textbox. DO NOT SUBMIT
What You Will Be Marked On:
1. Functional specifications:
 Does the program behave according to specifications?
 Does it run with the main program provided?
 Are your classes created properly?
 Are you using appropriate data structures?
 Is the output according to specifications?
2. Non-functional specifications: as described above
3. Assignment submission: via OWL assignment submission

PlaceholderCS1027: Assignment 2
Open chat
Need help?
Can we help?