Add timer

This commit is contained in:
Christian Basler
2025-02-27 20:15:28 +01:00
committed by Christian Basler
parent 7b8d5cb244
commit f9dc62d148
8 changed files with 128 additions and 20 deletions

View File

@@ -7,6 +7,9 @@ class Game(
val horizontalClues = clues.filterIsInstance<HorizontalClue>()
val verticalClues = clues.filterIsInstance<SameColumnClue<ItemClass<*>, ItemClass<*>>>()
private val onStartListeners = mutableListOf<() -> Unit>()
private val onSolvedListeners = mutableListOf<() -> Unit>()
init {
for (position in clues.filterIsInstance<PositionClue<ItemClass<*>>>()) {
val row = grid[position.item.itemType.companion]
@@ -18,6 +21,29 @@ class Game(
}
}
}
grid.forEach { row ->
row.forEach { cell ->
cell.optionsRemovedListeners.add {
if (onStartListeners.isNotEmpty()) {
onStartListeners.forEach { it() }
onStartListeners.clear()
}
}
}
row.onSolved {
if (onSolvedListeners.isNotEmpty() && isSolved) {
onSolvedListeners.forEach { it() }
}
}
}
}
fun onStart(listener: () -> Unit) {
onStartListeners.add(listener)
}
fun onSolved(listener: () -> Unit) {
onSolvedListeners.add(listener)
}
/**

View File

@@ -6,13 +6,22 @@ class GameRow<C : ItemClass<C>>(
val cells: List<GameCell<C>>
) : List<GameCell<C>> by cells {
var isSolved = false
private set
private set(value) {
field = value
if (value) {
onSolvedListener()
}
}
var onSolvedListener = {}
fun onSolved(listener: () -> Unit) {
onSolvedListener = listener
}
fun indexOf(element: C) = indexOfFirst { it.selection?.itemType == element }
fun cleanupOptions() {
if (
isSolved && all {
if (isSolved && all {
it.solution != null
&& it.options.size == 1
&& it.options.single() == it.selection