Generate game
This commit is contained in:
@@ -1,27 +1,39 @@
|
||||
package domain
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
class Game(
|
||||
val grid: Grid,
|
||||
val rules: List<GameRule>
|
||||
val clues: List<Clue>
|
||||
) {
|
||||
val horizontalRules = rules.filterIsInstance<HorizontalRule>()
|
||||
val verticalRules = rules.filterIsInstance<VerticalRule>()
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fun areCategoriesValid(): Boolean {
|
||||
val usedCategories = mutableSetOf<KClass<out ItemClass>>()
|
||||
val usedCategories = mutableSetOf<ItemClassCompanion<*>>()
|
||||
for (row in grid.rows) {
|
||||
val category = row.first().options.first()::class
|
||||
if (usedCategories.contains(category)) {
|
||||
if (usedCategories.contains(row.category)) {
|
||||
return false
|
||||
}
|
||||
usedCategories.add(category)
|
||||
usedCategories.add(row.category)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fun areRulesViolated(): Boolean = rules
|
||||
fun areRulesViolated(): Boolean = clues
|
||||
.map { it.isRuleViolated(grid) }
|
||||
.reduce { a, b -> a || b }
|
||||
}
|
||||
Reference in New Issue
Block a user