This commit is contained in:
Lucas Lima 2020-07-15 22:29:39 -03:00 committed by Lucas Lima
parent a982b570ab
commit e85d94ff96
No known key found for this signature in database
GPG key ID: 0259A3F43EC1027A

View file

@ -12,7 +12,7 @@ class MinefieldHandler(
private var changes = 0
fun turnOffAllHighlighted() {
changes = field.filter { it.highlighted }.onEach { it.highlighted = false }.count()
changes += field.filter { it.highlighted }.onEach { it.highlighted = false }.count()
}
fun removeMarkAt(index: Int) {
@ -41,7 +41,7 @@ class MinefieldHandler(
field.getOrNull(index)?.run {
if (isCovered) {
changedIndex = index
changes += 1
changes++
isCovered = false
mark = Mark.None
@ -81,10 +81,21 @@ class MinefieldHandler(
val neighbors = field.filterNeighborsOf(this)
val flaggedCount = neighbors.count { it.mark.isFlag() }
if (flaggedCount >= minesAround) {
changes++
changes += neighbors
.filter { it.mark.isNone() && it.isCovered }
.filter { it.isCovered && it.mark.isNone() }
.onEach { openAt(it.id) }
.count()
} else {
val coveredNeighbors = neighbors.filter { it.isCovered }
if (coveredNeighbors.count() == minesAround) {
changes++
changes += coveredNeighbors.filter {
it.mark.isNone()
}.onEach {
switchMarkAt(it.id)
}.count()
}
}
}
}