buildGridFromRandom method

Grid? buildGridFromRandom(
  1. Grid? grid,
  2. int cellsRemaining
)

Modifies grid according to random pattern and cellsRemaining (clues)

Implementation

Grid? buildGridFromRandom(Grid? grid, int cellsRemaining) {
  /// Catch for those trying to generate illegal grids
  if (cellsRemaining > 80 || cellsRemaining < 1) {
    throw new InvalidPatternException("Cannot generate random grid with "
        "$cellsRemaining cells remaining (min: 1, max: 80)");
  }

  Position pos;
  List<int> cellIndices = [];

  for (int i = 0; i < 81; i++) {
    cellIndices.add(i);
  }
  cellIndices.shuffle();

  /// Randomly pull index val. between [0, 80] and clear that cell
  while (cellIndices.length != cellsRemaining) {
    pos = new Position(index: cellIndices.removeLast());
    grid!.matrix()![pos.grid!.x as int][pos.grid!.y as int].clear();
  }

  return grid;
}