diff --git a/domain/src/main/kotlin/ch/dissem/yaep/domain/GameCell.kt b/domain/src/main/kotlin/ch/dissem/yaep/domain/GameCell.kt new file mode 100644 index 0000000..9b7b9e9 --- /dev/null +++ b/domain/src/main/kotlin/ch/dissem/yaep/domain/GameCell.kt @@ -0,0 +1,14 @@ +package ch.dissem.yaep.domain + +class GameCell>( + var selection: Item?, + val solution: Item, + val options: MutableList> +) +//{ +// val options = options.toMutableStateList() +// var selection by mutableStateOf(selection) +//} + +fun > GameCell?.mayBe(item: Item) = + this != null && (selection == item || (selection == null && options.contains(item))) diff --git a/domain/src/main/kotlin/ch/dissem/yaep/domain/GameRow.kt b/domain/src/main/kotlin/ch/dissem/yaep/domain/GameRow.kt new file mode 100644 index 0000000..32d8808 --- /dev/null +++ b/domain/src/main/kotlin/ch/dissem/yaep/domain/GameRow.kt @@ -0,0 +1,14 @@ +package ch.dissem.yaep.domain + +class GameRow>( + val category: ItemClassCompanion, + val options: List>, + val cells: List> +) : List> by cells { + fun indexOf(element: C) = indexOfFirst { it.selection?.itemType == element } + + fun updateOptions() { + val selections = mapNotNull { it.selection } + forEach { it.options.removeAll(selections) } + } +} diff --git a/domain/src/main/kotlin/ch/dissem/yaep/domain/Grid.kt b/domain/src/main/kotlin/ch/dissem/yaep/domain/Grid.kt new file mode 100644 index 0000000..1349a5c --- /dev/null +++ b/domain/src/main/kotlin/ch/dissem/yaep/domain/Grid.kt @@ -0,0 +1,41 @@ +package ch.dissem.yaep.domain + +class Grid( + val rows: List>> +) : List>> by rows { + + fun > indexOf(element: C): Int { + return this[element.companion] + .indexOfFirst { it.selection?.itemType == element } + } + + operator fun > get(itemType: ItemClassCompanion): GameRow { + @Suppress("UNCHECKED_CAST") + return rows.first { it.category == itemType } as GameRow + } + + operator fun > get(item: Item): GameCell { + return this[item.itemType.companion].first { it.selection == item } + } + + override fun toString(): String { + return rows.map { row -> row.map { it.selection?.symbol ?: " " }.joinToString("") } + .joinToString("\n") + } +} + +fun List>>>.toGrid() = Grid( + map { row -> + GameRow( + category = row.first().itemType.companion, + options = row, + cells = row.map { + GameCell( + selection = null, + solution = it, + options = row.toMutableList() + ) + } + ) + } +) diff --git a/domain/src/main/kotlin/ch/dissem/yaep/domain/Item.kt b/domain/src/main/kotlin/ch/dissem/yaep/domain/Item.kt new file mode 100644 index 0000000..87ae858 --- /dev/null +++ b/domain/src/main/kotlin/ch/dissem/yaep/domain/Item.kt @@ -0,0 +1,23 @@ +package ch.dissem.yaep.domain + + +class Item>( + val itemType: C, + val symbol: String = itemType.symbols.random() +) { + constructor(itemType: C) : this(itemType, itemType.symbols.random()) + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is Item<*>) return false + return itemType == other.itemType + } + + override fun hashCode(): Int { + return itemType.hashCode() + } + + override fun toString(): String { + return itemType.toString() + symbol + } +} diff --git a/domain/src/main/kotlin/ch/dissem/yaep/domain/grid.kt b/domain/src/main/kotlin/ch/dissem/yaep/domain/grid.kt deleted file mode 100644 index c17dbbd..0000000 --- a/domain/src/main/kotlin/ch/dissem/yaep/domain/grid.kt +++ /dev/null @@ -1,88 +0,0 @@ -package ch.dissem.yaep.domain - -class GameRow>( - val category: ItemClassCompanion, - val options: List>, - val cells: List> -) : List> by cells { - fun indexOf(element: C) = indexOfFirst { it.selection?.itemType == element } - - fun updateOptions() { - val selections = mapNotNull { it.selection } - forEach { it.options.removeAll(selections) } - } -} - -class Grid( - val rows: List>> -) : List>> by rows { - - fun > indexOf(element: C): Int { - return this[element.companion] - .indexOfFirst { it.selection?.itemType == element } - } - - operator fun > get(itemType: ItemClassCompanion): GameRow { - @Suppress("UNCHECKED_CAST") - return rows.first { it.category == itemType } as GameRow - } - - operator fun > get(item: Item): GameCell { - return this[item.itemType.companion].first { it.selection == item } - } - - override fun toString(): String { - return rows.map { row -> row.map { it.selection?.symbol ?: " " }.joinToString("") } - .joinToString("\n") - } -} - -fun List>>>.toGrid() = Grid( - map { row -> - GameRow( - category = row.first().itemType.companion, - options = row, - cells = row.map { - GameCell( - selection = null, - solution = it, - options = row.toMutableList() - ) - } - ) - } -) - -class GameCell>( - var selection: Item?, - val solution: Item, - val options: MutableList> -) -//{ -// val options = options.toMutableStateList() -// var selection by mutableStateOf(selection) -//} - -fun > GameCell?.mayBe(item: Item) = - this != null && (selection == item || (selection == null && options.contains(item))) - -class Item>( - val itemType: C, - val symbol: String = itemType.symbols.random() -) { - constructor(itemType: C) : this(itemType, itemType.symbols.random()) - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other !is Item<*>) return false - return itemType == other.itemType - } - - override fun hashCode(): Int { - return itemType.hashCode() - } - - override fun toString(): String { - return itemType.toString() + symbol - } -} \ No newline at end of file