Files
YAEP/composeApp/src/commonMain/kotlin/domain/Game.kt

39 lines
1.1 KiB
Kotlin
Raw Normal View History

2024-06-11 11:04:02 +02:00
package domain
2024-06-20 17:37:06 +02:00
import androidx.compose.ui.util.fastAny
2024-06-11 11:04:02 +02:00
class Game(
val grid: Grid,
2024-06-13 17:37:33 +02:00
val clues: List<Clue>
2024-06-11 11:04:02 +02:00
) {
2024-06-13 17:37:33 +02:00
val horizontalClues = clues.filterIsInstance<HorizontalClue>()
val verticalClues = clues.filterIsInstance<SameRowClue<ItemClass<*>>>()
val positionalClues = clues.filterIsInstance<PositionClue<ItemClass<*>>>()
init {
for (position in positionalClues) {
val row = grid[position.item.itemType.companion]
row.forEachIndexed { index, gameCell ->
if (index == position.index) {
gameCell.selection = position.item
} else {
gameCell.options.remove(position.item)
}
}
}
}
2024-06-12 17:01:49 +02:00
fun areCategoriesValid(): Boolean {
2024-06-13 17:37:33 +02:00
val usedCategories = mutableSetOf<ItemClassCompanion<*>>()
2024-06-12 17:01:49 +02:00
for (row in grid.rows) {
2024-06-13 17:37:33 +02:00
if (usedCategories.contains(row.category)) {
2024-06-12 17:01:49 +02:00
return false
}
2024-06-13 17:37:33 +02:00
usedCategories.add(row.category)
2024-06-11 11:04:02 +02:00
}
2024-06-12 17:01:49 +02:00
return true
}
2024-06-11 11:04:02 +02:00
2024-06-13 17:37:33 +02:00
fun areRulesViolated(): Boolean = clues
2024-06-20 17:37:06 +02:00
.any { it.isRuleViolated(grid) }
2024-06-11 11:04:02 +02:00
}