addRow method

List<Cell> addRow(
  1. LevelConfig level,
  2. List<Cell> current
)

Implementation

List<Cell> addRow(LevelConfig level, List<Cell> current) {
    final List<Cell> next = List.from(current);
    int targetRow = -1;
    for (int r = level.rows - 1; r >= 0; r--) {
      final rowValues = next.where((c) => c.row == r).toList();
      final isEmptyRow = rowValues.every((c) => c.value == null);
      if (isEmptyRow) {
        targetRow = r;
        break;
      }
    }
    if (targetRow == -1) {
      for (int r = 0; r < level.rows; r++) {
        final rowValues = next.where((c) => c.row == r).toList();
        final hasNull = rowValues.any((c) => c.value == null);
        if (hasNull) {
          targetRow = r;
          break;
        }
      }
    }
    if (targetRow == -1) {
      return next;
    }


    for (int c = 0; c < level.cols; c++) {
      final idx = targetRow * level.cols + c;
      final old = next[idx];
      next[idx] = old.copyWith(value: LevelPresets.randomAllowed(level.allowedNumbers, _rng), matched: false);
    }
    return next;
  }