From 69bb20d12a99ddbd17245ad31dbdb226b1f61bbe Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Tue, 25 Jun 2024 23:53:29 +0200 Subject: [PATCH] Fix tests & clues (WIP) --- composeApp/src/commonMain/kotlin/domain/clues.kt | 8 ++++---- composeApp/src/commonMain/kotlin/domain/generator.kt | 7 +++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/domain/clues.kt b/composeApp/src/commonMain/kotlin/domain/clues.kt index dd9a8ed..5b0a184 100644 --- a/composeApp/src/commonMain/kotlin/domain/clues.kt +++ b/composeApp/src/commonMain/kotlin/domain/clues.kt @@ -57,11 +57,11 @@ class OrderClue, R : ItemClass>(val left: Item, val right if (iLeft != -1) { if (iRight != -1) return iRight <= iLeft - return rowRight.indexOfLast { it.mayBe(right) } > iLeft + return rowRight.indexOfLast { it.mayBe(right) } in 0..iLeft } if (iRight != -1) { - return rowLeft.indexOfFirst { it.mayBe(left) } in 0 until iRight + return rowLeft.indexOfFirst { it.mayBe(left) } >= iRight } return rowLeft.indexOfFirst { it.mayBe(left) } >= rowRight.indexOfLast { it.mayBe(right) } @@ -79,7 +79,7 @@ class TripletClue, B : ItemClass, C : ItemClass>( private val cType = c.itemType override fun isRuleViolated(grid: Grid): Boolean { - val rowA by lazy { grid[aType.companion] } + val rowA = grid[aType.companion] val rowB by lazy { grid[bType.companion] } val rowC by lazy { grid[cType.companion] } @@ -178,7 +178,7 @@ class SameColumnClue, B : ItemClass>(val a: Item, val b: } for (i in 0 until grid.size) { - if (!rowA[i].mayBe(a) && !rowB[i].mayBe(b)) { + if (rowA[i].mayBe(a) && rowB[i].mayBe(b)) { return false } } diff --git a/composeApp/src/commonMain/kotlin/domain/generator.kt b/composeApp/src/commonMain/kotlin/domain/generator.kt index 794dfd5..65f2169 100644 --- a/composeApp/src/commonMain/kotlin/domain/generator.kt +++ b/composeApp/src/commonMain/kotlin/domain/generator.kt @@ -37,7 +37,6 @@ 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.) } -// FIXME: I need to better include the options into the solver (rule violations checks) private fun solve( grid: Grid, clues: Collection @@ -124,8 +123,8 @@ private fun getAllClues(rows: List>>>): MutableSet // } // } - rows.forEach { columns -> - columns.forEachIndexed { j, item -> + rows.forEach { row -> + row.forEachIndexed { j, item -> // Clue: Neighbours if (j > 0) { rows.map { it[j - 1] }.forEach { @@ -141,7 +140,7 @@ private fun getAllClues(rows: List>>>): MutableSet // Clue: Order if (j > 0) { - rows.flatMap { it.take(j - 1) }.forEach { + rows.flatMap { it.take(j) }.forEach { clues.add(OrderClue(it, item)) } }