Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1.     class Cell
  2.     {
  3.         public char type;
  4.         public Cell() : this('0') { }
  5.         public Cell(char type)
  6.         {
  7.             this.type = type;
  8.         }
  9.     }
  10.  
  11.     class Map
  12.     {
  13.         public Cell[,] map;
  14.         public in size;
  15.         public int height, width;
  16.         public void ReadFile(string path)
  17.         {
  18.             Console.WriteLine("Reading file...");
  19.             string data;
  20.             try
  21.             {
  22.                 using (FileStream fs = new FileStream(path, FileMode.Open))
  23.                 {
  24.                     bool check = true;
  25.                     byte[] buf = new byte[fs.Length];
  26.                     fs.Read(buf, 0, (int)fs.Length);
  27.                     data = Encoding.Default.GetString(buf);
  28.                     data = data.Replace("\r\n", " ");
  29.                     for (int i = 0; !data[i].Equals(' ') || height == 0 || check == true; i++)
  30.                     {
  31.                         if (!data[i].Equals(' ') && check)
  32.                         {
  33.                             this.width += (int)char.GetNumericValue(data[i]);
  34.                         }
  35.                         else if (!data[i].Equals(' ') && !check)
  36.                         {
  37.                             this.height += (int)char.GetNumericValue(data[i]);
  38.                         }
  39.                         else
  40.                         {
  41.                             check = false;
  42.                         }
  43.                     }
  44.                     data = data.Replace(width.ToString(), "");
  45.                     data = data.Replace(height.ToString(), "");
  46.                    
  47.                     data = data.Replace(" ", "");
  48.                 }
  49.  
  50.             }
  51.             catch (Exception)
  52.             {
  53.                 Console.WriteLine("File reading ERROR!");
  54.                 return;
  55.             }
  56.             size = data.Length;
  57.             Generate(data);
  58.  
  59.         }
  60.         private void Generate(string data) {
  61.             map = new Cell[height, width];
  62.             try
  63.             {
  64.                 for (int i = 0; i < height; i++)
  65.                 {
  66.                     for (int j = 0; j < width; j++)
  67.                     {
  68.                         this.map[i, j] = new Cell(data[width * i + j]);
  69.                     }
  70.                 }
  71.             }
  72.             catch (Exception)
  73.             {
  74.                 Console.WriteLine("Map generate error!");
  75.                 throw;
  76.             }
  77.            
  78.         }
  79.  
  80.         public override string ToString()
  81.         {
  82.             string result = "";
  83.  
  84.             for (int i = 0; i < height; i++)
  85.             {
  86.                 for (int j = 0; j < width; j++)
  87.                 {
  88.                     result += map[i,j].type + " ";
  89.                 }
  90.                 result += "\r\n";
  91.             }
  92.             return result;
  93.         }
  94.  
  95.  
  96.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement