Maze part II - Solve a maze

Objectives

  • Convert a two-dimensional maze into a graph.
  • Modify graph code to display desired information.
  • Find the shortest path through a maze using a graph algorithm.

Assignment

This assignment uses the maze creation program you have created in a previous assignment and the graph code given in class to create and then solve mazes. The code for the graph class is available via links later in this document and on the server in the directory: ~dklick/examples/cis250/graph. You can use the graph class with only one small modification to the shortestPath function.

The first step will be to convert the maze you create into a graph. The cells will be the vertices of the graph, and the connections between the vertices/cells will be the edges of the graph. Once the maze is turned into graph data, you just ask the graph for the shortest route from the start cell to the finish cell.

Requirements

  1. Create a new graph object.
  2. For each cell in the board
    1. If the cell is not in the first row and it does not have a wall on its north side, then add an edge to the graph that represents the connection between this cell and the cell to its north.
    2. If the cell is not in the first column and it does not have a wall on its west side, then add an edge to the graph that represents the connection between this cell and the cell to its west.
  3. The Board class has methods for getting the start and end cells. Get the start and end cells, and send their IDs to the graph's shortest path method. You should get back a String. Print it.
  4. The shortestPath function in the Graph class outputs internal vertex numbering. Change it to output the vertex name instead. The spot you want to make the modification is near the very end of the shortestPath function. There is a for loop which is printing the vertex numbers. All you need to do is take the number it wants to display and use that number as an index into the vertex list to display the vertex name instead.

Notes

  1. You can create a formatted string in a memory buffer using a stringstream
  2. To use a stringstream: #include <sstream>
  3. To use a stringstream: using std::stringstream;
  4. To create a stringstream: stringstream buf;
  5. Output to a stringstream: buf << x << " * " << y << " is " << (x * y) << "\n";
  6. Convert a stringstream to a string: buf.str()
  7. Clear a stringstream: buf.str("");
  8. You can create a string object from an array of chars as follows: string newString(charArray);
  9. You can create a C-style string from a C++ string object using the .c_str() method/function of the string object.

Graph resources

Sample output

$ ./msolve 10 10 All cells on board visited: true All boundaries correct: true All cells reachable: true _ _ _ _ _ _ _ _ _ | | | |_ | |_| |_ _|_ _|_ | | | | _| | | _| | | | _ _| | |_ _|_ _| | |_ | _| | _ _ _ | | | | |_ _ _| | | | | _|_ _ _ | | | | | |_ _ |_ |_ _| | | | | | | _| _ _| _| |_|_ _ _|_ _|_ _ _ | The shortest path from R00C00 to R09C09 is: R00C00 R00C01 R00C02 R01C02 R01C03 R00C03 R00C04 R01C04 R01C05 R00C05 R00C06 R01C06 R01C07 R02C07 R02C06 R03C06 R03C05 R02C05 R02C04 R03C04 R04C04 R05C04 R05C05 R04C05 R04C06 R04C07 R04C08 R04C09 R05C09 R06C09 R07C09 R08C09 R08C08 R09C08 R09C09

Rubric

  • Program must compile and run to receive any points.
  • Points may be deducted for improper style, missing documentation comments, etc.
  • (34 pts) Maze properly converted to graph
  • (8 pts) Each cell has a unique, intuitive ID
  • (8 pts) Shortest path modification made to print out cell names/IDs.