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 }
}

View File

@@ -1,8 +1,10 @@
package domain
fun generateGame(): Game {
fun generateGame(size: Int = 6): Game {
// Here's a simple algorithm making use of your solver:
// 0. Select $size classes and $size items per class.
// 1. Generate a random puzzle instance.
// 2. Build a set C of all possible clues that pertain to this puzzle instance. (There are a finite and in fact quite small number of possible clues: for example if there are 5 houses, there are 5 possible clues of the form "Person A lives in house B", 8 possible clues of the form "Person A lives next to house B", and so on.)
// 3. Pick a random permutation c1, c2, ..., cn of the clues in C.
// 4. Set i = 1.

View File

@@ -1,21 +1,21 @@
package domain
class GameRow(
val category: ItemCategory,
val cells: List<GameCell>
) : List<GameCell> by cells
class GameRow<C : ItemClass>(
val category: C,
val cells: List<GameCell<C>>
) : List<GameCell<C>> by cells
class Grid(
val rows: List<GameRow>
) : List<GameRow> by rows {
val rows: List<GameRow<*>>
) : List<GameRow<*>> by rows {
fun indexOf(element: ItemClass): Int {
val row = rows.first { it.category == element.category }
val row = rows.first { it.category == element::class }
return row.indexOfFirst { it.selection == element }
}
}
class GameCell(
var selection: ItemClass?,
val solution: ItemClass,
val options: List<ItemClass>
class GameCell<C:ItemClass>(
var selection: C?,
val solution: C,
val options: List<C>
)