Add improvements
There is an issue related to the new code finding single options
This commit is contained in:
@@ -8,18 +8,49 @@ class GameRow<C : ItemClass<C>>(
|
||||
fun indexOf(element: C) = indexOfFirst { it.selection?.itemType == element }
|
||||
|
||||
fun cleanupOptions() {
|
||||
cells.forEach { cleanupOptions(it) }
|
||||
cells.forEach {
|
||||
cleanupOptions(it)
|
||||
}
|
||||
// do {
|
||||
// var selectedSingleOption = false
|
||||
// cells.forEach { cleanupOptions(it) }
|
||||
// val selections = cells.mapNotNull { it.selection }
|
||||
// options
|
||||
// .filter { !selections.contains(it) }
|
||||
// .forEach { option ->
|
||||
// if (cells.count { cell -> cell.options.contains(option) } == 1) {
|
||||
// cells
|
||||
// .filter { it.selection == null }
|
||||
// .first { cell -> cell.options.contains(option) }
|
||||
// .let { it.selection = option }
|
||||
// selectedSingleOption = true
|
||||
// }
|
||||
// }
|
||||
// } while (selectedSingleOption)
|
||||
}
|
||||
|
||||
private fun cleanupOptions(cell: GameCell<C>, firstCall: Boolean = true) {
|
||||
if ((firstCall && cell.selection != null) || (cell.options.size == 1 && cell.selection == null)) {
|
||||
if (cell.selection == null) {
|
||||
private fun cleanupOptions(cell: GameCell<C>, justSelected: Boolean = true) {
|
||||
if ((justSelected && cell.selection != null) || (cell.options.size == 1 && cell.selection == null)) {
|
||||
val selection = cell.selection
|
||||
if (selection == null) {
|
||||
cell.selection = cell.options.first()
|
||||
} else {
|
||||
cell.options.clear()
|
||||
cell.options.add(selection)
|
||||
}
|
||||
filter { otherCell -> otherCell != cell && otherCell.selection == null }.forEach { otherCell ->
|
||||
filter { otherCell -> otherCell != cell && otherCell.hasNoSelection() }.forEach { otherCell ->
|
||||
otherCell.options.remove(cell.selection)
|
||||
cleanupOptions(otherCell, false)
|
||||
}
|
||||
filter { it.selection == null }
|
||||
.flatMap { c -> c.options.map { o -> o to c } }
|
||||
.groupBy { it.first }
|
||||
.filter { it.value.size == 1 }
|
||||
.forEach {
|
||||
val c = it.value.single().second
|
||||
c.selection = it.key
|
||||
cleanupOptions(c, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,10 +55,9 @@ class NeighbourClue<A : ItemClass<A>, B : ItemClass<B>>(val a: Item<A>, val b: I
|
||||
|
||||
for (iX in rowX.indices) {
|
||||
val cellX = rowX[iX]
|
||||
if (cellX.options.contains(x)) {
|
||||
if (cellX.mayBe(x, mayHaveSelection = false)) {
|
||||
if (!rowY.getOrNull(iX - 1).mayBe(y) && !rowY.getOrNull(iX + 1).mayBe(y)) {
|
||||
cellX.options.remove(x)
|
||||
removed = true
|
||||
removed = cellX.options.remove(x) || removed
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,8 +101,11 @@ class OrderClue<L : ItemClass<L>, R : ItemClass<R>>(val left: Item<L>, val right
|
||||
var removed = false
|
||||
try {
|
||||
rowL.takeLast(rowL.size - lastR)
|
||||
.filter { it.selection == null }
|
||||
.forEach { removed = it.options.remove(left) || removed }
|
||||
rowR.take(firstL + 1).forEach { removed = it.options.remove(right) || removed }
|
||||
rowR.take(firstL + 1)
|
||||
.filter { it.selection == null }
|
||||
.forEach { removed = it.options.remove(right) || removed }
|
||||
} catch (e: IllegalArgumentException) {
|
||||
throw UnsolvablePuzzleException(e)
|
||||
}
|
||||
@@ -274,11 +276,15 @@ class SameColumnClue<A : ItemClass<A>, B : ItemClass<B>>(val a: Item<A>, val b:
|
||||
val cellA = rowA[i]
|
||||
val cellB = rowB[i]
|
||||
|
||||
if (!cellA.mayBe(a)) {
|
||||
removed = cellB.options.remove(b) || removed
|
||||
if (cellB.hasNoSelection()) {
|
||||
if (!cellA.mayBe(a)) {
|
||||
removed = cellB.options.remove(b) || removed
|
||||
}
|
||||
}
|
||||
if (!cellB.mayBe(b)) {
|
||||
removed = cellA.options.remove(a) || removed
|
||||
if (cellA.hasNoSelection()) {
|
||||
if (!cellB.mayBe(b)) {
|
||||
removed = cellA.options.remove(a) || removed
|
||||
}
|
||||
}
|
||||
}
|
||||
return removed
|
||||
|
||||
@@ -29,6 +29,7 @@ class GameTest {
|
||||
val time = measureTime {
|
||||
game = generateGame()
|
||||
}
|
||||
println("Generated game #$i in ${time.inWholeMilliseconds}ms")
|
||||
expect(solve(game.grid, game.clues)).toEqual(SOLVABLE)
|
||||
expect(time).toBeLessThan(500.milliseconds)
|
||||
if (time < fastest) {
|
||||
|
||||
Reference in New Issue
Block a user