Merge pull request #32 from lucasnlm/improve-safe-area

Improve safe area. Use a circle instead of square
This commit is contained in:
Lucas Nunes 2020-03-25 11:44:02 -03:00 committed by GitHub
commit 6109001a54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -87,14 +87,21 @@ class LevelFacade {
putMinesTips()
}
private fun plantRandomMines(ignoreIndex: Int, includeSafeArea: Boolean) {
getArea(ignoreIndex).run {
private fun plantRandomMines(safeIndex: Int, includeSafeArea: Boolean) {
getArea(safeIndex).run {
safeZone = true
if (includeSafeArea) {
findNeighbors().forEach {
it.safeZone = true
}
findCrossNeighbors().forEach { neighbor ->
neighbor
.findCrossNeighbors()
.filterNot { it.safeZone }
.forEach { it.safeZone = true }
}
}
}
@ -129,6 +136,10 @@ class LevelFacade {
target.findNeighbors().forEach { openField(it) }
}
if (this.hasMines) {
field.filter { it.safeZone }.forEach { openField(it) }
}
if (target.hasMine) {
target.mistake = true
}
@ -248,6 +259,13 @@ class LevelFacade {
getNeighbor(1, -1)
).filterNotNull()
private fun Area.findCrossNeighbors() = arrayOf(
getNeighbor(1, 0),
getNeighbor(0, 1),
getNeighbor(-1, 0),
getNeighbor(0, -1)
).filterNotNull()
private fun Area.getNeighbor(x: Int, y: Int) = field.firstOrNull {
(it.posX == this.posX + x) && (it.posY == this.posY + y)
}