Improve domain (WIP)

This commit is contained in:
Christian Basler
2024-06-12 17:01:49 +02:00
parent 60204f3b04
commit baa371ac51
3 changed files with 30 additions and 24 deletions

View File

@@ -1,23 +1,27 @@
package domain
const val PUZZLE_SIZE = 6
import kotlin.reflect.KClass
class Game(
val categories: List<ItemCategory>,
val grid: Grid,
val horizontalRules: List<HorizontalRule>,
val verticalRules: List<VerticalRule>
val rules: List<GameRule>
) {
fun areCategoriesValid(): Boolean = categories.mapIndexed { index, category ->
category.any { item ->
categories.filterIndexed { i, _ -> i > index }.any { it.contains(item) }
val horizontalRules = rules.filterIsInstance<HorizontalRule>()
val verticalRules = rules.filterIsInstance<VerticalRule>()
fun areCategoriesValid(): Boolean {
val usedCategories = mutableSetOf<KClass<out ItemClass>>()
for (row in grid.rows) {
val category = row.first().options.first()::class
if (usedCategories.contains(category)) {
return false
}
usedCategories.add(category)
}
}.none { it }
return true
}
fun areRulesViolated(): Boolean = horizontalRules
.map { it.isRuleViolated(grid) }
.reduce { a, b -> a || b }
|| verticalRules
fun areRulesViolated(): Boolean = rules
.map { it.isRuleViolated(grid) }
.reduce { a, b -> a || b }
}