Quantcast
Channel: Reading a text file and changing each digit into a value inside an array c# - Game Development Stack Exchange
Viewing all articles
Browse latest Browse all 2

Reading a text file and changing each digit into a value inside an array c#

$
0
0

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:

111111000110001100011000111111

It will turn into an array like this:

1,1,1,1,11,0,0,0,11,0,0,0,11,0,0,0,11,0,0,0,11,1,1,1,1

Thanks for any help provided. :)


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>