Render puzzle (WIP)
This commit is contained in:
5
composeApp/src/androidMain/kotlin/ui/theme/emoji.kt
Normal file
5
composeApp/src/androidMain/kotlin/ui/theme/emoji.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package ui.theme
|
||||
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
|
||||
actual val emojiFontFamily: FontFamily? = null
|
||||
@@ -1,5 +1,7 @@
|
||||
package domain
|
||||
|
||||
import androidx.compose.ui.util.fastAny
|
||||
|
||||
class Game(
|
||||
val grid: Grid,
|
||||
val clues: List<Clue>
|
||||
@@ -18,7 +20,6 @@ class Game(
|
||||
gameCell.options.remove(position.item)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +35,5 @@ class Game(
|
||||
}
|
||||
|
||||
fun areRulesViolated(): Boolean = clues
|
||||
.map { it.isRuleViolated(grid) }
|
||||
.reduce { a, b -> a || b }
|
||||
.any { it.isRuleViolated(grid) }
|
||||
}
|
||||
@@ -9,7 +9,7 @@ fun generateGame(size: Int = 6): Game {
|
||||
// Generate a random puzzle instance.
|
||||
val classes = ItemClass.randomClasses(size)
|
||||
|
||||
val grid: List<List<Item<ItemClass<*>>>> = classes.map { it ->
|
||||
val grid: List<List<Item<ItemClass<*>>>> = classes.map {
|
||||
it.randomItems(size).map { item -> Item(item) }
|
||||
}
|
||||
|
||||
@@ -18,6 +18,10 @@ fun generateGame(size: Int = 6): Game {
|
||||
// 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.)
|
||||
var clues = getAllClues(grid).shuffled()
|
||||
// var positionClues: MutableSet<PositionClue<out ItemClass<*>>> = grid.flatMap { row ->
|
||||
// row.mapIndexed { i, item -> PositionClue(item, i) }
|
||||
// }.toMutableSet()
|
||||
|
||||
|
||||
var i = 0
|
||||
|
||||
@@ -37,12 +41,14 @@ fun generateGame(size: Int = 6): Game {
|
||||
// (You can speed this up by removing clues in batches rather than one at a time, but it makes the algorithm more complicated to describe.)
|
||||
}
|
||||
|
||||
private fun solve(grid: Grid, clues: List<Clue>): PuzzleSolution {
|
||||
private fun solve(
|
||||
grid: Grid,
|
||||
clues: Collection<Clue>
|
||||
): PuzzleSolution {
|
||||
// Start with a grid where each cell is a list of all possible items.
|
||||
|
||||
// First, set the positions of the items that are already known.
|
||||
val positionClues = clues.filterIsInstance<PositionClue<ItemClass<*>>>().toSet()
|
||||
positionClues.forEach { position ->
|
||||
clues.filterIsInstance<PositionClue<ItemClass<*>>>().forEach { position ->
|
||||
val row = grid[position.item.itemType.companion]
|
||||
row.forEachIndexed { index, gameCell ->
|
||||
if (index == position.index) {
|
||||
@@ -56,9 +62,10 @@ private fun solve(grid: Grid, clues: List<Clue>): PuzzleSolution {
|
||||
// For each clue, remove any items that violate the clue.
|
||||
// If any cell has only one item left, remove that item from all other cells.
|
||||
// Repeat until no more items can be removed.
|
||||
val otherClues = clues - positionClues
|
||||
var removedOptions = false
|
||||
val otherClues = clues.filter { it !is PositionClue<*> }
|
||||
var removedOptions: Boolean
|
||||
do {
|
||||
removedOptions = false
|
||||
grid.forEach { row ->
|
||||
removedOptions = row.tryOptionsForClues(grid, otherClues) || removedOptions
|
||||
}
|
||||
@@ -112,15 +119,17 @@ fun <C : ItemClass<C>> GameRow<C>.cleanupOptions(cell: GameCell<C>) {
|
||||
}
|
||||
}
|
||||
|
||||
private fun getAllClues(rows: List<List<Item<ItemClass<*>>>>): MutableList<Clue> {
|
||||
val clues = mutableListOf<Clue>()
|
||||
// For optimization reasons we want the positional clues first
|
||||
rows.forEach { row ->
|
||||
row.forEachIndexed { i, item ->
|
||||
clues.add(PositionClue(item, i))
|
||||
}
|
||||
}
|
||||
rows.forEachIndexed { i, columns ->
|
||||
private fun getAllClues(rows: List<List<Item<ItemClass<*>>>>): MutableSet<Clue> {
|
||||
val clues = mutableSetOf<Clue>()
|
||||
// rows.forEach { row ->
|
||||
// row.forEachIndexed { i, item ->
|
||||
// clues.add(PositionClue(item, i))
|
||||
// }
|
||||
// }
|
||||
clues.add(PositionClue(rows.random().first(), 0))
|
||||
clues.add(PositionClue(rows.random()[3], 3))
|
||||
|
||||
rows.forEach { columns ->
|
||||
columns.forEachIndexed { j, item ->
|
||||
// Clue: Neighbours
|
||||
if (j > 0) {
|
||||
|
||||
@@ -30,9 +30,9 @@ class Grid(
|
||||
fun List<List<Item<ItemClass<*>>>>.toGrid() = Grid(
|
||||
map { row ->
|
||||
GameRow(
|
||||
row.first().itemType.companion,
|
||||
row,
|
||||
row.map { GameCell(selection = null, solution = it, options = row.toMutableList()) }
|
||||
category = row.first().itemType.companion,
|
||||
options = row,
|
||||
cells = row.map { GameCell(selection = null, solution = it, options = row.toMutableList()) }
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -23,6 +23,7 @@ import domain.Item
|
||||
import domain.ItemClass
|
||||
import domain.ItemClassCompanion
|
||||
import org.jetbrains.compose.ui.tooling.preview.Preview
|
||||
import ui.theme.emojiFontFamily
|
||||
import kotlin.math.min
|
||||
|
||||
@Composable
|
||||
@@ -86,7 +87,7 @@ fun <C : ItemClass<C>> DrawItem(
|
||||
drawText(
|
||||
textMeasurer = textMeasurer,
|
||||
text = emoji,
|
||||
style = TextStyle(fontSize = fontSize),
|
||||
style = TextStyle(fontSize = fontSize, fontFamily = emojiFontFamily ?: TextStyle.Default.fontFamily),
|
||||
topLeft = offset
|
||||
)
|
||||
}
|
||||
|
||||
5
composeApp/src/commonMain/kotlin/ui/theme/emoji.kt
Normal file
5
composeApp/src/commonMain/kotlin/ui/theme/emoji.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package ui.theme
|
||||
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
|
||||
expect val emojiFontFamily: FontFamily?
|
||||
14
composeApp/src/desktopMain/kotlin/ui/theme/emoji.kt
Normal file
14
composeApp/src/desktopMain/kotlin/ui/theme/emoji.kt
Normal file
@@ -0,0 +1,14 @@
|
||||
package ui.theme
|
||||
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.font.FontStyle
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.platform.Font
|
||||
|
||||
actual val emojiFontFamily: FontFamily? = FontFamily(
|
||||
Font(
|
||||
resource = "NotoColorEmoji-Regular.ttf",
|
||||
weight = FontWeight.W400,
|
||||
style = FontStyle.Normal
|
||||
)
|
||||
)
|
||||
BIN
composeApp/src/desktopMain/resources/NotoColorEmoji-Regular.ttf
Normal file
BIN
composeApp/src/desktopMain/resources/NotoColorEmoji-Regular.ttf
Normal file
Binary file not shown.
Reference in New Issue
Block a user