Add TODOs
This commit is contained in:
@@ -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 <C : ItemClass<C>> GameCell<C>.countSolutions(
|
||||
grid: Grid,
|
||||
clues: Collection<Clue>
|
||||
): 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 <C : ItemClass<C>> GameRow<C>.tryOptionsForClues(grid: Grid, clues: List<Clue>): 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 <C : ItemClass<C>> GameRow<C>.cleanupOptions(cell: GameCell<C>) {
|
||||
if (cell.options.size == 1 && cell.selection == null) {
|
||||
cell.selection = cell.options.first()
|
||||
|
||||
Reference in New Issue
Block a user