I have the following file that looks like this:-
1,2,3,4,5,6,7,8,9,
10,11,12,13,14,15,
16,17,18,19,20,21,
now I'm reading it and I stored all the data into 1D string array. Then I parse that 1D string array to 1D Int array. However I need to copy the data to 2D array. I have been trying to do it but I can't seem to get my head around it.
Here is my code
class Program
{
static void Main(string[] args)
{
string buffer;
string[] map = new string[100];
int[] mapint = new int[100];
buffer = File.ReadAllText("D:/123.txt");
buffer = buffer.Replace("\r", string.Empty).Replace(" ", string.Empty);
map = buffer.Split(',');
for (int i = 0; i < 20; i++)
{
mapint = Convert.ToInt32(map);
Console.Write(mapint + " ");
}
Console.ReadKey();
}
}
so i want to do something like this
for(int x = 0; x < row; x++)
for(int y = 0; y < column; y++)
map[x, y] = mapint[x];
you know store the first row when y = 0; second row when y = 1; etc...
When doing this, I store the Width and Height of the 2D map together with the 1D array, so I have everything I need when parsing it back.
Converting from 2D to 1D is easy, since you just concatenate all values. Now, when parsing back, you must know how many columns a line has. Unless all of your maps are of a fixed size, lets say, 10x10. You will have to store this data.
After that, it's easy:
Map = new int[Height, Width];
for (int i = 0; i < Width; i++)
for (int j = 0; j < Height; j++)
Map[j, i] = strArray[j * Width + i];
If you don't store this data and all of your values are of fixed size, its even easier, just replace the values on the above example for the fixed values.
example:
You have an array like this, which is composed of 100 items. All of your maps are 10x10, so you'll always have 100 items on the 1d array. To parse this back to 2D, all you gotta do is
I always hated when someone replied to me like this, so please take this as a suggestion and not a criticism!
I think what your doing may not be the best way to save and load your map data.
But to create a 2d array you do the following:
int[,] MapData = new int[100, 100];
I would suggest saving your map cordinates along with the tile info, without over complicating things you could just parse the first int as X the second int as Y and the 3rd int as the tile number.
I always hated when someone replied to me like this, so please take this as a suggestion and not a criticism!
I think what your doing may not be the best way to save and load your map data.
But to create a 2d array you do the following:
int[,] MapData = new int[100, 100];
I would suggest saving your map cordinates along with the tile info, without over complicating things you could just parse the first int as X the second int as Y and the 3rd int as the tile number.
I know how to create a 2D array. I know that i need to do this
int[,] MapData = new int[100, 100];
I know what you mean and I have tried your way long time ago and it was very messy and i didn't really like it. I thought my way was cleaner. I don't know. I Just prefer my way
thank you anyways.
When doing this, I store the Width and Height of the 2D map together with the 1D array, so I have everything I need when parsing it back.
Converting from 2D to 1D is easy, since you just concatenate all values. Now, when parsing back, you must know how many columns a line has. Unless all of your maps are of a fixed size, lets say, 10x10. You will have to store this data.
After that, it's easy:
Map = new int[Height, Width];
for (int i = 0; i < Width; i++)
for (int j = 0; j < Height; j++)
Map[j, i] = strArray[j * Width + i];
If you don't store this data and all of your values are of fixed size, its even easier, just replace the values on the above example for the fixed values.
example:
You have an array like this, which is composed of 100 items. All of your maps are 10x10, so you'll always have 100 items on the 1d array. To parse this back to 2D, all you gotta do is
Lets review the process of getting stuff back from the 1d array to the 2d array.
Each line has 10 items. So, for the first line [index 0] you just get the first 10 items. For the second line [index 1], you will get the second group of 10 items after the first 10 items on the 1d array.
Do you follow? This translates to, the items you want to get from the 1d array, start at LineIndex * ValuesPerLine, which means Y * 10.
Ok, so, the second line starts at LineIndex * ValuesPerLine, now to get the exact column you want, you just add its index. LineIndex * ValuesPerLine + ColumnIndex
After that, you add X, the current column index. Understood?
Lets review the process of getting stuff back from the 1d array to the 2d array.
Each line has 10 items. So, for the first line [index 0] you just get the first 10 items. For the second line [index 1], you will get the second group of 10 items after the first 10 items on the 1d array.
Do you follow? This translates to, the items you want to get from the 1d array, start at LineIndex * ValuesPerLine, which means Y * 10.
Ok, so, the second line starts at LineIndex * ValuesPerLine, now to get the exact column you want, you just add its index. LineIndex * ValuesPerLine + ColumnIndex
After that, you add X, the current column index. Understood?
In fact, using this method removes the need to use 2D arrays. You can view a 2D array as being arranged in memory as as contiguous sequence of 1D arrays.