Skip to main content
added 430 characters in body
Source Link

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.

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;

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.

deleted 4 characters in body
Source Link

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 anymore graphical characters like, for instance, 'X' for walls.

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

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 any characters like, for instance, 'X' for walls.

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

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;
added 55 characters in body
Source Link

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 any characters like, for instance, 'X' for walls.

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

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

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;
    }
}

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 any characters like, for instance, 'X' for walls.

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