Reading tile maps from a text file

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am trying to read some integers from a text file as a tile map for use in an XNA game Im making, however the way I am doing it right now will only allow my map to be a maximum of 9 x 9 tiles:
<pre class="prettyprint List<List<int>> tile = new List<List<int>>();

StreamReader reader;
reader = new StreamReader("map.txt");

int rows = reader.Read() - 48;
int cols = reader.Read() - 48;

int dumpdata = reader.Read();
dumpdata = reader.Read();

for (int x = 0; x < rows; x++)
{
tile.Add(new List<int>());
for (int y = 0; y < cols; y++)
{
tile[x].Add(new int());
tile[x][y] = reader.Read() - 48;
}
dumpdata = reader.Read();
dumpdata = reader.Read();
}

reader.Close();[/code]
<br/>
and the text file Im loading from looks like:
<pre class="prettyprint 99
000000000
011111110
010000010
010000010
011111110
010000010
010000010
011111110
000000000[/code]
<br/>
What would I need to change in order to have my map files any size I want? I am able to do this in C++ fairly easy as it separates the variables with spaces meaning my map files could look like:
<pre class="prettyprint 20 10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 3 2 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1[/code]
<br/>
is there a way to separate what it reads using spaces in C# and System.IO?

View the full article
 
Back
Top