Grid.fromString(String stringGrid)

Initializes a Grid from a String.

Throws an ArgumentError if the argument stringGrid is not a String, or if it's an empty string (""), or if the matrix parsed from it is not rectangular.

Source

factory Grid.fromString(String stringGrid) {
  if (stringGrid is! String) {
    throw new ArgumentError('Argument `stringGrid` must be of type String');
  } else if (stringGrid == "") {
    throw new ArgumentError('Argument `stringGrid` cannot be "" (empty string)!');
  }
  stringGrid = stringGrid.trim();

  List<String> stringRows = stringGrid.split('\n');
  List<List<bool>> boolGrid = new List<List<bool>>();

  for (String stringRow in stringRows) {
    List<bool> boolRow = new List<bool>();

    for (int i = 0; i < stringRow.length; i++) {
      switch (stringRow[i]) {
        case "o": // passable / walkable
          boolRow.add(true);
          break;
        case "x": // impassable / unwalkable
          boolRow.add(false);
          break;
        default: // Default to impassable / unwalkable
          boolRow.add(false);
          break;
      }
    }

    boolGrid.add(boolRow);
  }

  return new Grid(boolGrid);
}