Fix error
where a puzzle is considered solved as soon as all cells have a selection.
This commit is contained in:
@@ -1,15 +1,31 @@
|
||||
package ch.dissem.yaep.domain
|
||||
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
|
||||
class Game(
|
||||
val grid: Grid,
|
||||
val clues: Collection<Clue>
|
||||
) {
|
||||
val log = KotlinLogging.logger { }
|
||||
val horizontalClues = clues.filterIsInstance<HorizontalClue>()
|
||||
val verticalClues = clues.filterIsInstance<SameColumnClue<ItemClass<*>, ItemClass<*>>>()
|
||||
|
||||
private val onStartListeners = mutableListOf<() -> Unit>()
|
||||
private val onSolvedListeners = mutableListOf<() -> Unit>()
|
||||
|
||||
/**
|
||||
* Make sure that no category is used more than once.
|
||||
*/
|
||||
val areCategoriesValid: Boolean
|
||||
get() = grid.rows.map { it.category }.distinct().size == grid.rows.size
|
||||
|
||||
val isValid: Boolean
|
||||
get() = areCategoriesValid && clues.all { it.isValid(grid) }
|
||||
|
||||
val isSolved: Boolean
|
||||
get() = grid.rows.all { it.isSolved } && isValid
|
||||
|
||||
|
||||
init {
|
||||
for (position in clues.filterIsInstance<PositionClue<ItemClass<*>>>()) {
|
||||
val row = grid[position.item.itemType.companion]
|
||||
@@ -32,7 +48,10 @@ class Game(
|
||||
}
|
||||
}
|
||||
row.onSolved {
|
||||
log.debug { "Is game valid: $isValid" }
|
||||
log.debug { "Is game solved: $isSolved" }
|
||||
if (onSolvedListeners.isNotEmpty() && isSolved) {
|
||||
log.debug { "Game solved, notifying listeners" }
|
||||
onSolvedListeners.forEach { it() }
|
||||
}
|
||||
}
|
||||
@@ -48,18 +67,6 @@ class Game(
|
||||
onSolvedListeners.add(listener)
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that no category is used more than once.
|
||||
*/
|
||||
fun areCategoriesValid(): Boolean {
|
||||
return grid.rows.map { it.category }.distinct().size == grid.rows.size
|
||||
}
|
||||
|
||||
fun isValid(): Boolean = areCategoriesValid() && clues.all { it.isValid(grid) }
|
||||
|
||||
val isSolved: Boolean
|
||||
get() = grid.rows.all { it.isSolved }
|
||||
|
||||
override fun toString(): String {
|
||||
return grid.toString() + "\n\n" + clues.joinToString("\n* ", prefix = "* ")
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[versions]
|
||||
agp = "8.10.0"
|
||||
agp = "8.10.1"
|
||||
jdk = "21"
|
||||
android-compileSdk = "35"
|
||||
android-minSdk = "24"
|
||||
|
||||
Reference in New Issue
Block a user