Improve domain (WIP)
This commit is contained in:
@@ -1,23 +1,27 @@
|
|||||||
package domain
|
package domain
|
||||||
|
|
||||||
const val PUZZLE_SIZE = 6
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
class Game(
|
class Game(
|
||||||
val categories: List<ItemCategory>,
|
|
||||||
val grid: Grid,
|
val grid: Grid,
|
||||||
val horizontalRules: List<HorizontalRule>,
|
val rules: List<GameRule>
|
||||||
val verticalRules: List<VerticalRule>
|
|
||||||
) {
|
) {
|
||||||
fun areCategoriesValid(): Boolean = categories.mapIndexed { index, category ->
|
val horizontalRules = rules.filterIsInstance<HorizontalRule>()
|
||||||
category.any { item ->
|
val verticalRules = rules.filterIsInstance<VerticalRule>()
|
||||||
categories.filterIndexed { i, _ -> i > index }.any { it.contains(item) }
|
|
||||||
}
|
|
||||||
}.none { it }
|
|
||||||
|
|
||||||
fun areRulesViolated(): Boolean = horizontalRules
|
fun areCategoriesValid(): Boolean {
|
||||||
.map { it.isRuleViolated(grid) }
|
val usedCategories = mutableSetOf<KClass<out ItemClass>>()
|
||||||
.reduce { a, b -> a || b }
|
for (row in grid.rows) {
|
||||||
|| verticalRules
|
val category = row.first().options.first()::class
|
||||||
|
if (usedCategories.contains(category)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
usedCategories.add(category)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun areRulesViolated(): Boolean = rules
|
||||||
.map { it.isRuleViolated(grid) }
|
.map { it.isRuleViolated(grid) }
|
||||||
.reduce { a, b -> a || b }
|
.reduce { a, b -> a || b }
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package domain
|
package domain
|
||||||
|
|
||||||
fun generateGame(): Game {
|
fun generateGame(size: Int = 6): Game {
|
||||||
// Here's a simple algorithm making use of your solver:
|
// 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.
|
// 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.)
|
// 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.
|
// 3. Pick a random permutation c1, c2, ..., cn of the clues in C.
|
||||||
// 4. Set i = 1.
|
// 4. Set i = 1.
|
||||||
|
|||||||
@@ -1,21 +1,21 @@
|
|||||||
package domain
|
package domain
|
||||||
|
|
||||||
class GameRow(
|
class GameRow<C : ItemClass>(
|
||||||
val category: ItemCategory,
|
val category: C,
|
||||||
val cells: List<GameCell>
|
val cells: List<GameCell<C>>
|
||||||
) : List<GameCell> by cells
|
) : List<GameCell<C>> by cells
|
||||||
|
|
||||||
class Grid(
|
class Grid(
|
||||||
val rows: List<GameRow>
|
val rows: List<GameRow<*>>
|
||||||
) : List<GameRow> by rows {
|
) : List<GameRow<*>> by rows {
|
||||||
fun indexOf(element: ItemClass): Int {
|
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 }
|
return row.indexOfFirst { it.selection == element }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class GameCell(
|
class GameCell<C:ItemClass>(
|
||||||
var selection: ItemClass?,
|
var selection: C?,
|
||||||
val solution: ItemClass,
|
val solution: C,
|
||||||
val options: List<ItemClass>
|
val options: List<C>
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user