gridToString static method

String gridToString(
  1. Grid<Object?> grid, {
  2. String fill = '.',
  3. String empty = '#',
})

Converts a Grid to a string like toString.

Each fill element is represented by a #, and each element in the grid is represented by .. The grid is represented as a series of rows, where each row is separated by a newline character.

Alternate characters can be used by providing the empty and fill parameters:

final grid = Grid.fromRows(
  [
    [Tile.wall, Tile.wall, Tile.wall],
    [Tile.wall, Tile.floor, Tile.wall],
  ],
  empty: Tile.wall,
);
print(grid.toString());

The above code will output:

###
#.#

Implementation

static String gridToString(
  Grid<Object?> grid, {
  String fill = '.',
  String empty = '#',
}) {
  final buffer = StringBuffer();
  for (final row in grid.rows) {
    for (final element in row) {
      buffer.write(element == grid.empty ? empty : fill);
    }
    buffer.writeln();
  }
  return buffer.toString();
}