Improve domain (WIP)
This commit is contained in:
@@ -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 }
|
||||
}
|
||||
Reference in New Issue
Block a user