In this assigment you will write a program that finds a solution to a maze. Your program will build on your stack ADT that you worked on in class. The main difference is that the nodes of your underlying linked-list will be squares corresponding to a maze (as opposed to strings).
A maze here is a puzzle that you may have solved with a paper and pencil before. The maze has a start location and an end location, and the objective is to find a path between these two locations without hitting any walls.
I provide you with a lot of starter code mazeSolver.tar.gz, which I recommend you review thoroughly before trying to add any of your own code.
Your program should use an argument to main to specify the name of the file to read from. Review a past lab if you’ve forgotten how to use command line arguments. Then you will need to open and read the file, review the relevant lab on files if needed.
Here is an example of how to run the program with the starter code:
$ make
$ ./mazeSolver maze1.txt
The first line of a maze file will be the dimensions of the maze: two positive integers, representing the number of rows and number of columns, respectively. The next line contains the location of the start square. That is, two numbers (0-indexed). The next line contains the location of the finish square. Finally, the maze is given, each row on a new line. For each row, there will be a character which describes whether or not the right and top walls exist in that square.
7 means that there is a top wall and a right wall| (vertical bar) means that there is a right wall_ (underscore) means that there is a top wall* (asterisk) means there is no top wall or right wallHere are example contents from a maze file:
2 3
1 1
0 2
__7
*_7
This file has the following interpretation: There are 2 rows and 3 columns. The start position of the maze is at the square (1,1) (bottom middle of the maze). The finish position of the maze is at position (0,2) (upper right of the maze). The first row: (0,0) has a top wall, (0,1) has a top wall, (0,2) has a top and right wall. For the second row: (1,0) has neither top or right walls, (1,1) has a top wall, (1,2) has a top and right wall.
A printed version of this maze with the start and finish squares marked looks like this:
+-----+-----+-----+
| |
| F |
| |
+ +-----+-----+
| |
| S |
| |
+-----+-----+-----+
You may assume that the input file contains the maze information in the correct format.
If you haven’t already, take a minute to think about why a stack would be a good ADT for the problem of simulating a human solving such a maze.
Using a stack, your solution will have the following outline:
Your program should print the solution to the maze with each square of the maze that is on the solution path marked with an asterisk. The starter code includes utilities for printing.
+-----+-----+-----+
| |
| * * * |
| |
+ +-----+-----+
| |
| * * |
| |
+-----+-----+-----+
If there is no solution to the maze, then your program should print a message to that effect. Also print the (empty) maze, for a visual check that there is no solution.
Note: The included printing functions may not always work perfectly. I didn’t have as much time to test these as I wanted. But, they should be able to approximately show you the solution.
This program will likely be more complex than those we’ve written before. You are required to carefully consider the organization and style of your finished program before turing it in. You should decompose your program into smaller functions for readability and maintainability. You should also carefully group functions based on their use.
In the provided starter code, I create a one-dimensional array to keep track of the squares of the maze, even though we mentally consider it to be two-dimensional. The reason for this is to avoid some extra crazy pointer situations, but nevertheless it introduces a new complexity.
Consider a 1D array of characters:
hello!
In this representation we can think of the 0th spot of the array holding h, while the 1st spot holds e, etc.
“Transforming” this data into a 2D array, we would have this arrangement of data:
hel
lo!
While the 1D array was of dimension 1x6, this new 2D array has dimension 2x3. The thing we want to be able to do, is correspond between index 1 in the 1D array (which remember holds e), and the (row,column) location in the 2D array (0,1). And possibly we might want to think about the reverse: given a (row,column) location, give the index in the 2D array.
When referencing the data of the grid, you will need to use the 1D indexing to access the relevant squares. However, each individual square will hold it’s corresponding row and column in the large maze. So you might already be able to see why we need to be able to quickly move between 1D and 2D.
If you want, you can think about how such functions would work. However, I provided them for you in the starter code.
You must create 3 additional mazes to test your program on. One should be named unsolvable.txt and there should be no solution. Your others should be named maze2.txt and maze3.txt. These last two mazes should test your program in unique ways (such as requiring that the solution move right or down). You may make more examples, but at least 2 solvable mazes are required.
Your program does not need to find the shortest path, it just needs to find a solution if there is one.
Your submitted files must compile under the command make, and run with command ./mazeSolver maze1.txt.
Your assignment will be graded on a scale of 0–100 points based on the following criteria:
unsolvable.txt, maze2.txt, and maze3.txt) and have solutions as specificed. (15 points)There will be potential deductions for any code quality violations. In addition to the requirements about decomposition listed above, there are additional requirements for quality code:
if–else block that explains what the purpose of that code is. The comment should not simply restate the code; it should add information for a human who reads the code and is confused.This problem is adapted from Anya Vostinar at Carleton College, maze homework.