Fix crash

This commit is contained in:
Lucas Lima 2020-10-02 18:42:37 -03:00
parent 485dd2297a
commit 93ea44df07
No known key found for this signature in database
GPG key ID: 049CCC5A365B00D2
2 changed files with 20 additions and 12 deletions

View file

@ -242,15 +242,13 @@ class GameController {
fun hasFlaggedAllMines(): Boolean = rightFlags() == minefield.mines
fun hasIsolatedAllMines() =
mines().map {
val neighbors = field.filterNeighborsOf(it)
val neighborsCount = neighbors.count()
val isolatedNeighborsCount = neighbors.count { neighbor ->
!neighbor.isCovered || neighbor.hasMine
}
neighborsCount != isolatedNeighborsCount
}.count { it } == 0
fun hasIsolatedAllMines(): Boolean {
return field.let {
val openSquares = it.count { area -> !area.isCovered }
val mines = it.count { area -> area.hasMine }
(openSquares + mines) == it.size
}
}
private fun rightFlags() = mines().count { it.mark.isFlag() }

View file

@ -212,12 +212,22 @@ class GameControllerTest {
assertFalse(controller.hasIsolatedAllMines())
assertFalse(controller.isGameOver())
val lastArea = controller.field()
.last { !it.hasMine }.id
controller.field()
.filter { !it.hasMine }
.forEach { controller.fakeSingleClick(it.id) }
.onEach {
controller.fakeSingleClick(it.id)
assertTrue(controller.hasIsolatedAllMines())
assertTrue(controller.isGameOver())
if (it.id != lastArea) {
assertFalse(controller.hasIsolatedAllMines())
assertFalse(controller.isGameOver())
} else {
assertTrue(controller.hasIsolatedAllMines())
assertTrue(controller.isGameOver())
}
}
}
}