Skip to content

Commit

Permalink
Merge branch 'master' into fix-center-board
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasnlm authored Jun 20, 2020
2 parents 8267679 + 692f04b commit fd702f0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,25 @@ class LevelFacade {
return neighbors
}

fun openNeighbors(index: Int): Sequence<Area> =
getArea(index)
.findNeighbors()
.filter {
it.mark.isNone() && it.isCovered
}.also {
it.forEach { area -> openField(area) }
}
/**
* Open all tiles neighbors of [index].
* If the number of flagged neighbors is less than the number of neighbors
* it won't open any of them to avoid miss clicks.
*/
fun openNeighbors(index: Int): Sequence<Area> {
val neighbors = getArea(index).findNeighbors()
val flaggedCount = neighbors.count { it.mark.isFlag() }
return if (flaggedCount >= getArea(index).minesAround) {
neighbors
.filter {
it.mark.isNone() && it.isCovered
}.also {
it.forEach { area -> openField(area) }
}
} else {
sequenceOf()
}
}

fun runFlagAssistant(): Sequence<Area> {
// Must not select Mark.PurposefulNone, only Mark.None. Otherwise, it will flag
Expand Down Expand Up @@ -323,7 +334,7 @@ class LevelFacade {
return (minesCount - flagsCount).coerceAtLeast(0)
}

private fun Area.findNeighbors() = sequenceOf(
fun Area.findNeighbors() = sequenceOf(
getNeighbor(1, 0),
getNeighbor(1, 1),
getNeighbor(0, 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ class LevelFacadeTest {
}

@Test
fun testOpenNeighbors() {
fun testOpenNeighborsWithoutFlag() {
levelFacadeOf(5, 5, 24, 200L).run {
plantMinesExcept(12)
singleClick(12)
Expand All @@ -309,15 +309,59 @@ class LevelFacadeTest {
),
field.map { if (it.isCovered) 1 else 0 }.toList()
)

// It won't open any if the mines were not flagged.
openNeighbors(12)
assertEquals(
listOf(
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1
),
1, 1, 1, 1, 1,
1, 1, 0, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1),
field.map { if (it.isCovered) 1 else 0 }.toList()
)
}
}

@Test
fun testOpenNeighbors() {
levelFacadeOf(5, 5, 15, 200L).run {
plantMinesExcept(12)
singleClick(12)
assertEquals(
listOf(
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 0, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1),
field.map { if (it.isCovered) 1 else 0 }.toList()
)

// It won't open any if the mines were not flagged.
singleClick(14)
openNeighbors(14)
assertEquals(
listOf(
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 0, 1, 0,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1),
field.map { if (it.isCovered) 1 else 0 }.toList()
)

// After flag its neighbors, it must open all clean neighbors.
getArea(14).findNeighbors().filter { it.hasMine }.forEach { it.mark = Mark.Flag }
openNeighbors(14)
assertEquals(
listOf(
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 0, 0, 0,
1, 1, 1, 1, 0,
1, 1, 1, 1, 1),
field.map { if (it.isCovered) 1 else 0 }.toList()
)
}
Expand Down

0 comments on commit fd702f0

Please sign in to comment.