user3587186
user3587186

Reputation: 49

Traversing a Maze

I am creating a maze game that is to be traversed and solved by the machine. I have created a maze class that contains the starting and ending positions of the maze as well as the maze itself which is contained in a 2d vector of bools. What I am getting tripped up on is how to actually code moving up and down and across the maze to get to the finish. My starting point is [11][4] in the maze and our professor has told us the best way to move about is to check all 4 locations around the current position and if its true (aka it is a path and not a wall) push it onto the stack. I understand conceptually what this means but I can't visualize how to code it properly, any help would be appreciated. FYI, there is a location struct that simplifies how to express a location.

struct Location  {
    friend std::ostream &operator <<(std::ostream &os, const Location &location) {
        os << "(" << location.x << ", " << location.y << ")";
        return os;
    }
    bool operator ==(const Location &rhs) const {return x == rhs.x && y == rhs.y;}
    bool operator !=(const Location &rhs) const {return !(*this == rhs);}
    operator bool() const {return x >= 0;}
    Location(int x=-1, int y=-1) : x(x), y(y) {}
    int x, y;
};

class Maze;

Maze load(std::string filename);

class Maze {
    friend std::ostream &operator <<(std::ostream &os, Maze &maze);
    friend Maze load(std::string filename);
  public:
    Maze(std::vector<std::vector<bool> > specifics, const Location &startPos, const           Location &endPos);
    bool solve();
    //void run();
  private:
    bool contains(const Location &location) const;
    bool isPath(const Location &location) const;
    int height() {return spec.size();}
    int width() {return spec[0].size();}
    std::vector<std::vector<bool> > spec;
    Location start, finish;
    Location current;
};


bool Maze::solve() {
    stack<Location> location; //vector to hold location objects/paths
    Location current; //will hold current position in maze
    current = start; //set current to start position in beginning
    location.push(current); //push first maze location (start) onto vector
    //cout << current;
    ///need to set current to top of stack; while traversing maze current is top of stack and if current hits (==) finish return true
    while (!location.empty()) //while location still has values inside
    {
        current=location.top();//set current to top of stack
        cout << current << endl;
        cout << spec[6][4];
        if (current==finish) //if current == finish the maze is solved
            return true;
        // for loop to start moving around... but how?        
    }
}

Upvotes: 0

Views: 1448

Answers (1)

Rakib
Rakib

Reputation: 7625

here is a pseudo code;

1. push the starting location in a stack ( you can use std::stack)
2. while end location is not reached and stack is not empty
    2.1  search all 4 surrounding locations of *top* location one by one
           if any of them is a path, push it to stack, go 2.1
           if none of them is a path, pop and remove top element from stack. go 2


3. if stack if empty and end location is not reached, maze can not be solved.

Edit

This is roughly the code (note, I did not compile it)

bool solve(Location currentLocation, Location endLocation)
{
  if(currentLocation == endLocation)
  {
    return true;
  }

  Location newLoc1(currentLocation.x-1,currentLocation.y-1); 
  if(contains(newLoc) && isPath(newLoc))
  {
    return solve(newLoc,endLocation);
  }

  Location newLoc2(currentLocation.x+1,currentLocation.y-1);
  if(contains(newLoc) && isPath(newLoc))
  {
    return solve(newLoc,endLocation);
  }

  Location newLoc3(currentLocation.x-1,currentLocation.y+1);
  if(contains(newLoc) && isPath(newLoc))
  {
    return solve(newLoc,endLocation);
  }

  Location newLoc4(currentLocation.x+1,currentLocation.y+1);
  if(contains(newLoc) && isPath(newLoc))
  {
    return solve(newLoc,endLocation);
  }

  return false;
}

You may need to add additional logic. And obviously you will go to infinite loop, since I did not checked if any cell was visited earlier before entering it. Somehow you have to remember it (may be a visited list).

Upvotes: 1

Related Questions