Thread: maze

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    maze

    Code:
    Hi im working on my maze. I want to be able to solve it. I have it generating and printing. its the solving part im having problems on. Any help or advice would be appreciated.
    
    
    
    
    #include<stdio.h>
    #include<iostream>
    #include<stdlib.h>
    #include<time.h>
    
    #define MAX 50  // 50 * 2 + 1
    #define CELL 2500  // 50 * 50
    #define WALL 1
    #define PATH 0
    
    void printpath(int maze[MAX][MAX], int maze_size);
    void init_maze(int maze[MAX][MAX]);
    void maze_generator(int direc, int maze[MAX][MAX], int goback1[CELL],int goback2[CELL], int x, int y, int n, int visited);
    void print_maze(int maze[MAX][MAX], int maze_size);
    int is_closed(int maze[MAX][MAX], int x, int y);
    
    int main()
    {
        srand((unsigned)time(NULL));
    
        int size;
        int direc = 0; /* directions start at 0 */
        printf("Input the size you would like for the amount of rows and columns to be:\n");
        scanf("%d", &size);
        printf("\n");
        int maze[MAX][MAX]; /* this sets the maze go out to MAX rows and MAX columns */
        int goback1[CELL]; 
        int goback2[CELL];
    
         /* this function initialized the maze to the fullest size */
        init_maze(maze);
    
        /* this initializes the goback1 & goback2 to 1 so when it hits wall 
    	it goes in another directions */
        goback1[direc] = 1;
        goback2[direc] = 1;
    
         /* this generates the maze using the directions, maze, backtracking 1 & 2
    	the x and y are set to 1, size of the maze, and at least 1 cell is visited */
    
        maze_generator(direc, maze, goback1,goback2, 1, 1, size, 1);
        printf("This is the maze at the first look:");
        printf("\n");
        
        /* this prints the maze */
        print_maze(maze, size); 
        printf("\n");
    
        printf("This is the maze with the path taken:");
        printf("\n");
      
        /*this prints the path from start to finish in the maze*/
        printpath(maze,size);
    
        return 0;
    }
    
    void init_maze(int maze[MAX][MAX])
    {
         for(int a = 0; a < MAX; a++)
         {
             for(int b = 0; b < MAX; b++)
             {
                 if(a % 2 == 0 || b % 2 == 0)
                     maze[a][b] = 1;
                 else
                     maze[a][b] = PATH;
             }
         }
    }
    /* this function will generate the maze given the appropriate input and starts makings the walls and paths.
     	It also will recursively call itself until solved. The generator will also use is_closed() to determine
    	if there is a wall in the way or not, then actions taken to do so if encountered */
    void maze_generator(int direc, int maze[MAX][MAX],int goback1[CELL],int goback2[CELL], int x, int y, int n, int visited)
    {
        if(visited < n * n)
        {
            int validmove = -1; // this sets the valid moves to -1 so if its invalid an error occurs
            int nextrm[4]; // this gives 4 directions for the rows
            int otherrm[4]; // this gives 4 directions for the columns
            int step[4]; // the amount of steps to be taken
    	 int count =-1;
            int x_next;
            int y_next;
    
            if(x - 2> 0 && is_closed(maze, x - 2, y))  // upside
            {   count++;
                validmove++;
                nextrm[validmove]=x - 2;
                otherrm[validmove]=y;
                step[validmove]=1;
            }
    
            if(y - 2 > 0 && is_closed(maze, x, y - 2))  // leftside
            {   count++;
                validmove++;
                nextrm[validmove]=x;
                otherrm[validmove]=y - 2;
                step[validmove]=2;
            }
    
            if(y + 2 < n *2 + 1 && is_closed(maze, x, y + 2))  // rightside
            {	count++;
                validmove++;
                nextrm[validmove]=x;
                otherrm[validmove]=y + 2;
                step[validmove]=3;
    
            }
    
            if(x + 2 < n* 2 + 1  && is_closed(maze, x + 2, y))  // downside
            {	count ++;
                validmove++;
                nextrm[validmove]=x+2;
                otherrm[validmove]=y;
                step[validmove]=4;
            }
    	
    	/* this checks if the valid move is -1 one
    	   if so, go back and try again */
            if(validmove == -1)
            {
                // backtrack
                x_next = goback1[direc];
                y_next = goback2[direc];
                direc--;
            }
    
    	/* if the valid move isn't -1, we will start randomly start
    	   putting up walls and checking for the paths */
            if(validmove !=-1)
            {
                int randomization = validmove + 1;
                int random = rand()%randomization; 
                x_next = nextrm[random]; // puts the randomization in the rows
                y_next = otherrm[random]; // puts the randomization in the columns
                direc++; // increment the directions
                goback1[direc] = x_next;
                goback2[direc] = y_next;
    
    		printf("%d",count);
    		
                int rstep = step[random];
    	
    	 /* this checks if there is a path to be taken in the maze */
                if(rstep == 1)
                    maze[x_next+1][y_next] = PATH;
                else if(rstep == 2)
                    maze[x_next][y_next + 1] = PATH;
                else if(rstep == 3)
                    maze[x_next][y_next - 1] = PATH;
                else if(rstep == 4)
                    maze[x_next - 1][y_next] = PATH;
                visited++; // this marks if it has been visited
            }
    	
    	/* recursive call for the maze to keep going until all the cells initilized
    		have been visitied until the end */
            maze_generator(direc, maze, goback1,goback2, x_next, y_next, n, visited);
        }
    }
    void solvemaze(int maze[MAX][MAX],int n,
    /* This function prints out the maze using the size indicated by the user,
    	using the + as walls and " " as a space to be taken */
    void print_maze(int maze[MAX][MAX], int maze_size)
    {
         for(int a = 0; a < maze_size *2 +1 ; a++)
         {
             for(int b = 0; b < maze_size *2 +1 ; b++)
             {
                 if(maze[a][b] == WALL)
                     printf("+");
                 else
                     printf(" ");
             }
             printf("\n");
         }
    }
    
    int is_closed(int maze[MAX][MAX], int x, int y)
    {
        if(maze[x - 1][y]  == WALL
           && maze[x][y - 1] == WALL
           && maze[x][y + 1] == WALL
           && maze[x + 1][y] == WALL
        )
            return 1;
    
        return 0;
    }
    
    
    void printpath(int maze[MAX][MAX], int maze_size)
    {
         for(int a = 0; a < maze_size*2+1 ; a++)
         {
             for(int b = 0; b < maze_size*2+1 ; b++)
             {
                 if(maze[a][b] == WALL)
                     printf("+");
                 else
    		printf(".");
             }
             printf("\n");
         }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What specifically are you having problems with?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    my problem is that im not too sure on how to solve the quickest way. I have my printpath function but all it does it put '.' in all the empty cells.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't worry about fastest. Just get something that will move from one room to the next until it hits the end. Once you know you can do that, you can worry about faster.
    Code:
    while not at exit
        look for available move next to where i am and move there
        otherwise, go back one space to where i just was
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Solving A Maze Using C Language!!!!
    By jonnybgood in forum C Programming
    Replies: 6
    Last Post: 11-08-2005, 12:15 PM
  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