Fix drawer bug

This commit is contained in:
Lucas Lima 2020-04-05 10:41:40 -03:00
parent 698d74bc9e
commit eb92414df1
No known key found for this signature in database
GPG key ID: C828A958035D9C34
4 changed files with 39 additions and 28 deletions

View file

@ -230,16 +230,12 @@ class GameActivity : DaggerAppCompatActivity() {
}
override fun onDrawerOpened(drawerView: View) {
if (status is Status.Over) {
viewModel.pauseGame()
}
viewModel.pauseGame()
analyticsManager.sentEvent(Analytics.OpenDrawer())
}
override fun onDrawerClosed(drawerView: View) {
if (status is Status.Over) {
viewModel.resumeGame()
}
viewModel.resumeGame()
analyticsManager.sentEvent(Analytics.CloseDrawer())
}

View file

@ -112,7 +112,7 @@ class LevelFacade {
.shuffled(randomGenerator)
.take(minefield.mines)
.forEach { it.hasMine = true }
mines = field.filter { it.hasMine }.asSequence()
mines = field.filter { it.hasMine }
hasMines = mines.count() != 0
}
@ -304,6 +304,9 @@ class LevelFacade {
fun checkVictory(): Boolean =
hasMines && hasIsolatedAllMines() && !hasAnyMineExploded()
fun isGameOver(): Boolean =
checkVictory() || hasAnyMineExploded()
fun remainingMines(): Int {
val flagsCount = field.count { it.mark.isFlag() }
val minesCount = mines.count()
@ -355,6 +358,6 @@ class LevelFacade {
}
companion object {
fun randomSeed(): Long = Random().nextLong()
private fun randomSeed(): Long = Random().nextLong()
}
}

View file

@ -134,8 +134,9 @@ class GameViewModel(
}
fun resumeGame() {
if (initialized && levelFacade.hasMines) {
if (initialized && levelFacade.hasMines && !levelFacade.isGameOver()) {
eventObserver.postValue(Event.Resume)
runClock()
}
}

View file

@ -482,27 +482,22 @@ class LevelFacadeTest {
@Test
fun testHasIsolatedAllMines() {
levelFacadeOf(3, 3, 5, 200L).run {
val width = 12
val height = 12
val mines = 9
levelFacadeOf(width, height, mines, 200L).run {
plantMinesExcept(3)
assertFalse(hasIsolatedAllMines())
field.filterNot { it.hasMine }.take(width * height - 1 - mines).forEach {
singleClick(it.id)
assertFalse(hasIsolatedAllMines())
assertFalse(isGameOver())
}
singleClick(6)
assertFalse(hasIsolatedAllMines())
singleClick(4)
assertFalse(hasIsolatedAllMines())
singleClick(2)
assertFalse(hasIsolatedAllMines())
singleClick(3)
assertFalse(hasIsolatedAllMines())
singleClick(5)
assertFalse(hasIsolatedAllMines())
singleClick(8)
assertTrue(hasIsolatedAllMines())
field.first { !it.hasMine && it.isCovered }.run {
singleClick(id)
assertTrue(hasIsolatedAllMines())
assertTrue(isGameOver())
}
}
}
@ -516,10 +511,26 @@ class LevelFacadeTest {
isCovered = false
mistake = true
}
assertTrue(hasAnyMineExploded())
}
}
@Test
fun testGameOverWithMineExploded() {
levelFacadeOf(3, 3, 5, 200L).run {
plantMinesExcept(3)
assertFalse(isGameOver())
field.first { it.hasMine }.apply {
isCovered = false
mistake = true
}
assertTrue(isGameOver())
}
}
@Test
fun testVictory() {
levelFacadeOf(3, 3, 5, 200L).run {