| View previous topic :: View next topic |
| Author |
Message |
sg57
Joined: 14 Oct 2005 Posts: 154
|
Posted: Tue Nov 07, 2006 4:09 pm Post subject: grr.... General C - 1D array to 2D array nothing is working! |
|
|
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: |
##### ## ## # #
##### ##### ##### ######## ##
##### ## # ## #### ###
######## ## ## ##### ### ####
##### ## ## ## #####
#############################
###### ######
###### ######
###### ######
###### ######
###### ######
###### ######
###### # # ######
######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: |
j=0;
for(i=0; i<19; i++) {
for (z=0; z<30; z++) {
if(map[j] == '\n') {}
else {
level[i][z]=map[j];
}
j++;
}
} |
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 :( |
|
| Back to top |
|
 |
ryoko_no_usagi

Joined: 29 Nov 2005 Posts: 65
|
Posted: Tue Nov 07, 2006 5:51 pm Post subject: |
|
|
| Make sure the map is not using CRLF. The size of the array suggests this (592 = 19 * 31 + 3). |
|
| Back to top |
|
 |
sg57
Joined: 14 Oct 2005 Posts: 154
|
Posted: Tue Nov 07, 2006 7:54 pm Post subject: |
|
|
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? |
|
| Back to top |
|
 |
adrahil
Joined: 16 Mar 2006 Posts: 277
|
Posted: Tue Nov 07, 2006 8:06 pm Post subject: |
|
|
| Code: | j=0;
for(i=0; i<19; i++) {
for (z=0; z<30; z++) {
if(map[j] == '\n') {}
else {
level[i][z]=map[j];
}
j++;
}
} |
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:, and loop for let's say two, you get this in the array: | Code: | {{"*", "*"}, // end of first two, increment i...
{0, "*"}, // continue counting, but the \n also increments the loop's i/j or whatever... so it makes a shift...
...
} |
|
|
| Back to top |
|
 |
ryoko_no_usagi

Joined: 29 Nov 2005 Posts: 65
|
Posted: Tue Nov 07, 2006 8:08 pm Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
adrahil
Joined: 16 Mar 2006 Posts: 277
|
Posted: Tue Nov 07, 2006 8:08 pm Post subject: |
|
|
| Oh, and learn serialization, it will help you a lot in a multi-level kind of game ;) |
|
| Back to top |
|
 |
sg57
Joined: 14 Oct 2005 Posts: 154
|
Posted: Wed Nov 08, 2006 10:21 am Post subject: |
|
|
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: |
j=0;
for(i=0; i<19; i++) {
for (z=0; z<29; z++) {
level[i][z]=map[j];
j++;
}
level[i][29]='\0';
} |
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[i][29]='\0', j+=2 (ill experiment tosee if it works) |
|
| Back to top |
|
 |
Raphael

Joined: 17 Jan 2006 Posts: 646 Location: Germany
|
Posted: Wed Nov 08, 2006 1:54 pm Post subject: |
|
|
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[i] 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 |
|
| Back to top |
|
 |
sg57
Joined: 14 Oct 2005 Posts: 154
|
Posted: Wed Nov 08, 2006 4:37 pm Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
|