Add tests

This commit is contained in:
2024-06-23 22:27:45 +02:00
parent a598218cf2
commit f76be158a0
7 changed files with 258 additions and 57 deletions

View File

@@ -3,7 +3,6 @@ package domain
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import kotlin.math.abs
sealed class Clue {
abstract fun isRuleViolated(grid: Grid): Boolean
@@ -17,12 +16,21 @@ class NeighbourClue<C : ItemClass<C>>(val a: Item<C>, val b: Item<C>) : Horizont
private val bType = b.itemType
override fun isRuleViolated(grid: Grid): Boolean {
val ia = grid.indexOf(aType)
val ib = grid.indexOf(bType)
val rowA = grid[aType.companion]
val rowB = grid[bType.companion]
if (ia == -1 || ib == -1) return false
for (i in 1 until grid.size) {
if (rowA[i - 1].mayBe(a) &&
rowB[i - 0].mayBe(b)
)
return false
return abs(ia - ib) != 1
if (rowA[i - 0].mayBe(a) &&
rowB[i - 1].mayBe(b)
)
return false
}
return true
}
}
@@ -31,12 +39,10 @@ class OrderClue<C : ItemClass<C>>(val left: Item<C>, val right: Item<C>) : Horiz
private val rightType = right.itemType
override fun isRuleViolated(grid: Grid): Boolean {
val il = grid.indexOf(leftType)
val ir = grid.indexOf(rightType)
val rowLeft = grid[leftType.companion]
val rowRight = grid[rightType.companion]
if (il == -1 || ir == -1) return false
return ir <= il
return rowLeft.indexOfFirst { it.mayBe(left) } >= rowRight.indexOfLast { it.mayBe(right) }
}
}
@@ -46,33 +52,25 @@ class TripletClue<C : ItemClass<C>>(val a: Item<C>, val b: Item<C>, val c: Item<
private val bType = b.itemType
private val cType = c.itemType
private fun isNeighbourRuleViolated(ix: Int, iy: Int): Boolean {
if (ix == -1 || iy == -1) return false
return abs(ix - iy) != 1
}
override fun isRuleViolated(grid: Grid): Boolean {
val ia = grid.indexOf(aType)
val ib = grid.indexOf(bType)
val ic = grid.indexOf(cType)
val rowA = grid[aType.companion]
val rowB = grid[bType.companion]
val rowC = grid[cType.companion]
if (ib == 0 || ib == grid.size) {
return true
for (i in 2 until grid.size) {
if (rowA[i - 2].mayBe(a) &&
rowB[i - 1].mayBe(b) &&
rowC[i - 0].mayBe(c)
)
return false
if (rowA[i - 0].mayBe(a) &&
rowB[i - 1].mayBe(b) &&
rowC[i - 2].mayBe(c)
)
return false
}
if (ia == -1 && ic == -1) {
return false
}
if (ia != -1 && ic != -1) {
return !(ia + 2 == ic || ia == ic + 2)
}
if (isNeighbourRuleViolated(ia, ib) || isNeighbourRuleViolated(ib, ic)) {
return true
}
return false
return true
}
}
@@ -81,23 +79,20 @@ class SameRowClue<C : ItemClass<C>>(val a: Item<C>, val b: Item<C>) : Clue() {
private val bType = b.itemType
override fun isRuleViolated(grid: Grid): Boolean {
val ia = grid.indexOf(aType)
val ib = grid.indexOf(bType)
val rowA = grid[aType.companion]
val rowB = grid[bType.companion]
if (ia == -1 || ib == -1) return false
return ia != ib
for (i in 0 until grid.size) {
if (rowA[i].mayBe(a) && rowB[i].mayBe(b)) {
return false
}
}
return true
}
}
class PositionClue<C : ItemClass<C>>(val item: Item<C>, val index: Int) : Clue() {
private val aType = item.itemType
override fun isRuleViolated(grid: Grid): Boolean {
val ia = grid.indexOf(aType)
if (ia == -1) return false
return ia != index
return grid[item].mayBe(item)
}
}