MAZE Problem

This is a discussion on MAZE Problem within the C++ Programming forums, part of the General Programming Boards category; DWK~~Thx so much Have reduced the code replaced by yours haha.. I am so stupid ~ ~ Anyway can you ...

  1. #31
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    DWK~~Thx so much

    Have reduced the code replaced by yours haha.. I am so stupid ~ ~

    Anyway can you give me a little bit more hint how to place the 1's??
    thanks thanks

  2. #32
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    still getting error and i am not sure is this correct as well

    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};
    
    ////create map 32 X 32 with all zeros////
    char matrix[MATRIX_SIZE][MATRIX_SIZE];
    char matrix_data[MATRIX_SIZE][MATRIX_SIZE] = {{0}};
    ////created the map with zeros////
    
    ///////////////////////////now fill 1's into the field/////////////////////////
    
    ///////////////////////////////////////////////////////////////////////// 
    
    
    
    ///////////test/////////
    int main()  {
        int x;
        int c;
        srand(()%1024);
    
        for(x=0; x<100; x++) {
            int n = 100 + rand() % 300;
            printf("%d\n", 1);
            
        for (c = 0; c < 300; c++) matrix_data[rand()%MATRIX_SIZE][rand()%MATRIX_SIZE] = 1;
            
        }
    }
    
    /////////////////////////
    
    
    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');
       }
    }
    can any1 can tell m what's gone wrong????

  3. #33
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Hi the code is working now.

    Code:
    ////create array map 32 X 32 (1024 steps) with all zeros and trace map////
    int matrix[MATRIX_SIZE][MATRIX_SIZE];
    int matrix_data[MATRIX_SIZE][MATRIX_SIZE] = {{0}};
    int trace_matrix[MATRIX_SIZE][MATRIX_SIZE];
    
    ///////////////////////////////////////////char to int//////////////
    void output_program(struct prog *progp)
    {
      char *codep = progp->code;
      int token;
    
      while (token = *codep++)
         fputs(token_table[token].name, stdout);
      putchar('\n');
    }
    
    int main()
    {
      int i; time_t start;
    
      seed = time(NULL);
      time(&start);

    Now the program produce a random map. But how can i ensure food do not overlap??

    In addition, is it able to create 1 random map and 2 maps to allow user to choose? and how can I implement it?
    thanks

  4. #34
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by peacealida View Post
    Hi the code is working now.

    \Now the program produce a random map. But how can i ensure food do not overlap??
    Watch carefully; there may be a test afterwards:

    Code:
    for (c = 0; c < 300; c++) {
        int x = rand()%MATRIX_SIZE;
        int y = rand()%MATRIX_SIZE; //our x- and y-coordinates
        matrix_data[x][y]++; // add 1 to our randomly chosen spot
        if (matrix_data[x][y] > 1) { // oops!  too much!
            matrix_data[x][y] = 1; //reset food
            c--; //take away from the count of food
        }
    }

  5. #35
    Registered User
    Join Date
    Mar 2008
    Posts
    85

    Thumbs up

    I cant belive you ability so fast......
    It runs perfet!!!!

    You know the array I have creted... can you hint me why it kept error pleasE??
    Code:
    int matrix[MATRIX_SIZE][MATRIX_SIZE];
    int matrix_data[4][MATRIX_SIZE][MATRIX_SIZE];
    {    
    
        cout <<"Ender Maze wanted:";
        cin >> x;
        cout <<"Maze Choosen is: " << x << endl <<endl;
    
    
        matrix_data[0][MATRIX_SIZE][MATRIX_SIZE] = {{0}};
        matrix_data[1][MATRIX_SIZE][MATRIX_SIZE]= {{1}};
      //  //--------------------------------------------------------------//
      //  //--------------------------------------------------------------//
        return matrix_data[0];
    }
    int trace_matrix[MATRIX_SIZE][MATRIX_SIZE];

    in the main

    Code:
    int main()
    {
        int n;
        for (n=0; n<4; n++);
            //result += matrix_data[n];
        printf("%d\n", matrix_data[n]);
        //std::cout<< matrix_data <<std::enl;
        //cout << result
        
        return 0;
    I think I am completely wrong in the int main()
    wt statement i need to read the array and put it back?

    Thanks for you Great Help X 10000000000

  6. #36
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can only use an array initializer when you declare it, so this line:
    Code:
    matrix_data[0][MATRIX_SIZE][MATRIX_SIZE] = {{0}};
    (and the one after it) are errors.

    To initialize a matrix, you need to run a double for-loop. So, for instance, to initialize matrix_data[0], we would run a for-loop on row, and inside that a for-loop on col, and inside that we would set matrix_data[0][row][col].

    Alternatively, you could do one big initializer as so:
    Code:
    matrix_data[4][MATRIX_SIZE][MATRIX_SIZE] = {
    {{0}}, //this is matrix_data[0], the random one
    {{0,1,1,0,1,1,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0}, //this is first row of [1]
     { ...stuff ...}, //the second row of [1]
     { et cetera },
     { and so on}} //end of [1]
    {{more numbers}, //first row of [2]
    .
    .
    .
    It will be big, but if you've got them already you can copy'n'paste.

  7. #37
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    These are what I have mdify:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h> 
    
    
    
    
    ////create array map 32 X 32 (1024 steps) with all zeros and trace map////
    
    int matrix[MATRIX_SIZE][MATRIX_SIZE];
    int matrix_data[2][MATRIX_SIZE][MATRIX_SIZE];
    {
        cout <<"Ender Maze wanted, 0,1,2,3:\n";
        cout <<"    0- Random Maze\n";
        cout <<"    1- Set Maze (1)\n";
        cout <<"    2- Exit\n";
        cin >> x;
        cout <<"Maze Choosen is: " << x << endl <<endl;
        
        matrix_data[0][MATRIX_SIZE][MATRIX_SIZE] = {{0}};
        matrix_data[1][MATRIX_SIZE][MATRIX_SIZE] =
      { {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,1},
        {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,1,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,1,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,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,1,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,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,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,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,1,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,1,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,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,1,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,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,1,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,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {1,1,1,1,1,1,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,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,1,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,1,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,1,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,1,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,1,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,1,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,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,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,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,1,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,1,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,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,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,1,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,1,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,1}
      };
    
        return matrix_data[0];
    }
    int trace_matrix[MATRIX_SIZE][MATRIX_SIZE];
    
    
    
    ///////////////////////////////////////////char to int//////////////
    void output_program(struct prog *progp)
    {
      char *codep = progp->code;
      int 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');
       }
    }
    Like you said it's gonna be huge if 3-4 maps existed. So ihave only include 1 random map and 1 pre-defined map.

    But where do i use the double- for loop?? and what should i include in the main?

    What i am currently in the main isn't very usefulllll

    Code:
    int main()
    {
        int n;
        for (n=0; n<4; n++);
            //result += matrix_data[n];
        printf("&#37;d\n", matrix_data[n]);
        std::cout<< matrix_data <<std::enl;
        //cout << result
        
        return 0;





    ---------------------------[thanks for the Help]--------------------------------

  8. #38
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
        matrix_data[1][MATRIX_SIZE][MATRIX_SIZE] =
      { {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,1},
        {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,1,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,1,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,0,0,0},
      ....
       };
    Won't compile - it's not possible to initialiize the variable once it's been declared - it has to be on this line:
    Code:
    int matrix_data[2][MATRIX_SIZE][MATRIX_SIZE];
    Edit: and of course you need to initialize both at the same time.
    --
    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.

  9. #39
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by peacealida View Post
    Like you said it's gonna be huge if 3-4 maps existed. So ihave only include 1 random map and 1 pre-defined map.

    But where do i use the double- for loop?? and what should i include in the main?

    What i am currently in the main isn't very usefulllll
    ---------------------------[thanks for the Help]--------------------------------
    So, wherever you set up the random matrix, that's where you set up the other pre-defined matrix as well.

  10. #40
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Hi Mats I am sorry can you explain further what do i need to change??

    you mean i have to change from:

    matrix_data[1][MATRIX_SIZE][MATRIX_SIZE] =
    { {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,1},
    {0,1,.....
    [/code]

    to

    Code:
    int matrix[MATRIX_SIZE][MATRIX_SIZE];
    int matrix_data[2][MATRIX_SIZE][MATRIX_SIZE];
        cout <<"Ender Maze wanted, 0,1,2,3:\n";
        cout <<"    0- Random Maze\n";
        cout <<"    1- Set Maze (1)\n";
        cout <<"    2- Exit\n";
        cin >> x;
        cout <<"Maze Choosen is: " << x << endl <<endl;
        
        matrix_data[0][MATRIX_SIZE][MATRIX_SIZE] = {{0}};
        matrix_data[1][MATRIX_SIZE][MATRIX_SIZE] =
      { {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,1},
        {0,1,0,0,0,0

    or sth else???? Cheers



    [code]

  11. #41
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    tapstop ic

    So i should put the pre-define matrix inside the
    int main()?

  12. #42
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I mean "something else".

    You have variable declaration (int matrix_data[...][...][...], and then you attempt to assign this variable using an initializer list somewhere else. That is not valid code.

    Simple example:
    Code:
    // Valid code:
    int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    // Not valid code
    int arr[10];
    
    arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    --
    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.

  13. #43
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    This is the problem; coz i am try to store differetn 32 X 32 matrix.
    therefore in my array have 2 maps.

    I can't not just put 1 number.
    So how can i resolve this???

    or like this?

    Code:
    int matrix_data[2][MATRIX_SIZE][MATRIX_SIZE]; = { {{0}}, {{1,1,1,1,1,0,0,0,0,0,0,},{1,1,1,0,0,0,0,1,1,0}};}










    ---------------------------[thanks for the Help]--------------------------------

  14. #44
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by peacealida View Post
    Code:
    int matrix_data[2][MATRIX_SIZE][MATRIX_SIZE];
    This is your declaration. The only only only only place you can do an initializer is here. Only here. Anywhere else, and you'll have to access the array elements one-by-one.

  15. #45
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by peacealida View Post
    Code:
    int matrix_data[2][MATRIX_SIZE][MATRIX_SIZE]; = { {{0}}, {{1,1,1,1,1,0,0,0,0,0,0,},{1,1,1,0,0,0,0,1,1,0}};}
    This is the one you want. You have to initialize the whole thing in one shot with an initializer list like this.

Page 3 of 5 FirstFirst 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, 06:24 PM
  2. Having trouble solving maze.
    By eurus in forum C Programming
    Replies: 3
    Last Post: 02-17-2006, 01:52 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 02:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM

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