Thread: Large 1D array -> Grid-like 2D array. HOW?

  1. #16
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    Yes, the map is 29x19 (29 over, 19 down). Using your method, it didnt perfect it (yet it made it look better, as if it took away one of the 2 characters causing the extra step like look). So, ive made it something like:
    Code:
         for(i=0; i<19; i++) {
                           for (z=0; z<30; z++) {
                                             if (map[j] != '\n' || map[j] != '\0') {
                                                         level[i][z] = map[j];
                                             }
                                             j++;
                           }
         }
    Just tried it, yet no prevail... I honestly havent a clue now... This is such a pain now, this one problem is resulkting in days loss of coding.

    EDIT

    uhh.... I think a big big big problem is that its <19 rather <20... Im going to try it with this little fix
    Last edited by SG57; 11-06-2006 at 10:41 PM.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You want and not or. If it's not a newline AND it's not a null character. Why don't you just read your map correctly in the first place? Read it one character at a time. If you reach a new line, go to the next row in the map and ignore that character.


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

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >If you reach a new line, go to the next row in the map and ignore that character.
    I would agree with Quzah here. That way you know the characters in your array are part of the map, not newlines.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I took your level 1 maze and altered it a bit to this:

    Code:
    ##### ##### ##### ######### #
    #####  #### ##### ######## ##
    #####   ### ##### ####### ###
    #####    ## ##### ###### ####
    #####     # ##### ##### #####
    #############################
    ######                 ######
    ######                 ######
    ######                 ######
    ######                 ######
    ######                 ######
    ######                 ######
    ######   #       #     ######
    ######F  #   # * # * S ######
    #############################
    ###########   ###############
    ###########   ###############
    ###########   ###############
    ###########   ###############
    abc# = brick/wall/cieling/floor/ground/etc.
    ' ' = nothing/space/walk in/through
    * = block (moveable brick basically)
    F = finsih
    S = start/player position.

    and wrote this program to show one way to transform the array data, back and forth from a 2D array, to a 1D array, and back to a zeroed out 2D array.

    It uses the above data in a file named 1SG.txt

    /*
    SG.c Loads a maze from a file, into a 2D array, then transfers that maze
    data into from the 2D array, into the 1D array, then transfers it back into
    a zeroed out 2D array.
    */

    Code:
    #include <stdio.h>
    
    const int Row = 20;
    const int Col = 30;
    
    /* In this program, the zero'th row is not used at all */
    
    int main(void)  {
      char chr;
      char ch;
      int c, good, i, r;
    
      FILE *in;
      printf("\n\n\n");
      if ((in = fopen("1SG.txt", "rt")) == NULL)  {
         fprintf(stderr, "Can't open input file - program terminating\n");
         return 1;
      }
      char maze2d[Row][Col];
      char maze1d[Row * Col];
      /* load the 2D array up from the file */
      printf("\n\n This is the 2D Array, loaded from the file: \n\n");
      int cell = 0;
      for(r = 1; r < Row; r++) { 
         for(c = 1; c <= Col; c++) {
            if((good = fscanf(in, "%c", &ch)) == 1)  {
               cell++;
               if(c % Col == 0) {
                 printf("\n");
                 continue;
               }
               maze2d[r][c] = ch;
               printf("%c", ch);
            }
            else {
               printf("\n Couldn't read the char in the file - terminating\n");
               return 1;
            }
         }
      }
      fclose(in);
      printf("\n\n Transfer the 2D array, into the 1D array: \n\n");
      printf(" Hit Enter to Continue\n\n");
      while((ch = getchar()) != '\n');
      /* (569 array elements to be printed) transfer the 2D array into the 1D */
      r = 1;
      for(i = 1, c = 1; i < 570; i++)  {
         c = i % Col;
         if(c == 0) { 
           r++;
           printf("\n");
           continue;
         }
         ch = maze2d[r][c];
         maze1d[i] = ch;
         printf("%c", ch);
         if(ch == '9')
           printf("\n");
      } /* end of for i = 1...*/
    
      printf("\n\n Finally, transfer the 1D array into the 2D array: \n");
      printf(" Zeroing out the 2D array\n\n");
      printf(" Reloading it from the 1D array. This is the 2D Array: \n");
      for(r = 0; r < Row; r++)
        for (c = 0; c < Col; c++)
           maze2d[r][c] = 0;
      while((chr = getchar()) != '\n');
      r = 1;
      for(i = 1, c = 1; i < 570; i++)  {
         c = i % Col;
         if (c == 0) {
           r++;
           ch = 32;
           maze1d[i] = 32; /* This will give you a blank space, as a char */
           printf("\n");
           continue;
         }
         ch = maze1d[i];   /* just for debug */
         maze2d[r][c] = maze1d[i];
         printf("%c", maze2d[r][c]);
    
      } /* end of for */
      printf("\n\n Program Complete - Press Enter to Exit \n");
      while((chr = getchar()) != '\n');
      return 0;
    }
    Adak
    Last edited by Dave_Sinkula; 11-07-2006 at 12:28 AM. Reason: Added [code][/code] tags to preserve whitespace.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your code will only compile on a C99 compiler. Variable sized arrays, mixed declarations, etc etc.


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

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The 2D array is 20 x 30, the 1D array is 20 * 30. What is variable about their size? It doesn't change.

    I just don't need it all for the program.

    In the new standards, aren't char's just small int's?

    Adak

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They've always been "small ints". That is to say, they've always been able to store small integeral values. Anyway, even if your array size is constant, it's still C99 due to your mixed declarations.


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

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I did that on purpose to show another way it could be done to the OP. (That is, that SG could use either char's or int's, and just print out the int's with the %c modifier).

    SG if you get stuck with this, let me know and I'll fix it up.

    Adak

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I have no idea what you're talking about. This is a "mixed declaration":
    Code:
    int stuffhere;
    
    function( );
    
    int thisisamixeddeclaration;
    This has nothing to do with using ints or chars.


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

  10. #25
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    Yes - im still having trouble... Only this stair case effect

    Im seriosuly considering just making an app compilable via Dev-C++ MinGW compiler, that will take whats in the lvl file, and reverse stair case it. That way, when loading, itll load and display correctly. Seems better than constantly getting on alot of peoples nerves here

    Also, this could add a nice feat. to my game... Make it seem more - high tech needing a .lvl converter :-P Anyways, Ill keep you posted on the development of that and Im 45% sure ill have a problem.

    quzah - As for mixed declarations, my compiler allows it so no big deal, unless they lead to efficiency loss.
    Last edited by SG57; 11-08-2006 at 08:00 PM.

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What trouble? What stair case effect?

    My code worked great from file to one type of array, and then to the other type of array.

    Could you be about 10X more specific and drop by more often?

    After several days the post sinks into the abyss of page 2 and hardly anyone will even see it.

    There is no "efficiency loss", for crying out loud. Quzah is just .....being Quzah.

    Adak

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array -> 2D array
    By Tupcia in forum C Programming
    Replies: 4
    Last Post: 05-16-2008, 02:29 AM
  2. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  3. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM