From aa8bac13cec0abba29e072658782cc9b5555e052 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Fri, 19 Jul 2024 23:49:39 +0200 Subject: [PATCH] Change clues interface --- .../kotlin/ch/dissem/yaep/domain/clues.kt | 41 +++++++++++++------ .../dissem/yaep/domain/SameColumnClueTest.kt | 13 +++--- .../ch/dissem/yaep/domain/TripletClueTest.kt | 16 ++++---- gradle/libs.versions.toml | 2 +- 4 files changed, 43 insertions(+), 29 deletions(-) diff --git a/domain/src/main/kotlin/ch/dissem/yaep/domain/clues.kt b/domain/src/main/kotlin/ch/dissem/yaep/domain/clues.kt index 32975f0..41f5f11 100644 --- a/domain/src/main/kotlin/ch/dissem/yaep/domain/clues.kt +++ b/domain/src/main/kotlin/ch/dissem/yaep/domain/clues.kt @@ -25,10 +25,12 @@ class NeighbourClue, B : ItemClass>(val a: Item, val b: I if (ia != -1) { if (ib != -1) return ib == ia - 1 || ib == ia + 1 - return rowB.getOrNull(ia - 1).hasNoSelection() || rowB.getOrNull(ia + 1).hasNoSelection() + return rowB.getOrNull(ia - 1).hasNoSelection() || rowB.getOrNull(ia + 1) + .hasNoSelection() } if (ib != -1) { - return rowA.getOrNull(ib - 1).hasNoSelection() || rowA.getOrNull(ib + 1).hasNoSelection() + return rowA.getOrNull(ib - 1).hasNoSelection() || rowA.getOrNull(ib + 1) + .hasNoSelection() } return true } @@ -133,20 +135,24 @@ class TripletClue, B : ItemClass, C : ItemClass>( if (ia != -1) { return when (ib) { -1 -> when (ic) { - -1 -> (rowB[ia - 1].hasNoSelection() && rowC[ia - 2].hasNoSelection()) || (rowB[ia + 1].hasNoSelection() && rowC[ia + 2].hasNoSelection()) - ia - 2 -> rowB[ia - 1].hasNoSelection() - ia + 2 -> rowB[ia + 1].hasNoSelection() + -1 -> (rowB.getOrNull(ia - 1).hasNoSelection() && rowC.getOrNull(ia - 2) + .hasNoSelection()) || + (rowB.getOrNull(ia + 1).hasNoSelection() && rowC.getOrNull(ia + 2) + .hasNoSelection()) + + ia - 2 -> rowB.getOrNull(ia - 1).hasNoSelection() + ia + 2 -> rowB.getOrNull(ia + 1).hasNoSelection() else -> false } ia - 1 -> when (ic) { - -1 -> rowC[ia - 2].hasNoSelection() + -1 -> rowC.getOrNull(ia - 2).hasNoSelection() ia - 2 -> true else -> false } ia + 1 -> when (ic) { - -1 -> rowC[ia + 2].hasNoSelection() + -1 -> rowC.getOrNull(ia + 2).hasNoSelection() ia + 2 -> true else -> false } @@ -156,19 +162,28 @@ class TripletClue, B : ItemClass, C : ItemClass>( } if (ib != -1) { when (ic) { - -1 -> return (rowA[ib - 1].hasNoSelection() && rowC[ib + 1].hasNoSelection()) || (rowA[ib + 1].hasNoSelection() && rowC[ib - 1].hasNoSelection()) - ib - 1 -> return rowA[ib + 1].hasNoSelection() - ib + 1 -> return rowA[ib - 1].hasNoSelection() + -1 -> return (rowA.getOrNull(ib - 1).hasNoSelection() && rowC.getOrNull(ib + 1) + .hasNoSelection()) || + (rowA.getOrNull(ib + 1).hasNoSelection() && rowC.getOrNull(ib - 1) + .hasNoSelection()) + + ib - 1 -> return rowA.getOrNull(ib + 1).hasNoSelection() + ib + 1 -> return rowA.getOrNull(ib - 1).hasNoSelection() } } if (ic != -1) { - return (rowB[ic - 1].hasNoSelection() && rowA[ic - 2].hasNoSelection()) || (rowB[ic + 1].hasNoSelection() && rowA[ic + 2].hasNoSelection()) + return (rowB.getOrNull(ic - 1).hasNoSelection() && rowA.getOrNull(ic - 2) + .hasNoSelection()) || + (rowB.getOrNull(ic + 1).hasNoSelection() && rowA.getOrNull(ic + 2) + .hasNoSelection()) } return rowA.mapIndexed { index, gameCell -> if (gameCell.hasNoSelection()) index else null } .filterNotNull() .any { index -> - (rowB.getOrNull(index - 1).hasNoSelection() && rowC.getOrNull(index - 2).hasNoSelection()) || - (rowB.getOrNull(index + 1).hasNoSelection() && rowC.getOrNull(index + 2).hasNoSelection()) + (rowB.getOrNull(index - 1).hasNoSelection() && rowC.getOrNull(index - 2) + .hasNoSelection()) || + (rowB.getOrNull(index + 1).hasNoSelection() && rowC.getOrNull(index + 2) + .hasNoSelection()) } } diff --git a/domain/src/test/kotlin/ch/dissem/yaep/domain/SameColumnClueTest.kt b/domain/src/test/kotlin/ch/dissem/yaep/domain/SameColumnClueTest.kt index fe9175c..e378fa9 100644 --- a/domain/src/test/kotlin/ch/dissem/yaep/domain/SameColumnClueTest.kt +++ b/domain/src/test/kotlin/ch/dissem/yaep/domain/SameColumnClueTest.kt @@ -41,7 +41,7 @@ class SameColumnClueTest : ClueTest() { } @Test - fun `if a is set, but b is not an option in the same column, it's considered invalid`() { + fun `if a is set, but b is set to a wrong value, it's considered invalid`() { val grid = createGrid { null } for (ia in 0 until size - 1) { @@ -52,8 +52,7 @@ class SameColumnClueTest : ClueTest() { val b = grid[ib][jb] a.selection = a.solution - b.selection = null - b.options.remove(b.solution) + b.selection = b.solution expect(SameColumnClue(a.solution, b.solution).isValid(grid)).toEqual(false) expect(SameColumnClue(b.solution, a.solution).isValid(grid)).toEqual(false) @@ -75,16 +74,16 @@ class SameColumnClueTest : ClueTest() { val b = rowB[i] rowA.forEachIndexed { index, gameCell -> if (index < i) { - gameCell.options.remove(a.solution) + gameCell.selection = rowA.options.filter { it != a.solution }.random() } else { - gameCell.options.add(a.solution) + gameCell.selection = null } } rowB.forEachIndexed { index, gameCell -> if (index < i) { - gameCell.options.add(b.solution) + gameCell.selection = null } else { - gameCell.options.remove(b.solution) + gameCell.selection = rowB.options.filter { it != b.solution }.random() } } diff --git a/domain/src/test/kotlin/ch/dissem/yaep/domain/TripletClueTest.kt b/domain/src/test/kotlin/ch/dissem/yaep/domain/TripletClueTest.kt index fed4881..fde06fb 100644 --- a/domain/src/test/kotlin/ch/dissem/yaep/domain/TripletClueTest.kt +++ b/domain/src/test/kotlin/ch/dissem/yaep/domain/TripletClueTest.kt @@ -125,16 +125,16 @@ class TripletClueTest : ClueTest() { @Test fun `grid with a set and no b and c as option on the same side is considered invalid`() { val grid = createGrid { null } - val a = grid[2][3] - val b = grid[0][2] - val c = grid[1][1] + val rowA = grid[2] + val rowB = grid[0] + val rowC = grid[1] + val a = rowA[3] + val b = rowB[2] + val c = rowC[1] a.selection = a.solution - b.options.remove(b.solution) - c.options.add(c.solution) - - grid[0][4].options.add(b.solution) - grid[1][5].options.remove(c.solution) + rowB[4].selection = rowC[3].solution + c.selection = rowC[3].solution expect(TripletClue(a.solution, b.solution, c.solution).isValid(grid)).toEqual(false) expect(TripletClue(c.solution, b.solution, a.solution).isValid(grid)).toEqual(false) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index bf45bba..03d1dba 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -agp = "8.5.0" +agp = "8.5.1" android-compileSdk = "34" android-minSdk = "24" android-targetSdk = "34"