Add minor improvements

This commit is contained in:
2024-07-02 23:11:53 +02:00
parent d33d80e875
commit 5b97b789a5
7 changed files with 32 additions and 19 deletions

View File

@@ -41,6 +41,8 @@ class NeighbourClue<A : ItemClass<A>, B : ItemClass<B>>(val a: Item<A>, val b: I
}
return true
}
override fun toString() = "$aType is next to $bType"
}
class OrderClue<L : ItemClass<L>, R : ItemClass<R>>(val left: Item<L>, val right: Item<R>) :
@@ -66,6 +68,8 @@ class OrderClue<L : ItemClass<L>, R : ItemClass<R>>(val left: Item<L>, val right
return rowLeft.indexOfFirst { it.mayBe(left) } >= rowRight.indexOfLast { it.mayBe(right) }
}
override fun toString() = "$leftType is left of $rightType"
}
class TripletClue<A : ItemClass<A>, B : ItemClass<B>, C : ItemClass<C>>(
@@ -152,6 +156,9 @@ class TripletClue<A : ItemClass<A>, B : ItemClass<B>, C : ItemClass<C>>(
}
return true
}
override fun toString(): String =
"$bType is between the neighbours $aType and $cType to both sides"
}
class SameColumnClue<A : ItemClass<A>, B : ItemClass<B>>(val a: Item<A>, val b: Item<B>) : Clue() {
@@ -184,13 +191,19 @@ class SameColumnClue<A : ItemClass<A>, B : ItemClass<B>>(val a: Item<A>, val b:
}
return true
}
override fun toString(): String = "$aType and $bType are in the same column"
}
class PositionClue<C : ItemClass<C>>(val item: Item<C>, val index: Int) : Clue() {
val itemType = item.itemType
override fun isRuleViolated(grid: Grid): Boolean {
val i = grid.indexOf(item.itemType)
if (i != -1) return i != index
return grid[item].mayBe(item)
}
override fun toString() = "$itemType is at position $index"
}

View File

@@ -19,7 +19,7 @@ fun generateGame(size: Int = 6): Game {
// (There are a finite and in fact quite small number of possible clues: for example
// if there are 5 houses, there are 5 possible clues of the form "Person A lives in
// house B", 8 possible clues of the form "Person A lives next to house B", and so on.)
var clues = getAllClues(grid).toList() //.shuffled()
var clues = getAllClues(grid).sortedBy { it.toString() } //.shuffled()
var i = 0
@@ -47,9 +47,9 @@ private fun solve(
// First, set the positions of the items that are already known.
clues.filterIsInstance<PositionClue<ItemClass<*>>>().forEach { position ->
val row = grid[position.item.itemType.companion]
val row = grid[position.itemType.companion]
val cell = row[position.index]
cell.options.retainAll { it == cell.selection }
cell.options.retainAll { it == position.item }
row.cleanupOptions(cell)
}