Search code examples
c++maze

Check if the position is a valid cell in a maze


I am trying to make a maze. The walls consist of '#' and I want to make a path from top left to bottom right using '.'. I have come this far, However when I run it now I get a segmentation fault and nothing happens. Anyone know what I am doing wrong? I think my problems lie with my recursive function. Be my guest to edit it! Thanks in advance! :D

#include <iostream>
#include <vector>
#include <stack>
#include <sstream>
#include <time.h>

#define North 0
#define East 1
#define South 2
#define West 3

class Maze {
    private:
        int mazeHeight;
        int mazeWidth;
        int seedValue;
        std::vector <std::vector <char>> Maze;
    public:
        void checkuserInput(int Input1, int Input2);
        void mazeConstructor(int x, int y, int z, std::vector <std::vector <char>> vect);
        std::vector <std::vector <char>> initializeMaze();
};

class Path {
    private:
        std::vector <std::vector <char>> Grid;
        bool visited;
        int Height;
        int Width;
    public:
        void pathConstructor(std::vector <std::vector <char>> Maze, int mazeHeight, int mazeWidth);
        bool checkValid(int yPos, int xPos);
        void findPath(int yPos, int xPos);
        void printMaze();
};

// Check if user input is valid
void checkUserInput(int Input1, int Input2) {
    int Height;
    int Width;
    if (!(Input1 >> Height)) {
        throw std::runtime_error ("Invalid input");
    }
    if (!(Input2 >> Height)) {
        throw std::runtime_error ("Invalid input");
    }
} 
//Make the variables accesible
void Maze::mazeConstructor(int x, int y, int z, std::vector <std::vector <char>> vect) {
    mazeHeight = x;
    mazeWidth = y;
    seedValue = z;
    Maze = vect;
}
// Initialize the outer walls with '#'
std::vector <std::vector <char>> Maze::initializeMaze() {
    for (int i = 0; i < mazeWidth; i++) {
        for (int j = 0; j < mazeHeight; j++) {
            Maze[i][j] = '#';
        }
    }
    return Maze;
}
// Make the variables accessible
void Path::pathConstructor(std::vector <std::vector <char>> Maze, int mazeHeight, int mazeWidth) {
    Grid = Maze;
    Height = mazeHeight;
    Width = mazeWidth;
}

bool Path::checkValid(int yPos, int xPos) {
  if(xPos >= Width || xPos < 0) {
      return false;
  } 
  if(yPos >= Height || yPos < 0) {
      return false;
  }
  if (Grid[xPos][yPos] == '#') {
     return false;
  }
  return true;
} 

// Find a path using recursion
void Path::findPath(int yPos, int xPos) {
    if (yPos == Height || xPos == Width) {
        printMaze();
    }
    else {
        Grid[yPos][xPos] = '.';
        int randomNumber = rand() % 3; 
        switch (randomNumber) {
            case South:
                if (checkValid(yPos, xPos)) {
                findPath(yPos + 1, xPos);
                }
                else {
                    findPath(yPos, xPos);
                }
            case East:
                if (checkValid(yPos, xPos)) {
                findPath(yPos, xPos + 1);
                }
                else {
                    findPath(yPos, xPos);
                }
            case North:
                if (checkValid(yPos, xPos)) {
                findPath(yPos - 1, xPos); 
                }
                else {
                    findPath(yPos, xPos);
                }
            case West:
                if (checkValid(yPos, xPos)) {
                findPath(yPos, xPos - 1); 
                }
                else {
                    findPath(yPos, xPos);
                } 
        }   
    }
} 

// Output the maze
void Path::printMaze() {
    for (int i = 0; i < Grid.size(); i++) {
        for (int j = 0; j < Grid[0].size(); j++) {
            std::cout << Grid[i][j];
        }
        std::cout << std::endl;
    }
}

// Get command line arguments 
int main(int argc, char* argv[]) {
    Maze maze;
    Path Path;
    srand (time(0));
    int Height;
    int Width;
    int seedValue;
    Height = atoi(argv[2]);
    Width = atoi(argv[2]);
    try {
        checkUserInput(Height, Width);
    }
    catch(std::runtime_error& e) {
        std::cerr << e.what() << std::endl;
        return 1;
    }
    if (argc > 3) {
        seedValue = atoi(argv[3]);
    } else {
        seedValue = rand();
    }
    std::vector <std::vector <char>> Maze (Height * 3 + 1, std::vector <char> (Width * 5 + 1, ' '));
    maze.mazeConstructor(Height, Width, seedValue, Maze);
    Path.pathConstructor(maze.initializeMaze(), Height, Width);
    Path.printMaze();
    Path.findPath(1, 1);
}```

Solution

  • The square will be invalid if

    1. Its x,y position is outside the maze.
    2. Its x,y position is a brick
    bool Path::checkValid(int yPos, int xPos) {
      if(xPos >= mazeWidth || xPos < 0){
          return false;
      } 
    
      if(yPos >= mazeHeight || yPos < 0){
          return false;
      }
    
      if(Maze[xPos][yPos] == '#'){
         return false;
      }
      return true;
    }