maze generator

This is a discussion on maze generator within the C Programming forums, part of the General Programming Boards category; okay first let me post the code... Code: #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 15 #define COL 50 ...

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    maze generator

    okay first let me post the code...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define ROW 15
    #define COL 50
    
    void creatmaze (int [][COL]);
    void printarea(int [][COL]);
    
    int main(void)
    {
     int maze[ROW][COL]={0};
     srand(time(0));
    
     creatmaze(maze);
     printarea(maze);
    
    
    
          system("PAUSE");
          return 0;
    }
    void creatmaze ( int maze[][COL] )
    {
         int cnt_main, cnt_sub, cnt;
         int row, col, movement,cnt_stop;
    
         for (  cnt_main = 0; cnt_main <= COL-1; cnt_main+=COL-1 ){
             for ( cnt_sub = 0; cnt_sub <= ROW-1; cnt_sub++ )
                 maze[cnt_sub][cnt_main] = 3;
         }
    
         for ( cnt_main = 0; cnt_main <= ROW-1; cnt_main+=ROW-1 ){
             for ( cnt_sub = 0; cnt_sub <= COL-1; cnt_sub++ )
                 maze[cnt_main][cnt_sub] = 3;
         }
    
         maze[3][0] = -2;  /*starting point*/
         maze[8][49] = -1;  /*Exit point */
    
         cnt_main = 0;
         while ( ++cnt_main != 400 ){
               col = rand() / (RAND_MAX /COL-1)+1;
               row = rand() / (RAND_MAX / ROW-1)+1;
    
               if ( maze[row][col] < 0 || maze[row][col] == 3)
                  ;
               else
                   maze[row][col] = 1;
        }
        /* My Fake AI **** */
        /*Gotta fin out how to make it work ;) */
    
    
    }
    void printarea(int maze[][COL])
    {
     int cnt_main, cnt_sub;
    
     for ( cnt_main = 0; cnt_main < ROW; cnt_main++ ){
         for ( cnt_sub = 0; cnt_sub < COL; cnt_sub++ ){
             if ( maze[cnt_main][cnt_sub] == 3 )
                putchar(178);
             else if ( maze[cnt_main][cnt_sub] == 1 )
                putchar(177);
             else if ( maze[cnt_main][cnt_sub] == -2 )
                  putchar(2);
             else if ( maze[cnt_main][cnt_sub] == 0 )
                  putchar(255);
             else
                 putchar(6);
    
         }
         printf("\n");
     }
    }
    Okay the first problem is the one that is important well exactly it isnt a problem in the code ...what i wanna try is after randomly puts the walls in the area ...i want to simulate the path like creat a path that starts from the begenning and ends at the exit point...i have tried doing it but i cant get the right code to apply

    2) I execute the same stuff on a linux box and instead of giving me the oem character like the walls smilly face and the spade it gave me ansi characters but on a windows it gives me a oem character ...why and how can i change that ???

    3) The programm some times crashes in the middle of the program or after pressing enter .In linux the gcc compiler produces an error like segementation fault ,....in windows yeah it gives the same stuff ....

    4) Help me
    Last edited by datainjector; 11-20-2002 at 04:59 PM.
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>#define COL 50
    >>col = rand() / (RAND_MAX /COL-1)+1;
    >>maze[row][col] = 1;
    Your calculation on the rand() line means that the number assigned to col could be 50, which when used to access the array, would go out of its bounds.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    hey thanks hammer
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  2. Random maze generator
    By Bingo The Clown in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2003, 10:15 AM
  3. mission notimpossible (maze gen)
    By datainjector in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 05:51 PM
  4. IDEA: Maze Generator
    By ygfperson in forum Contests Board
    Replies: 2
    Last Post: 09-09-2002, 09:43 PM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-25-2002, 12:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21