loading an ascii file into a 2D array

Started by
4 comments, last by CaptCanada 15 years, 9 months ago
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
Better to try and fail than to fail by not trying.
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?
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 fileifstream fin(szLevelName);for (int y=0;y<MazeHeight;y++)	{		for (int x=0;x<MazeWidth;x++)		{			fin >> Maze[x][y];		}        }}
Better to try and fail than to fail by not trying.
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?
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
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 =)
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.

Better to try and fail than to fail by not trying.

This topic is closed to new replies.

Advertisement