Add and improve domain (WIP)

This commit is contained in:
2024-06-11 11:04:02 +02:00
parent 1b9c8633c9
commit f891dea885
11 changed files with 232 additions and 29 deletions

View File

@@ -0,0 +1,21 @@
package domain
class Game(
val categories: List<ItemCategory>,
val grid: Grid,
val horizontalRules: List<HorizontalRule>,
val verticalRules: List<VerticalRule>
) {
fun areCategoriesValid(): Boolean = categories.mapIndexed { index, category ->
category.any { item ->
categories.filterIndexed { i, _ -> i > index }.any { it.contains(item) }
}
}.none { it }
fun areRulesViolated(): Boolean = horizontalRules
.map { it.isRuleViolated(grid) }
.reduce { a, b -> a || b }
|| verticalRules
.map { it.isRuleViolated(grid) }
.reduce { a, b -> a || b }
}

View File

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

View File

@@ -0,0 +1,44 @@
@file:OptIn(ExperimentalResourceApi::class)
package domain
import org.jetbrains.compose.resources.DrawableResource
import org.jetbrains.compose.resources.ExperimentalResourceApi
fun category(block: CategoryContext.() -> Unit): ItemCategory {
class CategoryImpl(val items: MutableList<ItemImpl>) :
ItemCategory, List<Item> by items {
inner class ItemImpl(
override val image: DrawableResource
) : Item {
override val category: CategoryImpl = this@CategoryImpl
init {
items.add(this)
}
}
}
val ctx = object : CategoryContext {
val category = CategoryImpl(mutableListOf())
override fun item(image: DrawableResource) {
category.ItemImpl(image)
}
}
ctx.block()
return ctx.category
}
interface CategoryContext {
fun item(image: DrawableResource)
}
interface Item {
val category: ItemCategory
@OptIn(ExperimentalResourceApi::class)
val image: DrawableResource
}
interface ItemCategory : List<Item>

View File

@@ -0,0 +1,69 @@
package domain
import kotlin.math.abs
sealed class GameRule {
abstract fun isRuleViolated(grid: Grid): Boolean
}
sealed class HorizontalRule : GameRule()
class NeighbourRule(val a: Item, val b: Item) : HorizontalRule() {
override fun isRuleViolated(grid: Grid): Boolean {
val ia = grid.indexOf(a)
val ib = grid.indexOf(b)
if (ia == -1 || ib == -1) return false
return abs(ia - ib) != 1
}
}
class OrderRule(val left: Item, val right: Item) : HorizontalRule() {
override fun isRuleViolated(grid: Grid): Boolean {
val il = grid.indexOf(left)
val ir = grid.indexOf(right)
if (il == -1 || ir == -1) return false
return ir <= il
}
}
class TripletRule(val a: Item, val b: Item, val c: Item) : HorizontalRule() {
private fun isNeighbourRuleViolated(ix: Int, iy: Int): Boolean {
if (ix == -1 || iy == -1) return false
return abs(ix - iy) != 1
}
override fun isRuleViolated(grid: Grid): Boolean {
val ia = grid.indexOf(a)
val ib = grid.indexOf(b)
val ic = grid.indexOf(c)
if (ia == -1 && ic == -1) {
return false
}
if (ia == ib) {
return true
}
if (isNeighbourRuleViolated(ia, ib) || isNeighbourRuleViolated(ib, ic)) {
return true
}
return false
}
}
class VerticalRule(val a: Item, val b: Item) : GameRule() {
override fun isRuleViolated(grid: Grid): Boolean {
val ia = grid.indexOf(a)
val ib = grid.indexOf(b)
if (ia == -1 || ib == -1) return false
return ia != ib
}
}