nonogram: one placed run can no longer grey two clues
- Commit
fd2757f7de127a430af8947c6d183b17060be1e2- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 05:57 UTC (5 days ago)
- Parents
d0ef4aa
rowGreying counted leading runs and trailing runs independently and took the union, so a single maximal run greyed both the first and the last clue. On the 3x3 plus puzzle (#.# / ### / #.#, top-row clues [1,1]), filling only the top-left cell greyed both 1s — the player was told a clue was satisfied when its run did not exist yet. The same defect greyed three clues from two runs and both 2s in [2,2] from one run. Greying now computes the line's maximal run lengths once: the prefix matches leading clues, and the suffix only counts trailing clues over runs the prefix did not already consume, so one run can never account for two clues. Fully matched lines still grey everything and distinct prefix/suffix runs still grey both ends. Four new regression tests pin the shapes (no existing assertion modified); solver uniqueness, win detection and persistence were audited and cleared. Verified: :games:nonogram:testDevUnitTest 153 tests pass and :games:nonogram:lint passes (warnings are pre-existing in library dependencies).
Diff
games/nonogram/src/main/java/com/vayunmathur/games/nonogram/domain/NonogramClues.kt
diff --git a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/domain/NonogramClues.kt b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/domain/NonogramClues.ktindex 21ad851c98..b4bbf96d99 100644--- a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/domain/NonogramClues.kt+++ b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/domain/NonogramClues.kt@@ -7,11 +7,13 @@ import com.vayunmathur.games.nonogram.data.NonogramGameState * Which numbers of one line's clue are already accounted for by the fills at that end * of the line. *- * [prefixMatched] and [suffixMatched] are counted independently from their own end,- * so on a completely matched line they can overlap over the same middle clue(s);- * [greyed] is the union and is what the UI renders. A clue is only ever greyed as- * part of a matched prefix or suffix — a run in the middle of the line never greys- * a clue on its own.+ * [prefixMatched] counts the leading clues matched by the runs from the start of+ * the line, and [suffixMatched] counts the trailing clues matched by the runs+ * the prefix did not already consume — one run can never account for two clues,+ * so a single placed run greys at most one clue. On a completely matched line+ * the prefix alone accounts for every clue. [greyed] is the union and is what+ * the UI renders. A clue is only ever greyed as part of a matched prefix or+ * suffix — a run in the middle of the line never greys a clue on its own. */ data class ClueGreying( val prefixMatched: Int,@@ -54,8 +56,9 @@ object NonogramClues { val count = if (matched) 1 else 0 return ClueGreying(count, count, listOf(matched)) }- val prefix = countPrefix(clues, filled)- val suffix = countSuffix(clues, filled)+ val runs = runLengths(filled)+ val prefix = countPrefix(clues, runs)+ val suffix = countSuffix(clues, runs, prefixMatched = prefix) return ClueGreying( prefixMatched = prefix, suffixMatched = suffix,@@ -63,34 +66,44 @@ object NonogramClues { ) } + /** Lengths of the maximal runs of filled cells in [filled], in line order. */+ private fun runLengths(filled: List<Boolean>): List<Int> {+ val runs = mutableListOf<Int>()+ var run = 0+ for (cell in filled) {+ if (cell) {+ run+++ } else if (run > 0) {+ runs.add(run)+ run = 0+ }+ }+ if (run > 0) runs.add(run)+ return runs+ }+ /** How many leading clues the runs from the start of the line match exactly. */- private fun countPrefix(clues: List<Int>, filled: List<Boolean>): Int {- var clueIndex = 0- var pos = 0- while (clueIndex < clues.size) {- while (pos < filled.size && !filled[pos]) pos++- if (pos >= filled.size) break- val start = pos- while (pos < filled.size && filled[pos]) pos++- if (pos - start != clues[clueIndex]) break- clueIndex+++ private fun countPrefix(clues: List<Int>, runs: List<Int>): Int {+ var matched = 0+ while (matched < clues.size && matched < runs.size && runs[matched] == clues[matched]) {+ matched++ }- return clueIndex+ return matched } - /** How many trailing clues the runs from the end of the line match exactly. */- private fun countSuffix(clues: List<Int>, filled: List<Boolean>): Int {- var clueIndex = clues.lastIndex- var pos = filled.lastIndex+ /**+ * How many trailing clues the runs from the end of the line match exactly.+ *+ * Only runs the prefix did not consume are available: without that cap the+ * same run would be counted from both ends, so one placed run could grey+ * two clues (e.g. `[1, 1]` over a single fill greying both numbers).+ */+ private fun countSuffix(clues: List<Int>, runs: List<Int>, prefixMatched: Int): Int { var matched = 0- while (clueIndex >= 0) {- while (pos >= 0 && !filled[pos]) pos--- if (pos < 0) break- val end = pos- while (pos >= 0 && filled[pos]) pos--- if (end - pos != clues[clueIndex]) break+ while (matched < clues.size && matched < runs.size - prefixMatched &&+ runs[runs.lastIndex - matched] == clues[clues.lastIndex - matched]+ ) { matched++- clueIndex-- } return matched }
games/nonogram/src/test/java/com/vayunmathur/games/nonogram/domain/NonogramCluesTest.kt
diff --git a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/domain/NonogramCluesTest.kt b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/domain/NonogramCluesTest.ktindex 02e7a1da7e..27f4cac599 100644--- a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/domain/NonogramCluesTest.kt+++ b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/domain/NonogramCluesTest.kt@@ -139,6 +139,36 @@ class NonogramCluesTest { assertTrue(g.prefixMatched + g.suffixMatched >= 2) } + @Test fun aSingleRunCannotAccountForTwoClues() {+ // One fill is one run: it can satisfy the first "1" or the second, but+ // counting it from both ends greys both clues while a run is still missing.+ val g = grey(listOf(1, 1), "#....")+ assertEquals(1, g.prefixMatched); assertEquals(0, g.suffixMatched)+ assertEquals(listOf(true, false), g.greyed)+ }++ @Test fun twoRunsCannotAccountForThreeClues() {+ val g = grey(listOf(1, 1, 1), "#.#..")+ assertEquals(listOf(true, true, false), g.greyed)+ }++ @Test fun aSingleRunOfTwoCannotGreyBothTwos() {+ assertEquals(listOf(true, false), grey(listOf(2, 2), "..##.").greyed)+ }++ @Test fun rowGreyingDoesNotReuseTheSameRunForBothClues() {+ // Top row of this board is "#.#" (clues [1, 1]); only its first cell is filled.+ val board = puzzle(+ """+ #.#+ ###+ #.#+ """+ )+ val s = NonogramGameState(board, setOf(0), emptySet(), emptySet(), STARTING_HEARTS, GameMode.CASUAL, 1)+ assertEquals(listOf(true, false), NonogramClues.rowGreying(s, 0).greyed)+ }+ @Test fun middleRunAloneNeverGreysAMiddleClue() { // A lone central run of 1 matches the middle clue's length but is // reachable from neither end, so nothing greys.