2017-08-09 23 views
1

我正在经历一个问题,虽然它很简单,但我看不到出路。如何正确地将文本文件中的值分配给C#中的2d矩阵#

我读一个文件,并将其分配给数组:

string[] lines = File.ReadAllLines(@"C:\Users\\Documents\Visual Studio 2017\Projects\TheMaze\TheMaze\Data\" + FileName + ".txt"); 

map = new MapFile(); 

      int width = lines[0].Replace(" ", "").Length; 
      int height = lines.Length; 

      map.Matrix = new byte[width, height]; 



      for (int y = 0; y < height; y++) 
      { 
       string line = lines[y].Replace(" ", ""); 

       for (int x = 0; x < line.Length;x++) 

       { 

        // 

       } 

       Console.ReadKey(); 
      } 


     } 

     return map; 
    } 

我只是不明白如何从文件中正确分配值到我的矩阵对象。

要赋值给哪个变量?

我试过map.Matrix[x,y] = line[x] 但我得到一个错误,因为我必须将x转换为字节。

上我的课

映射文件,我只有这个变量:public byte[,] Matrix = null;

如果我转换线[X]为字节所有的值都为35

以后打印我是那种完全丧失和欣赏任何帮帮我。

The File structure I have:

这仅仅是一个文本文件,我手动写道。

+0

你不必转换''X ''转换为''byte'',但是''line [x]''的返回值。尝试下面的map.Matrix [x,y] = Convert.ToByte(line [x]);''如果你的文件在每一行都有一个数字,你必须写''map.Matrix [x,y ] = Convert.ToByte(int.Parse(line [x]));'' –

+0

感谢您的答案。 我的文件实际上由'#','。','S','F'构成 其中#是墙壁,'。'是有效路径,'S'是起点,'F'是终点。 – Fix3r

+0

你可以发表你的文件的样本,只需几行就足够了。 –

回答

1

简短的版本只是为了好玩:]

string text = File.ReadAllText(@"..\..\Data\" + FileName + ".txt"); 

foreach (char c in text.Replace(" ", "")) 
{ 
    Console.BackgroundColor = Console.ForegroundColor = (ConsoleColor)(c & 15); 
    Console.Write(c); 
} 
0

我将变量height和width添加到我的MapFile类中,以便更容易跟踪数组的大小。

的代码是从这里不仅改变:

for (int y = 0; y < map.height; y++) 
      { 
       string line = lines[y].Replace(" ", ""); 


       for (int x = 0; x < map.width;x++) 

       { 
        map.Matrix[x,y] = Convert.ToByte(line[x]); 

        if (line[x] == '#') 
        { 
         map.Matrix[x, y] = 1; 
        } 

        if (line[x] == '.') 
        { 
         map.Matrix[x, y] = 2; 
        } 

        if (line[x] == 'S') 
        { 
         map.Matrix[x, y] = 3; 
        } 

        if (line[x] == 'F') 
        { 
         map.Matrix[x, y] = 5; 
        } 
       } 
      } 

而且我MapDisplay类现在我有:

for(int y = 0; y < map.height; y++) 
     { 


      Console.WriteLine(); 

      for (int x = 0; x<map.width; x++) 
      { 

       Console.Write(map.Matrix[x, y]); 
      } 
     } 

从我的矩阵输出正确的:

With the Right output from my matrix:

现在为了让迷宫看起来更好一点,我改变了后面输出地和Foregound颜色是这样的:

for(int y = 0; y < map.height; y++) 
     { 


      Console.WriteLine(); 

      for (int x = 0; x<map.width; x++) 
      { 
       if (map.Matrix[x, y] == 3) 
       { 

        Console.BackgroundColor = ConsoleColor.Green; 
        Console.ForegroundColor = ConsoleColor.Green; 
        Console.Write("3"); 
        Console.ResetColor(); 


       } 
       else if (map.Matrix[x, y] == 2) 
       { 
        Console.BackgroundColor = ConsoleColor.Blue; 
        Console.ForegroundColor = ConsoleColor.Blue; 
        Console.Write("2"); 
        Console.ResetColor(); 
       } 
       else if (map.Matrix[x, y] == 5) 
       { 
        Console.BackgroundColor = ConsoleColor.Green; 
        Console.ForegroundColor = ConsoleColor.Green; 
        Console.Write("5"); 
        Console.ResetColor(); 
       } 
       else if (map.Matrix[x, y] == 1) 
       { 
        Console.BackgroundColor = ConsoleColor.DarkRed; 
        Console.ForegroundColor = ConsoleColor.DarkRed; 
        Console.Write("1"); 
       } 

      } 
     } 

输出用不同的颜色: Colored Maze Output

相关问题