Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Cell
- {
- public char type;
- public Cell() : this('0') { }
- public Cell(char type)
- {
- this.type = type;
- }
- }
- class Map
- {
- public Cell[,] map;
- public in size;
- public int height, width;
- public void ReadFile(string path)
- {
- Console.WriteLine("Reading file...");
- string data;
- try
- {
- using (FileStream fs = new FileStream(path, FileMode.Open))
- {
- bool check = true;
- byte[] buf = new byte[fs.Length];
- fs.Read(buf, 0, (int)fs.Length);
- data = Encoding.Default.GetString(buf);
- data = data.Replace("\r\n", " ");
- for (int i = 0; !data[i].Equals(' ') || height == 0 || check == true; i++)
- {
- if (!data[i].Equals(' ') && check)
- {
- this.width += (int)char.GetNumericValue(data[i]);
- }
- else if (!data[i].Equals(' ') && !check)
- {
- this.height += (int)char.GetNumericValue(data[i]);
- }
- else
- {
- check = false;
- }
- }
- data = data.Replace(width.ToString(), "");
- data = data.Replace(height.ToString(), "");
- data = data.Replace(" ", "");
- }
- }
- catch (Exception)
- {
- Console.WriteLine("File reading ERROR!");
- return;
- }
- size = data.Length;
- Generate(data);
- }
- private void Generate(string data) {
- map = new Cell[height, width];
- try
- {
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- this.map[i, j] = new Cell(data[width * i + j]);
- }
- }
- }
- catch (Exception)
- {
- Console.WriteLine("Map generate error!");
- throw;
- }
- }
- public override string ToString()
- {
- string result = "";
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- result += map[i,j].type + " ";
- }
- result += "\r\n";
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement