From baa371ac5126a16aabe9d39188e0298112eb32e6 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Wed, 12 Jun 2024 17:01:49 +0200 Subject: [PATCH] Improve domain (WIP) --- .../src/commonMain/kotlin/domain/Game.kt | 28 +++++++++++-------- .../src/commonMain/kotlin/domain/generator.kt | 4 ++- .../src/commonMain/kotlin/domain/grid.kt | 22 +++++++-------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/domain/Game.kt b/composeApp/src/commonMain/kotlin/domain/Game.kt index 9816de7..bd73a7a 100644 --- a/composeApp/src/commonMain/kotlin/domain/Game.kt +++ b/composeApp/src/commonMain/kotlin/domain/Game.kt @@ -1,23 +1,27 @@ package domain -const val PUZZLE_SIZE = 6 +import kotlin.reflect.KClass class Game( - val categories: List, val grid: Grid, - val horizontalRules: List, - val verticalRules: List + val rules: List ) { - fun areCategoriesValid(): Boolean = categories.mapIndexed { index, category -> - category.any { item -> - categories.filterIndexed { i, _ -> i > index }.any { it.contains(item) } + val horizontalRules = rules.filterIsInstance() + val verticalRules = rules.filterIsInstance() + + fun areCategoriesValid(): Boolean { + val usedCategories = mutableSetOf>() + 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 } } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/domain/generator.kt b/composeApp/src/commonMain/kotlin/domain/generator.kt index 595dee7..81fd7f5 100644 --- a/composeApp/src/commonMain/kotlin/domain/generator.kt +++ b/composeApp/src/commonMain/kotlin/domain/generator.kt @@ -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. diff --git a/composeApp/src/commonMain/kotlin/domain/grid.kt b/composeApp/src/commonMain/kotlin/domain/grid.kt index bea6955..b89a772 100644 --- a/composeApp/src/commonMain/kotlin/domain/grid.kt +++ b/composeApp/src/commonMain/kotlin/domain/grid.kt @@ -1,21 +1,21 @@ package domain -class GameRow( - val category: ItemCategory, - val cells: List -) : List by cells +class GameRow( + val category: C, + val cells: List> +) : List> by cells class Grid( - val rows: List -) : List by rows { + val rows: List> +) : List> 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 +class GameCell( + var selection: C?, + val solution: C, + val options: List )