MAZE Problem

This is a discussion on MAZE Problem within the C++ Programming forums, part of the General Programming Boards category; Hi every1 I have a code here to create a 2 dimensional array Maze with Food and Space. 1= Food ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    85

    MAZE Problem

    Hi every1

    I have a code here to create a 2 dimensional array Maze with Food and Space.
    1= Food 0 = space

    Is it possible to make it randomly generate food in the array instead of pre-positioning??

    It will be showing by web browesr. Any recommandation?
    thanks

    Thanks
    I have not included the XML tress, coz dont want to mess up the post
    thanks



    Code:
    int run;
    char filename[16];
    FILE *filep;
    
    char *nodep;
    int row, col, next_row, next_col, heading, steps, pellets;
    
    enum {NORTH, EAST, SOUTH, WEST};
    
    char matrix[MATRIX_SIZE][MATRIX_SIZE];
    char matrix_data[MATRIX_SIZE][MATRIX_SIZE] =
      { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0}
      };
    
    char trace_matrix[MATRIX_SIZE][MATRIX_SIZE];
    
    
    void output_program(struct prog *progp)
    {
      char *codep = progp->code;
      char token;
    
      while (token = *codep++)
         fputs(token_table[token].name, stdout);
      putchar('\n');
    }
    
    
    void print_matrix(void)
    {  int i, j; char symbs[] = " OX";
    
       for (i=0; i < MATRIX_SIZE; i++)
       {  for (j=0; j < MATRIX_SIZE; j++)
              putchar(symbs[trace_matrix[i][j]]);
          putchar('\n');
       }
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    it is possible using loops and rand function for example
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Thanks for your Help

    But you have some simple example?

    this was my first post and reply within 10 mins This is amazing

  4. #4
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Um, what sort of pattern do you want to use. I mean, if you just want to scatter food all you have to do is:
    Code:
    for (c = 0; c < 100; c++) matrix_data[rand()&#37;MATRIX_SIZE][rand()%MATRIX_SIZE] = 1;
    This assumes c has been declared as an int, you've already filled the array with 0s, and you've included <stdlib.h> and run srand() at some point. This also assumes you don't care about the fact that you'll actually have less than 100 foods at a given time due to overlaps and this assumes that you don't want those nice little line patterns you've got going on there.

    Now if it's a sort of maze generation you'll have to make something a little more robust.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    #include <time.h>
    #include <stdlib.h>
    
    void intimatrinx( char matrix_data[MATRIX_SIZE][MATRIX_SIZE])
    {
    	int i;
    	int j;
    	srand((unsigned int)time(NULL));
    
    	for(j=0;j<MATRIX_SIZE;j++)
    	   for(i=0;i<MATRIX_SIZE;i++)
    	   {
    		   int res = rand()&#37;5;
    		   matrix_data[i][j] = (res ==0);
    	   }
    }
    More about rand - http://www.eternallyconfuzzled.com/a..._art_rand.aspx
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    You guys reply speed is so FAST
    Thanks every1

    I will have a go now and see does it work
    Once i have test I will post back
    thanks


    Thanks

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You guys reply speed is so FAST
    Yup, this is a very active board.

    Just thought I'd add this: since you appear to be wanting paths of 1s, just generating them randomly isn't going to give you a very satisfying result. As guesst said:
    Now if it's a sort of maze generation you'll have to make something a little more robust.
    To generate "paths", consider picking a random starting position, and a length from say 5 to 50. (You could make shorter or longer paths more common if you wanted to -- more on that later.) Then pick a direction and draw 1s in that direction until you get to the length you wanted. You could turn every now and then, say on a 15&#37; chance.

    I'm not sure if that's what you're looking for so I won't explain any further.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    i saw you website which saying you produce the program with maze selector under 24 hr you must start programming long long time ago...

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Oh, that. Yes, I've been programming for quite some time . . . but it's not really a very difficult type of program to create. tmaze doesn't generate mazes, either -- they're all hard-coded. I did write another program that generated maps, but I never uploaded it. Generating maps like I think you're trying to do is a quite a bit harder.

    tmaze was designed to run on a 4MHz (that's four megahertz!) laptop, which is why it uses DOS code and stuff like that. Looking at it now, it's not very well written, actually.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    I am not trying to generate a map
    I am make a mouse to eat food place somewhere on the map.
    Not necessary a path.
    Concentrate on how it is going to choose the food

    once again
    thanks 4 quick quick reply

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Have tried both code, but didn't come up any food on the map.
    Any idea wt else do i need??
    cheers

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Quote Originally Posted by vart View Post
    Code:
    ...
    	for(j=0;j<MATRIX_SIZE;j++)
    	   for(i=0;i<MATRIX_SIZE;i++)
    	   {
    		   int res = rand()%5;
    		   matrix_data[i][j] = (res ==0);
    	   }
    ...
    Accessing array elements in column-major order.. in C? Bad Vart!

    And of course, a little more on topic:
    Quote Originally Posted by peacealida
    Any idea wt else do i need??
    Yes. Debug your code. How do you even know it isn't working? Are you sure you're printing the array properly? Are you printing the right array?

    Unfortunately, we can't debug your code for you.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Unfortunately, we can't debug your code for you.
    Well, we can, but in order to do that we need to see it.

    I am make a mouse to eat food place somewhere on the map.
    The closest food or something?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Thanks for every1's help again.

    This is meant to be genetic programming. The mouse will choose the food.
    The animation will be showing in SVG format and XML
    The program was working when it had a map like my 1st post. However after my last night's modifaction, it's stop working.

    Do i need to place 0's first and how???

    How do i add the srand()??
    Thakns Thanks

    any good site recommand for XML and SVG???
    thanks

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can fill a chunk of memory with memset(something, 0, sizeof(something)) - if something isn't an array, you need memset(&something, ...) [the rest stays the same].

    Sorry, can't help with XML or SVG.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 5 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Having trouble solving maze.
    By eurus in forum C Programming
    Replies: 3
    Last Post: 02-17-2006, 12:52 AM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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