Change clues interface

This commit is contained in:
2024-07-19 23:49:39 +02:00
parent 6356e0f4ac
commit aa8bac13ce
4 changed files with 43 additions and 29 deletions

View File

@@ -25,10 +25,12 @@ class NeighbourClue<A : ItemClass<A>, B : ItemClass<B>>(val a: Item<A>, val b: I
if (ia != -1) { if (ia != -1) {
if (ib != -1) return ib == ia - 1 || ib == 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) { 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 return true
} }
@@ -133,20 +135,24 @@ class TripletClue<A : ItemClass<A>, B : ItemClass<B>, C : ItemClass<C>>(
if (ia != -1) { if (ia != -1) {
return when (ib) { return when (ib) {
-1 -> when (ic) { -1 -> when (ic) {
-1 -> (rowB[ia - 1].hasNoSelection() && rowC[ia - 2].hasNoSelection()) || (rowB[ia + 1].hasNoSelection() && rowC[ia + 2].hasNoSelection()) -1 -> (rowB.getOrNull(ia - 1).hasNoSelection() && rowC.getOrNull(ia - 2)
ia - 2 -> rowB[ia - 1].hasNoSelection() .hasNoSelection()) ||
ia + 2 -> rowB[ia + 1].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 else -> false
} }
ia - 1 -> when (ic) { ia - 1 -> when (ic) {
-1 -> rowC[ia - 2].hasNoSelection() -1 -> rowC.getOrNull(ia - 2).hasNoSelection()
ia - 2 -> true ia - 2 -> true
else -> false else -> false
} }
ia + 1 -> when (ic) { ia + 1 -> when (ic) {
-1 -> rowC[ia + 2].hasNoSelection() -1 -> rowC.getOrNull(ia + 2).hasNoSelection()
ia + 2 -> true ia + 2 -> true
else -> false else -> false
} }
@@ -156,19 +162,28 @@ class TripletClue<A : ItemClass<A>, B : ItemClass<B>, C : ItemClass<C>>(
} }
if (ib != -1) { if (ib != -1) {
when (ic) { when (ic) {
-1 -> return (rowA[ib - 1].hasNoSelection() && rowC[ib + 1].hasNoSelection()) || (rowA[ib + 1].hasNoSelection() && rowC[ib - 1].hasNoSelection()) -1 -> return (rowA.getOrNull(ib - 1).hasNoSelection() && rowC.getOrNull(ib + 1)
ib - 1 -> return rowA[ib + 1].hasNoSelection() .hasNoSelection()) ||
ib + 1 -> return rowA[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) { 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 } return rowA.mapIndexed { index, gameCell -> if (gameCell.hasNoSelection()) index else null }
.filterNotNull() .filterNotNull()
.any { index -> .any { index ->
(rowB.getOrNull(index - 1).hasNoSelection() && rowC.getOrNull(index - 2).hasNoSelection()) || (rowB.getOrNull(index - 1).hasNoSelection() && rowC.getOrNull(index - 2)
(rowB.getOrNull(index + 1).hasNoSelection() && rowC.getOrNull(index + 2).hasNoSelection()) .hasNoSelection()) ||
(rowB.getOrNull(index + 1).hasNoSelection() && rowC.getOrNull(index + 2)
.hasNoSelection())
} }
} }

View File

@@ -41,7 +41,7 @@ class SameColumnClueTest : ClueTest() {
} }
@Test @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 } val grid = createGrid { null }
for (ia in 0 until size - 1) { for (ia in 0 until size - 1) {
@@ -52,8 +52,7 @@ class SameColumnClueTest : ClueTest() {
val b = grid[ib][jb] val b = grid[ib][jb]
a.selection = a.solution a.selection = a.solution
b.selection = null b.selection = b.solution
b.options.remove(b.solution)
expect(SameColumnClue(a.solution, b.solution).isValid(grid)).toEqual(false) expect(SameColumnClue(a.solution, b.solution).isValid(grid)).toEqual(false)
expect(SameColumnClue(b.solution, a.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] val b = rowB[i]
rowA.forEachIndexed { index, gameCell -> rowA.forEachIndexed { index, gameCell ->
if (index < i) { if (index < i) {
gameCell.options.remove(a.solution) gameCell.selection = rowA.options.filter { it != a.solution }.random()
} else { } else {
gameCell.options.add(a.solution) gameCell.selection = null
} }
} }
rowB.forEachIndexed { index, gameCell -> rowB.forEachIndexed { index, gameCell ->
if (index < i) { if (index < i) {
gameCell.options.add(b.solution) gameCell.selection = null
} else { } else {
gameCell.options.remove(b.solution) gameCell.selection = rowB.options.filter { it != b.solution }.random()
} }
} }

View File

@@ -125,16 +125,16 @@ class TripletClueTest : ClueTest() {
@Test @Test
fun `grid with a set and no b and c as option on the same side is considered invalid`() { fun `grid with a set and no b and c as option on the same side is considered invalid`() {
val grid = createGrid { null } val grid = createGrid { null }
val a = grid[2][3] val rowA = grid[2]
val b = grid[0][2] val rowB = grid[0]
val c = grid[1][1] val rowC = grid[1]
val a = rowA[3]
val b = rowB[2]
val c = rowC[1]
a.selection = a.solution a.selection = a.solution
b.options.remove(b.solution) rowB[4].selection = rowC[3].solution
c.options.add(c.solution) c.selection = rowC[3].solution
grid[0][4].options.add(b.solution)
grid[1][5].options.remove(c.solution)
expect(TripletClue(a.solution, b.solution, c.solution).isValid(grid)).toEqual(false) expect(TripletClue(a.solution, b.solution, c.solution).isValid(grid)).toEqual(false)
expect(TripletClue(c.solution, b.solution, a.solution).isValid(grid)).toEqual(false) expect(TripletClue(c.solution, b.solution, a.solution).isValid(grid)).toEqual(false)

View File

@@ -1,5 +1,5 @@
[versions] [versions]
agp = "8.5.0" agp = "8.5.1"
android-compileSdk = "34" android-compileSdk = "34"
android-minSdk = "24" android-minSdk = "24"
android-targetSdk = "34" android-targetSdk = "34"