Merge pull request #32 from lucasnlm/improve-safe-area
Improve safe area. Use a circle instead of square
This commit is contained in:
commit
6109001a54
1 changed files with 20 additions and 2 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue