Project 2: The Rat

Educational Objectives: After completing this assignment, the student should be able to accomplish the following:

Operational Objectives: Design and implement a simulation that receives as input a rectangular 2-dimensional maze along with "begin" and "goal" locations in the maze and produces a path through the maze from "begin" to "goal" using depth-first search.

Deliverables: Five (6) files: maze.h, maze.cpp, therat.cpp, makefile, mymaze, and log.txt.

Maze Concepts

  1. A maze is a rectangular array of square cells such that:
    1. The exterior perimeter of the rectangle is solid (impenetrable).
    2. Walls between cells may be designated as extant, meaning a barrier is in place, or non-extant, meaning passage between cells at that location is not restricted.
    3. Cells are numbered consequtively from left to right and top to bottom, beginning with cell number 0 in the upper left corner.
  2. A maze problem consists of:
    1. A maze
    2. A designated "start" cell
    3. A designated "goal" cell
  3. A path in a maze is a sequence of cell numbers such that adjacent cells in the sequence share a common face with no wall.
  4. A solution to a maze problem is a path in the maze from the start cell to the goal cell.

Maze File Syntax

A maze file is designed to describe a maze problem as defined above. The file consists of unsigned integers that may be in any format in the file but are typically arranged to mimic the actual arrangement of cells in the maze problem. Your program should read maze data from an external file, assuming the following file format:

numrows numcols
(first row of codes)
(second row of codes)
...
(last row of codes)
start goal
[optional documentation or other data may be placed here]

We will refer to such a file as a maze file. (Note that the actual format is optional, for human readability. The file is syntactically correct as long as there are the correct number of (uncorrupted) unsigned integer entries.)

Maze File Semantics

A maze file is interpreted as follows:

  1. numrows and numcols are the number of rows and columns, respectively, in the maze.

  2. Each entry in a row of codes (after numrows and numcols but before start and goal) is a decimal integer code in the range [0..15]. The code is interpreted as giving the "walls" in the cell. We will refer to this as a walls code. A specific walls code is interpreted by looking at the binary representation of the code, with 1's bit = North wall, 2's bit = East wall, 4's bit = South wall, and 8's bit = West wall. (Interpret "up" as North when drawing pictures.) A bit value of 1 means the wall exists. A bit value of 0 means the wall does not exist. For example walls code 13 means a cell with North, South, and West walls but no East wall, because (13)10 = 8 + 4 + 1 = (1101)2 (no 2's bit).

  3. Cells are numbered consequtively from left to right and top to bottom, beginning with cell 0 at the upper left. (Thus, a 4x6 maze has cells 0,1,2,...,23.) We refer to these as cell numbers. start and goal are cell numbers (NOT walls codes). They inform where the maze starting location is and where the goal lies.

  4. The optional data following goal is treated as file documentation (i.e., ignored).

Procedural Requirements

  1. The official development/testing/assessment environment is: gnu g++ 3.4.3 / Red Hat Linux / i386 . This is the environment on the linprog machines.

  2. Work within your subdirectory cop4530/proj2. The usual COP 4530 rules apply, as explained in Chapter 1 of the Lecture Notes.

  3. From the course library directory LIB, copy the following files into your project directory:

    LIB/proj2/proj2submit.sh
    LIB/proj2/maze.startHere
    LIB/proj2/therat.cpp
    LIB/proj2/mazetest.cpp
    LIB/proj2/maze?
    LIB/proj2/badmaze?
    
  4. Begin by copying maze.startHere to maze.h. Edit maze.h appropriately, at the very least by personalizing the file header.

  5. Create the files makefile, maze.cpp, and mymaze adhering to the requirements and specifications below.

  6. Turn in files maze.h, maze.cpp, therat.cpp, makefile, mymaze, and log.txt using the submit script submitscripts/proj2submit.sh.

    Warning: Submit scripts do not work on the program and linprog servers. Use shell.cs.fsu.edu to submit projects. If you do not receive the second confirmation with the contents of your project, there has been a malfunction.

Technical Requirements and Specifications

  1. Solution design. Your solution should be based on the framework distributed in the file proj2/maze.startHere. This framework consists of two classes, Maze and Cell.

    1. Interface (Maze public member functions)

      1. bool Initialize(char*)
      2. bool Consistent()
      3. void Solve()
      4. void ShowMaze(std::ostream&)

    2. Internal representation

      1. A Maze object can hold one internal maze representation, read from a maze file. Class Maze is designed to be a singleton: only one Maze object exists in a given execution environment. Use of a copy constructor and assignment operator by client is prohibited. Maze data is as follows:

      2. private: // data
          unsigned int                                     numrows_, numcols_;
          Cell *                                           start_;
          Cell *                                           goal_;
          fsu::TVector < Cell >                            cellVector_;  // cell inventory
          fsu::CStack  < Cell* , fsu::TVector < Cell* > >  conStack_;    // control stack
        
      3. numrows_ and numcols_ store the dimensions of the maze.

      4. cellVector_ is used to store actual Cell objects that make up a maze. Cell objects should not be copied.

      5. start_ and goal_ point to the start and goal cells (in the cell inventory).

      6. conStack_ is the principal control stack for the maze solver algorithm. Note that conStack_ stores pointers to Cell objects, which reside in the cell inventory.

      7. solution is the place where the final solution is returned to the client program. Note that the test clients use this list to display the solution.

      8. No data members may be added to class Maze.

      9. Class Cell is a subsidiary class for Maze, tightly coupled by granting friend status to class Maze. A Cell object represents one square cell in a 2-dimensional maze, used by Maze to represent rectangular mazes of square cells. The constructors and assignment operator of Cell are public, to facilitate storing Cell objects in container classes. All other methods and data are private, hence accessible only by the friend class Maze. Cell data is as follows:

        private:  // data
          unsigned int          id_;
          bool                  visited_;
          fsu::TList < Cell* >  neighborList_;
        

      10. id_ is the name (cell number) of the cell; do not attempt to store walls code here

      11. visited_ is a flag indicating whether a cell has been visited

      12. neighborList_ is a list of (pointers to) cells that are adjacent to this cell in the maze

      13. No data members may be added to class Cell

    3. Internal functionality

      1. bool Maze::Initialize(char*)

        Reads data from a maze file and sets up the internal representation of the maze problem. The complete internal representation of a maze problem is established, including the maze dimensions, cell inventory (with neighborlists for each cell), and start/goal. Checks the maze file syntax while reading. Returns true iff read is successful. The following should be checked as the file is read:

        1. walls codes in range [0..15]
        2. start, goal in range of cells in maze
        3. uncorrupted input of the correct number of numbers
        The outer maze boundary should be represented as solid, independent of walls codes for the boundary cells. (If a walls code for a boundary cell fails to specify a wall at a boundary face, optionally report this fact, but continue building the representation.)

      2. bool Maze::Consistent() const

        Checks the internal representation of a maze (previously established by Initialize()), detecting and reporting situations where the representation is not self-consistent. Returns true iff no inconsistencies are found. The following logic errors should be caught ([R] = required, [O] = optional):

        1. disagreement by adjacent cells on their common wall [R]
        2. tunnelling (where a magic door opens from one cell to another non-adjacent cell) [O]
        3. missing boundary wall [O]
        Note that tunnelling is impossible to represent in a maze file, however, it can result from incorrect internal handling - see badrat.x for an example. Note also that missing boundary walls should be corrected by Initialize(), but again checking here will catch errors in the implementation of Initialize().

      3. void Maze::Solve(fsu::TList<unsigned int>& solution)

        Uses depth-first-search (facilitated by conStack_) to find a solution to the internally represented maze problem. Once problem is solved, puts the solution found in the argument solution (which is provided by the client program).

      4. void Maze::ShowMaze(std::ostream&) const
        Produces a graphic of the maze using the internal representation.

      5. void Cell::AddNeighbor (Cell * N)
        Adds a (pointer to) a new neighbor cell of this cell.

      6. Cell* Cell::GetNextNeighbor () const
        Returns (pointer to) this cell's next unvisited neighbor. Returns 0 if this has no unvisited neighbors.

      7. bool Cell::IsNeighbor (const Cell * N) const
        Returns true iff N is a neighbor of this cell.

      8. Various data accessors and manipulators
        Recommended to be used for safe programming practice when implementing Maze methods.

  2. Required elements.

    1. No data members may be added to class Maze or class Cell.

    2. Walls codes may not be stored in Maze or Cell objects. Walls codes may only be stored in local variables of method Initialize().

    3. Do not use recursion.

    4. Private methods may be added to Maze as desired, but no methods should be added to Cell.

    5. A minimum functioning solution consists of methods Initialize() and Solve() operating correctly on correct maze files. (This is the 80 point level.)

    6. An advanced solution also implements method Consistent() and operates correctly on both correct maze files and improperly configured maze files. (This is the full 100 point level.)

    7. Up to 20 extra credit project points may be earned for implementation of method ShowMaze(). Note: To get the full 20 points extra credit for ShowMaze(), you have to do something more creative than is illustrated in therat.x and mazetest.x [option 4]. If you do that exactly, you can get 10 extra credit points. A 20 point solution is illustrated in mazetest.x [option 5].

    8. One maze file mymaze of your own creation is required. (That is, it should not be based on any distributed files and should not be obtained from another source). Your mymaze should be a (correctly configured) representation of a maze problem (1) with at least 6 rows and 5 columns, (2) with more than one solution, and (3) such that all solutions are at least 20 steps in length.

    9. The client program distributed as proj2/therat.cpp should be modified only by un-commenting the functionality you are implementing and submitted as part of the project. Note that this is how you signal which functionalities you are submitting.

    10. Review of deliverables:
      maze.h and maze.cpp define and implement classes Maze and Cell
      therat.cpp showcases the functionality you implement for Maze
      makefile creates an executable called therat.x using the above three files
      mymaze is a maze file of your own creation

Hints: