grr.... General C - 1D array to 2D array nothing is working!

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

grr.... General C - 1D array to 2D array nothing is working!

Post by sg57 »

Ok, Ive asked around alot, tried alot of variations, etc yet I still cant get it working like it should.

All the results i get are

***
* **
** *
***

When it should be

***
***
***
***

I have this one array that gets a files data loaded into it.

map[592];

An example ofa map is:

Code: Select all

#####    ##    ##    #      #
##### ##### ##### ######## ##
#####    ## #  ##    #### ###
######## ## ## ##### ### ####
#####    ##    ##    ## #####
#############################
######                 ######
######                 ######
######                 ######
######                 ######
######                 ######
######                 ######
######   #       #     ######
######F  #   # * # * S ######
#############################
###########   ###############
############# ###############
############# ###############
###########     #############
abc
the last 3 chars are the password for the level.

To make it easier to work with, im trying to get it into a 2D array

level[19][30];

19 because i dont WANT the password in mine, 30 because thats how many chars wide i want to read in. so, i basically want to transfer each line of that, into the 2D array according to each line. So, my current method is:

Code: Select all

     j=0;
     for&#40;i=0; i<19; i++&#41; &#123;
                       for &#40;z=0; z<30; z++&#41; &#123;
                            if&#40;map&#91;j&#93; == '\n'&#41; &#123;&#125;
                            else &#123;
                                  level&#91;i&#93;&#91;z&#93;=map&#91;j&#93;;
                            &#125;
                             j++;
                       &#125;
     &#125;
This produces the stairs effect
***
* **
** *
***
Ive tried just about everything to no prevail.

If you need know more info to help solve this problem, please ask and i will supply. This little problem has held me back from progressing for 3 days now, its really getting annoying :(
User avatar
ryoko_no_usagi
Posts: 65
Joined: Tue Nov 29, 2005 4:47 pm

Post by ryoko_no_usagi »

Make sure the map is not using CRLF. The size of the array suggests this (592 = 19 * 31 + 3).
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

Post by sg57 »

Yes it is using Character Return Line Feed. Im guessing that is just the new line char right? Cause ive done an if statement saying whether map[j] == '\n' then just dont transfer it over

It, is, one character right? CRLF?
adrahil
Posts: 274
Joined: Thu Mar 16, 2006 1:55 am

Post by adrahil »

Code: Select all

     j=0;
     for&#40;i=0; i<19; i++&#41; &#123;
                       for &#40;z=0; z<30; z++&#41; &#123;
                            if&#40;map&#91;j&#93; == '\n'&#41; &#123;&#125;
                            else &#123;
                                  level&#91;i&#93;&#91;z&#93;=map&#91;j&#93;;
                            &#125;
                             j++;
                       &#125;
     &#125;
Well, you still increment z and i even if the map[j] is \n :) That's probably the problem, because it makes it skip some stuff... Like, if you have this:

Code: Select all

**
**
, and loop for let's say two, you get this in the array:

Code: Select all

&#123;&#123;"*", "*"&#125;, // end of first two, increment i...
&#123;0, "*"&#125;, // continue counting, but the \n also increments the loop's i/j or whatever... so it makes a shift...
...
&#125;
User avatar
ryoko_no_usagi
Posts: 65
Joined: Tue Nov 29, 2005 4:47 pm

Post by ryoko_no_usagi »

I meant that the map might terminate each line with "\r\n" (common on windows) as opposed to just a '\n" (UNIX method). So that's two bytes instead of just one.
adrahil
Posts: 274
Joined: Thu Mar 16, 2006 1:55 am

Post by adrahil »

Oh, and learn serialization, it will help you a lot in a multi-level kind of game ;)
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

Post by sg57 »

AHH!

That must be it... I knew each line has a \n at the end, but i didnt know Windows has it \r\n or some variation. Ill try it out :)

EDIT

Ah, your right (3 posts above me). i/z is still incrementing... Therefore, my new version:

Code: Select all

     j=0;
     for&#40;i=0; i<19; i++&#41; &#123;
                       for &#40;z=0; z<29; z++&#41; &#123;
                            level&#91;i&#93;&#91;z&#93;=map&#91;j&#93;;
                            j++;
                       &#125;
                       level&#91;i&#93;&#91;29&#93;='\0';
     &#125;
EDIT

Spotted the problem again. Sure, the 2d array is fine, but the map 'j' indicator willstill point ot the \n. so, ive added, right below level[29]='\0', j+=2 (ill experiment tosee if it works)
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

There are easy and hard ways to parse a ascii map. I think you digged the hard way :)
I'd suggest reading in line after line from the file (fgets), then copying 30 bytes (str/memcpy) from that line into your level array.
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

Post by sg57 »

Ya - ive been told to use memcpy. i have no experience with it. as for fgets - i dont have access to that rather sceIo* calls, so i should be fine. just need to practice with memcpy
Post Reply