public Map(int width, int height = 0, GameManager.FitnessFunctions fit = GameManager.FitnessFunctions.IntersectionScheme) // generate a uniform map { if (height == 0) { height = width; } this._width = width; this._height = height; _fitnessFunction = fit; _startingNodes = new List <Node>(); Node tempNode; Edge tempEdge; _grid = new List <List <Node> >(width); _grid.Add(new List <Node>(height)); tempNode = new Node(0, 0); _grid[0].Add(tempNode); tempEdge = new Edge(null, _grid[0][0], true, false, false); tempEdge = new Edge(null, _grid[0][0], false, false, false); for (int y = 1; y < height - 1; y++) { tempNode = new Node(0, y); _grid[0].Add(tempNode); tempEdge = new Edge(_grid[0][y - 1], _grid[0][y], true); tempEdge = new Edge(null, _grid[0][y], false, false, false); } tempNode = new Node(0, height - 1); _grid[0].Add(tempNode); tempEdge = new Edge(_grid[0][height - 2], _grid[0][height - 1], true); tempEdge = new Edge(_grid[0][height - 1], null, true, false, false); tempEdge = new Edge(null, _grid[0][height - 1], false, false, false); for (int x = 1; x < width; x++) { _grid.Add(new List <Node>(height)); tempNode = new Node(x, 0); _grid[x].Add(tempNode); tempEdge = new Edge(null, _grid[x][0], true, false, false); tempEdge = new Edge(_grid[x - 1][0], _grid[x][0], false); for (int y = 1; y < height - 1; y++) { tempNode = new Node(x, y); _grid[x].Add(tempNode); tempEdge = new Edge(_grid[x][y - 1], _grid[x][y], true); tempEdge = new Edge(_grid[x - 1][y], _grid[x][y], false); } tempNode = new Node(x, height - 1); _grid[x].Add(tempNode); tempEdge = new Edge(_grid[x][height - 2], _grid[x][height - 1], true); tempEdge = new Edge(_grid[x][height - 1], null, true, false, false); tempEdge = new Edge(_grid[x - 1][height - 1], _grid[x][height - 1], false); } for (int y = 0; y < height; y++) { tempEdge = new Edge(_grid[width - 1][y], null, false, false, false); } for (int y = 0; y < _height; y++) { _startingNodes.Add(_grid[0][y]); } _goal = _grid[_width - 1][Mathf.RoundToInt(_height / 2)]; }
public Map(string file, GameManager.FitnessFunctions fit = GameManager.FitnessFunctions.IntersectionScheme) // read map from file { _startingNodes = new List <Node>(); _fitnessFunction = fit; StreamReader sr = new StreamReader(file); try { string cur = sr.ReadLine(); char[] splits = new char[] { '\t', ' ', 'O', 'S' }; string[] lineSplit = cur.Split(splits); Edge tempEdge; _width = int.Parse(lineSplit[0]); _height = int.Parse(lineSplit[1]); _grid = new List <List <Node> >(); for (int x = 0; x < _width; x++) { _grid.Add(new List <Node>()); for (int y = 0; y < _height; y++) { _grid[x].Add(new Node(x, y)); } } cur = sr.ReadLine(); lineSplit = cur.Split(splits); _goal = _grid[int.Parse(lineSplit[0])][int.Parse(lineSplit[1])]; cur = sr.ReadLine(); lineSplit = cur.Split(splits); for (int x = 0; x < _width; x++) { tempEdge = new Edge(_grid[x][_height - 1], null, true, SymbToPass(lineSplit[x + 1]), SymbToPerc(lineSplit[x + 1])); } for (int y = _height - 1; y > 0; y--) { cur = sr.ReadLine(); lineSplit = cur.Split(splits); tempEdge = new Edge(null, _grid[0][y], false, SymbToPass(lineSplit[0]), SymbToPerc(lineSplit[0])); for (int x = 1; x < _width; x++) { tempEdge = new Edge(_grid[x - 1][y], _grid[x][y], false, SymbToPass(lineSplit[x]), SymbToPerc(lineSplit[x])); if (cur.Substring((x - 1) * 2 + 1, 1) == "S") { _startingNodes.Add(_grid[x - 1][y]); } } tempEdge = new Edge(_grid[_width - 1][y], null, false, SymbToPass(lineSplit[_width]), SymbToPerc(lineSplit[_width])); cur = sr.ReadLine(); lineSplit = cur.Split(splits); for (int x = 0; x < _width; x++) { tempEdge = new Edge(_grid[x][y - 1], _grid[x][y], true, SymbToPass(lineSplit[x + 1]), SymbToPerc(lineSplit[x + 1])); } } cur = sr.ReadLine(); lineSplit = cur.Split(splits); tempEdge = new Edge(null, _grid[0][0], false, SymbToPass(lineSplit[0]), SymbToPerc(lineSplit[0])); for (int x = 1; x < _width; x++) { tempEdge = new Edge(_grid[x - 1][0], _grid[x][0], false, SymbToPass(lineSplit[x]), SymbToPerc(lineSplit[x])); if (cur.Substring((x - 1) * 2 + 1, 1) == "S") { _startingNodes.Add(_grid[x - 1][0]); } } tempEdge = new Edge(_grid[_width - 1][0], null, false, SymbToPass(lineSplit[_width]), SymbToPerc(lineSplit[_width])); cur = sr.ReadLine(); lineSplit = cur.Split(splits); for (int x = 0; x < _width; x++) { tempEdge = new Edge(null, _grid[x][0], true, SymbToPass(lineSplit[x + 1]), SymbToPerc(lineSplit[x + 1])); } } catch (System.Exception ex) { throw new MapFileException(ex.Message, sr); } }