Thread: MAZE.. What a MAzzzzz

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    MAZE.. What a MAzzzzz

    This is the MAze problem....
    The first file goes like this ...
    Code:
    
    # include <list.h>
    # include <vector.h>
    # include <deque.h>
    # include <iostream.h>
    # include <fstream.h>
    
    class cell
    {
    public:
       // constructor
       cell(int n) : number(n), visited(false) { }
    
       // operations
       void addNeighbor(cell * n) { neighbors.push_back(n); }
       bool visit(deque<cell *> &);
    
    protected:
       int number;
       bool visited;
       list<cell *> neighbors;
    };
    
    class maze : backtrackFramework<cell *>
    {
    public:
       maze(istream &);
       void solveMaze();
    
    protected:
       cell * start;
       bool finished;
       deque <cell *> path;
    };
    
    maze::maze (istream & infile)
    // initialize maze by reading from file
    {
       int numRows, numColumns;
       int counter = 1;
       cell * current = 0;
    
       // read number of rows and columns
       infile >> numRows >> numColumns;
    
       // create vector for previous row
       cell * nothing = 0;
       vector<cell *> previousRow(numRows, nothing);
    
       // now read data values
       for (int i = 0; i < numRows; i++) {
          for (int j = 0; j < numColumns; j++) {
             current = new cell(counter++);
             int walls;
             infile >> walls;
             // make north connections
             if ((i > 0) && ((walls & 0x04) == 0)) {
                current->addNeighbor (previousRow[j]);
                previousRow[j]->addNeighbor (current);
             }
             // make west connections
             if ((j > 0) && ((walls & 0x08) == 0)) {
                current->addNeighbor (previousRow[j-1]);
                previousRow[j-1]->addNeighbor (current);
             }
             previousRow[j] = current;
          }
       }
       // most recently created cell is start of maze
       start = current;
       finished = false;
    }
    
    void maze::solveMaze ()
    // solve the maze puzzle
    {
       start->visit(path);
       while ((!finished) && (!path.empty())) {
          cell * current = path.front();
          path.pop_front();
          finished = current->visit(path);
       }
       if (!finished) {
          cout << "no solution found\n";
       }
    }
    
    bool cell::visit (deque<cell *> & path)
    // visit cell, place neighbors into queue
    // return true if solution is found
    {
       if (visited) { // already been here
          return false;
       }
       visited = true; // mark as visited
       cout << "visiting cell " << number << endl;
       if (number == 1) {
          cout << "puzzle solved\n";
          return true;
       }
    
       // put unvisited neighbors into deque
       list <cell *>::iterator start, stop;
       start = neighbors.begin();
       stop = neighbors.end();
       for (; start != stop; ++start) {
          if (!(*start)->visited) {
             // depth-first search
             path.push_front(*start);
             // breadth-first search
             //path.push_back(*start);
          }
       }
       return false;
    }
    
    
    void main ()
    {
       ifstream input("mazeone");
       maze foo(input);
       foo.solveMaze();
    }
    C++
    The best

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    error when compiling...

    errors:

    maze.cpp:33: parse error before `<'
    maze.cpp:39: parse error before `protected'
    maze.cpp:43: parse error before `}'
    maze.cpp:47: invalid use of undefined type `class maze'
    maze.cpp:33: forward declaration of `class maze'
    maze.cpp: In method `maze::maze (istream &)':
    maze.cpp:79: `start' undeclared (first use this function)
    maze.cpp:79: (Each undeclared identifier is reported only once for each
    function it appears in.)
    maze.cpp: At top level:
    maze.cpp:85: invalid use of undefined type `class maze'
    maze.cpp:33: forward declaration of `class maze'
    maze.cpp: In function `int main (...)':
    maze.cpp:130: variable `maze foo' has initializer but incomplete type
    C++
    The best

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Well, most of that is just simple parse errors....
    And some undeclarations.
    As for the rest... i dunno.
    What is C++?

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    fix the parse errors, sometimes all the other errors are just caused because of other errors.
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Sorry I forgot to post the resulte...

    Sorry guy... I forgot to post the final program,
    The progrom was very hard and it was great ... but I don't have it with me right now because I am not in CA,
    anytway, I will be back tomorrow....

    Sorry again,
    C++
    The best

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble solving maze.
    By eurus in forum C Programming
    Replies: 3
    Last Post: 02-17-2006, 01:52 AM
  2. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM
  3. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM
  4. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM
  5. Maze game, complete with maze editor and an example maze!
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-20-2002, 03:27 AM