Add timer
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user