Maze.parse constructor

Maze.parse(
  1. String map
)

Implementation

factory Maze.parse(String map) {
  final tiles = <List<Tile>>[];
  final rows = map.trim().split('\n');
  Tile? start;
  Tile? goal;

  for (var rowNum = 0; rowNum < rows.length; rowNum++) {
    final row = <Tile>[];
    final lineTiles = rows[rowNum].trim().split('');

    for (var colNum = 0; colNum < lineTiles.length; colNum++) {
      final t = lineTiles[colNum];
      final obstacle = t == 'x';
      final tile = Tile(colNum, rowNum, obstacle: obstacle);
      if (t == 's') {
        start = tile;
      }
      if (t == 'g') {
        goal = tile;
      }
      row.add(tile);
    }

    tiles.add(row);
  }

  // TODO: Error handling for invalid strings, including null start/goal.
  return Maze(tiles, start!, goal!);
}