Jump to content
  • Advertisement
Sign in to follow this  
CaptCanada

loading an ascii file into a 2D array

This topic is 3816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all I am coding a simple 2D maze dungeon game and have a test map level stored as a .txt file. I would like some help on how to load the 1's and 0's in the file into a 2 dimensional array called Maze I am using Visual C# and the XNA framework to code the game. Thanks

Share this post


Link to post
Share on other sites
Advertisement
I have a few questions to help me (or anyone else) answer your question. =)

What is the format of the maze in the text file? Is it simply a 1 means you can't walk thru that space and a zero means you can? Do you know the dimensions of the maze? Is it always NxM (or even NxN)? Do you have to figure out how big it is when you load it? Does the data file have a header?

File I/O in C# isn't too bad. Is that what you're having trouble with?

Share this post


Link to post
Share on other sites
Hi,

Sorry for not posting more info. Here are the answers to your questions:

1)The mapfile I am working with is 26x46 and is stored as an ascii text file.

2) It is comprised of 1's and 0's at the moment, whereby 1 represents a wall and 0 walkable space.

3)I know the dimensions of the maze, it is based on the mapfile size.

4) I am unfamiliar with the terms NxM and NxN.

5) I don't have to figure out how big the mapfile is when loaded.

6) The mapfile doesn't have any header info.

7) I am having trouble with C# file I/O in regards to reading the mapfile and storing the 1's and 0's within into the array.

I am rewriting the code for C#, as I wrote it originally in C++ as a console game. The map loading part of the C++ code basically loaded the text file and I used two nested "For" loops to store the information into a 2D array.

I just need some help on how do code the same in C#. Here is the code snippet from the C++ version:


void load_level(char* szLevelName)
{
cout << "Loading Level " << LevelCount << " ... " << endl;
Sleep(1000);
cout << "Maze Ready" << endl;
// read in the map file
ifstream fin(szLevelName);
for (int y=0;y<MazeHeight;y++)
{
for (int x=0;x<MazeWidth;x++)
{
fin >> Maze[x][y];
}
}
}


Share this post


Link to post
Share on other sites
3) What we're asking is whether the maze will always be that size, or if you want the maze made whatever size the map in the file is.

4) They're just numerical placeholders. In your case, NxM means N=26 and M=46.

Also, you're a very naughty programmer. You slept for a second just to simulate a level-load delay, and worse yet, loaded the level after reporting you had loaded it?

Share this post


Link to post
Share on other sites
My first recommendation to you is to do precisely what I do before I post to these forums =)

Google for it. In this case I searched for "c# file i/o" and I got [a href="http://support.microsoft.com/kb/304430"]this[/a] as a first hit. There were multiple links beyond that in the google search results that looked as if they would also be useful.

Essentially the loop you have will work; you just need to change what types you are using and what functions you are calling. The format, I think, will be identical.

One small thing: I can see that, since professional games (or even large hobbyist games) have "Loading" screens, you might like to have such an appearance for own game. I've always wanted to see "Loading" on one of my games...but frankly, none of them have ever taken more than a few seconds. There's no need to sleep the system ;) Eventually you will wish everything loaded instantly, because loading screens are sucky. (Compare Diablo 2 - very seldom and quick loading screens - vs something like Bioshock, that can take a minute or more.) Something else misleading about your code here is that you print "Maze Ready" before you start actually loading it =)

Share this post


Link to post
Share on other sites
I did find a few hits when I did exactly what you suggested, to google C# file i/o.

I am not sure if that was one of the links I found though. I will check it out for sure.

As for the printing that the level was loaded before it actually was, ya in hindsight I guess that was kinda misleading. I wrote the C++ console version quite a while ago, and was did it as a learning exercise, so the coding method is probably less than professional I'll admit

Wyrframe, the map size is a constant, they are always going to be 26x46.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!