Maze.random constructor

Maze.random({
  1. int? width,
  2. int? height,
})

Implementation

factory Maze.random({int? width, int? height}) {
  if (width == null) {
    throw ArgumentError('width must not be null');
  }
  if (height == null) {
    throw ArgumentError('height must not be null');
  }

  final rand = math.Random();
  final tiles = <List<Tile>>[];

  for (var y = 0; y < height; y++) {
    final row = <Tile>[];
    for (var x = 0; x < width; x++) {
      row.add(Tile(x, y, obstacle: rand.nextBool()));
    }
    tiles.add(row);
  }

  return Maze(tiles, tiles[0][0], tiles[height - 1][width - 1]);
}