public void AddNeighbor(Square neighbor) { // TODO: Error handling on neighbor already exists in list if (!neighbors.Contains(neighbor)) { neighbors.Add(neighbor); } }
public Board(int rows, int cols) { this.rows = rows; this.cols = cols; board = new Square[rows, cols]; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { board[r, c] = new Square(new Coordinate(r, c)); } } for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { if (r > 0) board[r, c].AddNeighbor(board[r - 1, c]); if (r < rows - 1) board[r, c].AddNeighbor(board[r + 1, c]); if (c > 0) board[r, c].AddNeighbor(board[r, c - 1]); if (c < cols - 1) board[r, c].AddNeighbor(board[r, c + 1]); } } }
public Board(string filename) { TextReader tr = new StreamReader(filename); rows = int.Parse(tr.ReadLine()); cols = int.Parse(tr.ReadLine()); board = new Square[rows, cols]; string line; int row = 0; while ((line = tr.ReadLine()) != null) { int col= 0; foreach (char ch in line.Trim()) { Coordinate coordinate = new Coordinate(row, col); MapElement element; switch (ch) { case 'X': element = new Wall(coordinate); break; case '~': element = new Water(coordinate); break; case '.': element = new Floor(coordinate); break; default: element = new Floor(coordinate); break; } Square square = new Square(coordinate, element); board[row, col] = square; col++; } row++; cols = col; } rows = row; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { if (r > 0) { Square neighbor = board[r - 1, c]; board[r, c].AddNeighbor(neighbor); } if (r < rows - 1) { Square neighbor = board[r + 1, c]; board[r, c].AddNeighbor(neighbor); } if (c > 0) { Square neighbor = board[r, c - 1]; board[r, c].AddNeighbor(neighbor); } if (c < cols - 1) { Square neighbor = board[r, c+ 1]; board[r, c].AddNeighbor(neighbor); } } } tr.Close(); }
public void AddNeighbor(Square neighbor) { // TODO: Error handling on neighbor already exists in list neighbors.Add(neighbor); }