Fix crash
This commit is contained in:
parent
485dd2297a
commit
93ea44df07
2 changed files with 20 additions and 12 deletions
|
@ -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() }
|
||||
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue