0
\$\begingroup\$

I'm having a bit of an ordeal with the StreamReader.

I'm working on a game where you build the map with a 2D array by imputing different numbers so the array like this:

    int[,] map = new int[,]
    {
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,1,1,1,1,1,2,1,1,1,1,1,1,0},
        {0,1,1,1,1,1,1,1,1,1,1,1,1,0},
        {0,1,1,1,1,1,1,1,1,1,1,1,1,0},
        {0,1,1,1,1,1,1,1,1,1,1,1,1,0},
        {0,1,1,1,1,1,1,1,1,1,1,1,1,0},
        {0,1,1,1,1,1,1,1,1,1,1,1,1,0},
        {0,1,1,1,1,1,1,1,1,1,1,1,1,0},
        {0,1,1,1,1,1,1,1,1,1,1,1,1,0},
        {0,1,1,1,1,1,1,1,1,1,1,1,1,0},
        {0,1,1,1,1,1,1,1,1,1,1,1,1,0},
        {0,1,1,1,1,1,1,1,1,1,1,1,1,0},
        {0,1,1,1,1,1,1,1,1,1,1,1,1,0},
        {0,1,1,1,1,1,1,1,1,1,1,1,1,0},
        {0,1,1,1,1,1,1,1,1,1,1,1,1,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    };

will give you the outcome where the 0's are one type of block and the 1's another and so on.

I currently have that working, however the array is hardcoded in.

What I am now trying to solve is how to read it from a text file. I attempted doing it in a Console Application with this code but I keep on receiving errors.

    static void Main(string[] args)
    {
        int[,] map = new int[300, 300];

        StreamReader SR = new StreamReader(@"F:\test.txt");

        for (int y = 0; y < 300; y++)
        {
            for (int x = 0; x < 300; x++)
            {
                map[y, x] = int.Parse(SR.ReadLine());
            }
        }
        SR.Close();

        for (int y = 0; y < 300; y++)
        {
            for (int x = 0; x < 300; x++)
            {
                Console.Write(map[y, x]);
            }
        }
        Console.ReadLine();
    }

Any idea how I can make it work for a 2D array so that whenever the compiler reads a digit, it allocates a number into a specific area in the array, so for example when a file like this is read:

11111
10001
10001
10001
10001
11111

It will turn into an array like this:

1,1,1,1,1
1,0,0,0,1
1,0,0,0,1
1,0,0,0,1
1,0,0,0,1
1,1,1,1,1

Thanks for any help provided. :)

\$\endgroup\$
3
\$\begingroup\$

With int.Parse(SR.ReadLine()) you are trying to convert the whole input line (consisting of 300 digits) into one single int. This does of course not work.

It is easier to read the file into an array of strings representing the lines all at once. No explicit opening and closing of files.

string[] lines = File.ReadAllLines(path);

Then you can access single characters of the line through an index

for (int y = 0; y < 300; y++)
{
    for (int x = 0; x < 300; x++)
    {
        map[y, x] = lines[y][x] == '0' ? 0 : 1;
    }
}

This also easily allows you to use more graphical characters like 'X' for walls.

map[y, x] = lines[y][x] == 'X' ? 1 : 0;

To be able to read maps of different sizes you can do

string[] lines = File.ReadAllLines(path);

int width = lines[0].Length;
int height = lines.Length;
if (lines[height - 1].Length < width) {
    height--;
}

int[,] map = new int[height, width];

//TODO: fill the map as shown above, but use width and height instead of constant 300.

If you hit enter after the last map line, you might get an empty last line. Therefore we subtract 1 if the last line is too short. This won't handle additional empty lines at the end.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Yes, however how do I edit my map array at that stage, calling it out as a int[,] map = new int[300,300] makes an out of bounds error... \$\endgroup\$ – Pavvel Jan 16 '18 at 19:03
  • \$\begingroup\$ Added code to allows maps variable in size. \$\endgroup\$ – Olivier Jacot-Descombes Jan 16 '18 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.