factory Field()

in lib/src/game/field.dart [13:33]


  factory Field([int bombCount = 40, int cols = 16, int rows = 16, int? seed]) {
    final squares = List<bool>.filled(rows * cols, false);
    assert(bombCount < squares.length);
    assert(bombCount > 0);

    final rnd = Random(seed);

    // This is the most simple code, but it'll get slow as
    // bombCount approaches the square count.
    // But more efficient if bombCount << square count
    // which is expected.
    for (var i = 0; i < bombCount; i++) {
      int index;
      do {
        index = rnd.nextInt(squares.length);
      } while (squares[index]);
      squares[index] = true;
    }

    return Field._internal(bombCount, cols, squares);
  }