Question comes from drawing a maze.

This is a discussion on Question comes from drawing a maze. within the C Programming forums, part of the General Programming Boards category; hi all, i write this snippet of code to randomly draw a maze. when ROW is defined less then 12, ...

  1. #1
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    Question Question comes from drawing a maze.

    hi all,
    i write this snippet of code to randomly draw a maze. when ROW is defined less then 12, it works well. but when ROW >12, the result truns out to be weired.(the "wall" around the maze is broken :( )
    eg. (PS: 'O' DENOTES SPACE)
    right form:
    XXXXXXX
    XOXOXOX
    XXOXOXX
    XXXXXXX
    undesired form: (when row >12)
    XXXXXXXoXXXX
    XXOOOOOXXO
    XOOOOXXXXX
    XX OXOOXX
    OXXXOOXXXXX
    the "wall" is broken...what happened?
    it does sth. with my complier or .......plz help me
    thanks in advance!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define ROW 10
    #define COLUMN 10
    
    int main()
    {
      int draw_maze();
     
      draw_maze();
    
      getchar();
      return 0;
    }
    
    int draw_maze()
    {
      int maze[ROW][COLUMN]={0};
      int i,j;
    
      for(i=1; i<(COLUMN-1); i++) //clear inner space
        for(j=1; j<(ROW-1); j++)
          maze[i][j]=1;
    
      srand((unsigned)time(NULL));
      for(i=1; i<COLUMN-1; i++)     //randomly set blocks
        for(j=1; j<ROW-1; j++)
          maze[i][j] = rand()/(RAND_MAX/2);  // maze[i][j] = 0 or 1
    
      for(i=0; i<COLUMN; i++)
       {
         for(j=0; j<ROW; j++)
            if(maze[i][j]==0)
               printf("x");
            else
               printf(" ");
         printf("\n");
       }
    }

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I got a bunch of compile errors, so I fixed it, look here for what I edited:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define ROW 10
    #define COLUMN 10
    
    int main(void)
    {
      int draw_maze();
    
      draw_maze();
    
      getchar();
      return 0;
    }
    
    int draw_maze()
    {
      int maze[ROW][COLUMN]={{0}};
      int i,j;
    
      for(i=1; i<(COLUMN-1); i++) //clear inner space
        for(j=1; j<(ROW-1); j++)
          maze[i][j]=1;
    
      srand((unsigned)time(NULL));
      for(i=1; i<COLUMN-1; i++)     //randomly set blocks
        for(j=1; j<ROW-1; j++)
          maze[i][j] = rand()/(RAND_MAX/2);  // maze[i][j] = 0 or 1
    
      for(i=0; i<COLUMN; i++)
       {
         for(j=0; j<ROW; j++)
            if(maze[i][j]==0)
               printf("x");
            else
               printf(" ");
         printf("\n");
       }
    return 0;
    }

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I don't know what you mean by specifying row greater than 12, so I can't check that.

  4. #4
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Ah nvm, I get what you meant now, and it works fine for me when I specify Row greater than 12, so check what I fixed, doesn't your compiler give you errors when you compile?

  5. #5
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    Unhappy oh,no...

    thanks for you re-edit.
    i've tried both version in dev-c++, vc2005, and code::blocks,and i got no complier warning,
    and your's still can't perform correctly when row>15 in my machine...oh,my god

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Here is what i get when i can compile your program. The warning messages
    Code:
    gcc -Wall -W strtol.c
    strtol.c: In function &#226;draw_maze&#226;:
    strtol.c:20: warning: missing braces around initializer
    strtol.c:20: warning: (near initialization for &#226;maze[0]&#226;)
    strtol.c:41: warning: control reaches end of non-void function
    ssharish

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by albert3721 View Post
    thanks for you re-edit.
    i've tried both version in dev-c++, vc2005, and code::blocks,and i got no complier warning,
    and your's still can't perform correctly when row>15 in my machine...oh,my god

    I do not know what's happening because I set rows to over 20 and it seems to be fine, make sure you have edited it all exactly and not missed anything out.

    What command do you use for compiling, maybe you haven't invoked the warning flags.

  8. #8
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    found it!

    I made a mistake with
    Code:
    maze[ROW][COLUMN]
    it should be
    Code:
    maze[COLUMN][ROW]
    so , it shouldn't have worked well with your complier, i think.
    thanks.

  9. #9
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by albert3721 View Post
    I made a mistake with
    Code:
    maze[ROW][COLUMN]
    it should be
    Code:
    maze[COLUMN][ROW]
    so , it shouldn't have worked well with your complier, i think.
    thanks.

    You have got your columns and rows mixed up. WHen I increase rows, it's actually the columns that are increasing.

  10. #10
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    This is what the code should be, you do not need to swap the ROWS and COLUMNS in the line you indicated, but you do need to for all the other lines.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define ROW 20
    #define COLUMN 10
    
    int main(void)
    {
      int draw_maze();
    
      draw_maze();
    
      getchar();
      return 0;
    }
    
    int draw_maze()
    {
      int maze[ROW][COLUMN]={{0}};
      int i,j;
    
      for(i=1; i<(ROW-1); i++) //clear inner space
        for(j=1; j<(COLUMN-1); j++)
          maze[i][j]=1;
    
      srand((unsigned)time(NULL));
      for(i=1; i<ROW-1; i++)     //randomly set blocks
        for(j=1; j<COLUMN-1; j++)
          maze[i][j] = rand()/(RAND_MAX/2);  // maze[i][j] = 0 or 1
    
      for(i=0; i<ROW; i++)
       {
         for(j=0; j<COLUMN; j++)
            if(maze[i][j]==0)
               printf("x");
            else
               printf(" ");
         printf("\n");
       }
    return 0;
    }
    Incorrectly doing this, will probably lead to a segmentation fault.

    Now this should be all correct

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shortest Path Maze Solver (Breadth Search Help)
    By Raskalnikov in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 07:41 PM
  2. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  3. couple of questions on drawing text and buttons
    By Leeman_s in forum Windows Programming
    Replies: 3
    Last Post: 12-28-2002, 10:45 AM
  4. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 08:28 AM

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