From 7190e4faef0d78aaa7466256fba68cffbb4b837f Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Tue, 16 Jul 2024 05:14:59 +0200 Subject: [PATCH] Add TODOs --- .../kotlin/ch/dissem/yaep/domain/generator.kt | 36 ++----------------- 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/domain/src/main/kotlin/ch/dissem/yaep/domain/generator.kt b/domain/src/main/kotlin/ch/dissem/yaep/domain/generator.kt index 5f7ba53..00d35fe 100644 --- a/domain/src/main/kotlin/ch/dissem/yaep/domain/generator.kt +++ b/domain/src/main/kotlin/ch/dissem/yaep/domain/generator.kt @@ -24,6 +24,7 @@ fun generateGame(size: Int = 6): Game { // If i >= n, we are done. The set of clues is minimal. while (i < clues.size) { // Run your solver on the reduced set of clues and count the number of possible solutions. + // TODO: first do binary search val temp = clues - clues[i] if (solve(grid.toGrid(), temp) == SOLVABLE) { // If there is exactly one solution, update clues and reset index. @@ -63,6 +64,7 @@ internal fun solve( } catch (e: UnsolvablePuzzleException) { return NO_SOLUTION } + // TODO clean up groups in rows (?) grid.forEach { row -> row.forEach { cell -> row.cleanupOptions(cell) } } // grid.forEach { row -> // removedOptions = row.tryOptionsForClues(grid, otherClues) || removedOptions @@ -88,46 +90,12 @@ internal fun solve( return MULTIPLE_SOLUTIONS } -internal fun > GameCell.countSolutions( - grid: Grid, - clues: Collection -): Int { - val options = options.toList() - var solvableCount = 0 - for (option in options) { - selection = option - if (solve(grid, clues) == SOLVABLE) { - solvableCount++ - } - } - selection = null - this.options.addAll(options) - - return solvableCount -} - internal enum class PuzzleSolution { NO_SOLUTION, SOLVABLE, MULTIPLE_SOLUTIONS } -fun > GameRow.tryOptionsForClues(grid: Grid, clues: List): Boolean { - var removedOptions = false - filter { cell -> cell.selection == null }.forEach { cell -> - val removed = cell.options.removeIf { option -> - cell.selection = option - clues.any { it.isRuleViolated(grid) } - } - cell.selection = null - if (removed) { - cleanupOptions(cell) - } - removedOptions = removedOptions || removed - } - return removedOptions -} - fun > GameRow.cleanupOptions(cell: GameCell) { if (cell.options.size == 1 && cell.selection == null) { cell.selection = cell.options.first()