char map[MAX_Y][MAX_X] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
...
...
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
As I got sick of hard-coding everything I made a visual level-editor to make life easier, and now I simply load everything into the array holding the levels from disk instead. It works good for my purposes, cause I can access and BitBlit any tile by map[y_tile][x_tile].
However as I surfed the web looking for other Sokoban clones I found out that some of them use ASCII text files for holding the level data instead, and now I would like to use something similar myself but I don''t know where to start. I found a text file containing all the original levels, and I would like to include those in my game as well, but I sure could use some guidance here.
The format of the text file looks something like this, where the numbers represent walls, boxes, floor etc.
00000000000000000000
00000000222222200000
00000002211215200000
00000002111211200000
00000002414141200000
00000002142211200000
00000222141212200000
00000233333112000000
00000222222222000000
00000000000000000000
How can I read these "maps" that are listed one by one in a text file into some form of char array (and also determine when ? Or is it possible to create a 2D char array looking somewhat like the map above (i mean without the commas that I used in my first hard-coded attempt), and still being able to render the tiles to screen with ease?
Even better would be if I could use the standard format for Sokoban level files. Again it''s simply a text file that uses the characters listed below to describe each level. This would probably be the best approach.
- empty space
$ - box
. - target space
* - box on target space
@ - player starting position
+ - player starting on target space
# - wall
Example level:
######
#@ #####
# $ $ ..#
##########
For those of you who don''t know what Sokoban is, here''s a link to a Java clone: http://javaboutique.internet.com/Sokoban/
(I will take a look at the source found here now also, even I don''t know Java)
All assistance is much appreciated! Thanks a million!
Loading tile map from ASCII file? Sokoban
For the sake of C/C++ programming practise I''m working on a clone of the classic box-pushing Sokoban game. I got it all up and playable by now, but I would like to change the way I define the tile map. As I first started out the programming I hard-coded every single level in a x*y 2D char array, which obviously turned out to be a very tedious method. The array looks something like this:
You already asked this in ''Game Programming''
"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green"
-Peter Griffin
"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green"
-Peter Griffin
"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that really matters is green"-Peter Griffin
I hope this helps, please forgive any syntax/sematic errors I don't use c/c++ normaly.
all in stdio.h
fopen opens a text file
fclose closes a text file
feof detects end-of-file marker in a file
fprintf prints formatted output to a file
fscanf reads formatted input from a file
fputs prints a string to a file
fgets reads a string from a file
fputc prints a character to a file
fgetc reads a character from a file
to read a level:
open the text file
basicly two nested for loops (x,y) with a map[x][y]=fgetc after.
close the text file
To jump to multiple levels you need to reset the file position to the begining (Zero) and read through : level*HeightOfArray*WidthOfArray
assuming the array is a constant size with no extra info (headers etc).
If you wish to ignore whitespace then check each char as its read and repeat reading chars until a non-whitespace char is found.
there may be better methods inc reading the whole file into a string and parsing the string (buffered input), but this will work.
Saving is just a reversal of the above process, although you will have to be aware that to add a level to an existing file you need to:
read the file
write all levels before the level your adding
write your level
write all levels after the level your adding
eg
write levels 1,2,3,5
write new level 6
write levels 7,8,9
Regards, Jay
Edited by - Jason Zelos on February 10, 2002 4:47:40 PM
Edited by - Jason Zelos on February 10, 2002 4:48:34 PM
all in stdio.h
fopen opens a text file
fclose closes a text file
feof detects end-of-file marker in a file
fprintf prints formatted output to a file
fscanf reads formatted input from a file
fputs prints a string to a file
fgets reads a string from a file
fputc prints a character to a file
fgetc reads a character from a file
to read a level:
open the text file
for(y=0,y<HeightOfArray,y++) for(x=0,x<WidthOfArray,x++) Map[x][y]=fgetc
basicly two nested for loops (x,y) with a map[x][y]=fgetc after.
close the text file
To jump to multiple levels you need to reset the file position to the begining (Zero) and read through : level*HeightOfArray*WidthOfArray
assuming the array is a constant size with no extra info (headers etc).
If you wish to ignore whitespace then check each char as its read and repeat reading chars until a non-whitespace char is found.
there may be better methods inc reading the whole file into a string and parsing the string (buffered input), but this will work.
Saving is just a reversal of the above process, although you will have to be aware that to add a level to an existing file you need to:
read the file
write all levels before the level your adding
write your level
write all levels after the level your adding
eg
write levels 1,2,3,5
write new level 6
write levels 7,8,9
Regards, Jay
Edited by - Jason Zelos on February 10, 2002 4:47:40 PM
Edited by - Jason Zelos on February 10, 2002 4:48:34 PM
Thanks for your help, and for giving me such good explanation... I tried your method, and everything is working like a charm now! Thanks!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement