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. :)