diff --git a/domain/src/commonMain/kotlin/ch/dissem/yaep/domain/generator.kt b/domain/src/commonMain/kotlin/ch/dissem/yaep/domain/generator.kt index ca1e21b..73a961b 100644 --- a/domain/src/commonMain/kotlin/ch/dissem/yaep/domain/generator.kt +++ b/domain/src/commonMain/kotlin/ch/dissem/yaep/domain/generator.kt @@ -1,6 +1,8 @@ package ch.dissem.yaep.domain -import ch.dissem.yaep.domain.PuzzleSolution.* +import ch.dissem.yaep.domain.PuzzleSolution.MULTIPLE_SOLUTIONS +import ch.dissem.yaep.domain.PuzzleSolution.NO_SOLUTION +import ch.dissem.yaep.domain.PuzzleSolution.SOLVABLE import kotlin.random.Random fun generateGame(size: Int = 6): Game { @@ -36,8 +38,9 @@ fun generateGame(size: Int = 6): Game { // If there is exactly one solution, update clues and reset index. clues = temp i = 0 + } else { + i++ } - i++ } return Game(grid.toGrid(), clues) diff --git a/domain/src/commonTest/kotlin/ch/dissem/yaep/domain/GameSolverTest.kt b/domain/src/commonTest/kotlin/ch/dissem/yaep/domain/GameSolverTest.kt index 54b1ce3..66ee851 100644 --- a/domain/src/commonTest/kotlin/ch/dissem/yaep/domain/GameSolverTest.kt +++ b/domain/src/commonTest/kotlin/ch/dissem/yaep/domain/GameSolverTest.kt @@ -1,29 +1,20 @@ package ch.dissem.yaep.domain import ch.dissem.yaep.domain.PuzzleSolution.SOLVABLE +import ch.tutteli.atrium.api.fluent.en_GB.toBeEmpty import ch.tutteli.atrium.api.fluent.en_GB.toEqual import ch.tutteli.atrium.api.verbs.expect -import io.github.oshai.kotlinlogging.KotlinLogging -import kotlin.io.path.Path -import kotlin.io.path.createDirectories -import kotlin.io.path.createFile -import kotlin.io.path.isDirectory -import kotlin.io.path.notExists -import kotlin.io.path.writeText import kotlin.test.Test -import kotlin.time.Clock import kotlin.time.ExperimentalTime class GameSolverTest { - private val log = KotlinLogging.logger {} @Test @OptIn(ExperimentalTime::class) - fun `find problematic clues`() { + fun `ensure there are no unnecessary clues`() { var game: Game var neighbours: List> - var i = 0 - do { + repeat(100) { game = generateGame() val triplets = game.horizontalClues.filterIsInstance>() neighbours = game.horizontalClues.filterIsInstance>() @@ -32,25 +23,9 @@ class GameSolverTest { t.contains(n.a) && t.contains(n.b) } } - i++ - log.info { "Found ${neighbours.size} neighbours after $i tries: $neighbours" } - } while (neighbours.isEmpty()) - - val dirName = """${System.getProperty("user.home")}/.yaep""" - val dir = Path(dirName) - if (dir.notExists()) { - dir.createDirectories() - } else if (!dir.isDirectory()) { - log.error { "Yaep data directory already exists and is not a directory: $dir" } - log.info { "Game: $game" } - } else { - val fileName = "$dirName/problematic-${Clock.System.now()}.yaep" - Path(fileName) - .createFile() - .writeText(game.toString()) - log.info { "Saved game to $fileName" } + expect(neighbours).toBeEmpty() + expect(game.solve()).toEqual(SOLVABLE) } - } fun TripletClue<*, *, *>.contains(item: Item<*>): Boolean {