diff --git a/common/src/main/java/dev/lucasnlm/antimine/common/level/GameController.kt b/common/src/main/java/dev/lucasnlm/antimine/common/level/GameController.kt index 715ad773..6b3398cd 100644 --- a/common/src/main/java/dev/lucasnlm/antimine/common/level/GameController.kt +++ b/common/src/main/java/dev/lucasnlm/antimine/common/level/GameController.kt @@ -21,7 +21,6 @@ import kotlin.random.Random */ class GameController { private val minefield: Minefield - private val randomGenerator: Random private val startTime = System.currentTimeMillis() private var saveId = 0 private var firstOpen: FirstOpen = FirstOpen.Unknown @@ -41,7 +40,6 @@ class GameController { constructor(minefield: Minefield, seed: Long, saveId: Int? = null) { this.minefieldCreator = MinefieldCreator(minefield, Random(seed)) this.minefield = minefield - this.randomGenerator = Random(seed) this.seed = seed this.saveId = saveId ?: 0 @@ -51,7 +49,6 @@ class GameController { constructor(save: Save) { this.minefieldCreator = MinefieldCreator(save.minefield, Random(save.seed)) this.minefield = save.minefield - this.randomGenerator = Random(save.seed) this.saveId = save.uid this.seed = save.seed this.firstOpen = save.firstOpen @@ -64,10 +61,10 @@ class GameController { fun getArea(id: Int) = field.first { it.id == id } @Deprecated("Will be removed") - fun plantMinesExcept(safeId: Int) { - field = minefieldCreator.create(safeId) + fun plantMinesExcept(safeId: Int, includeSafeZone: Boolean = false) { + field = minefieldCreator.create(safeId, includeSafeZone) mines = field.filter { it.hasMine }.asSequence() - hasMines = true + hasMines = mines.count() != 0 } /** @@ -238,7 +235,7 @@ class GameController { val changed = when (actionResponse) { ActionResponse.OpenTile -> { if (!hasMines) { - plantMinesExcept(id) + plantMinesExcept(id, true) } if (mark.isNotNone()) { @@ -250,7 +247,7 @@ class GameController { } ActionResponse.SwitchMark -> { if (!hasMines) { - plantMinesExcept(id) + plantMinesExcept(id, true) openTile() } else if (isCovered) { switchMark() diff --git a/common/src/main/java/dev/lucasnlm/antimine/common/level/logic/MinefieldCreator.kt b/common/src/main/java/dev/lucasnlm/antimine/common/level/logic/MinefieldCreator.kt index 55dd8158..9b713960 100644 --- a/common/src/main/java/dev/lucasnlm/antimine/common/level/logic/MinefieldCreator.kt +++ b/common/src/main/java/dev/lucasnlm/antimine/common/level/logic/MinefieldCreator.kt @@ -31,10 +31,10 @@ class MinefieldCreator( return createMutableEmpty().toList() } - fun create(safeIndex: Int): List { + fun create(safeIndex: Int, safeZone: Boolean): List { return createMutableEmpty().apply { // Plant mines and setup number tips - filterNotNeighborsOf(safeIndex) + if (safeZone) { filterNotNeighborsOf(safeIndex) } else { filterNot { it.id == safeIndex } } .shuffled(randomGenerator) .take(minefield.mines) .onEach { diff --git a/common/src/test/java/dev/lucasnlm/antimine/common/level/GameControllerTest.kt b/common/src/test/java/dev/lucasnlm/antimine/common/level/GameControllerTest.kt index df17b712..a7a090dc 100644 --- a/common/src/test/java/dev/lucasnlm/antimine/common/level/GameControllerTest.kt +++ b/common/src/test/java/dev/lucasnlm/antimine/common/level/GameControllerTest.kt @@ -1,5 +1,6 @@ package dev.lucasnlm.antimine.common.level +import dev.lucasnlm.antimine.common.level.logic.filterNeighborsOf import dev.lucasnlm.antimine.common.level.models.Area import dev.lucasnlm.antimine.common.level.models.Mark import dev.lucasnlm.antimine.common.level.models.Minefield @@ -374,7 +375,7 @@ class GameControllerTest { field.map { if (it.isCovered) 1 else 0 }.toList() ) - // It won't open any if the mines were not flagged. + // It won't open any if the mines were flagged. singleClick(14) getArea(14).openNeighbors() assertEquals( @@ -389,7 +390,7 @@ class GameControllerTest { ) // After flag its neighbors, it must open all clean neighbors. - getArea(14).findNeighbors().filter { it.hasMine }.forEach { it.mark = Mark.Flag } + field.filterNeighborsOf(getArea(14)).filter { it.hasMine }.forEach { it.mark = Mark.Flag } getArea(14).openNeighbors() assertEquals( listOf( @@ -683,7 +684,7 @@ class GameControllerTest { updateGameControl(GameControl.fromControlType(ControlStyle.Standard)) singleClick(3) assertFalse(at(3).isCovered) - at(3).findNeighbors().forEach { + field.filterNeighborsOf(at(3)).forEach { assertTrue(it.isCovered) } @@ -693,7 +694,7 @@ class GameControllerTest { } longPress(3) - at(3).findNeighbors().forEach { + field.filterNeighborsOf(at(3)).forEach { if (it.hasMine) { assertTrue(it.isCovered) } else { @@ -726,7 +727,7 @@ class GameControllerTest { updateGameControl(GameControl.fromControlType(ControlStyle.FastFlag)) longPress(3) assertFalse(at(3).isCovered) - at(3).findNeighbors().forEach { + field.filterNeighborsOf(at(3)).forEach { assertTrue(it.isCovered) } @@ -736,7 +737,7 @@ class GameControllerTest { } singleClick(3) - at(3).findNeighbors().forEach { + field.filterNeighborsOf(at(3)).forEach { if (it.hasMine) { assertTrue(it.isCovered) } else { @@ -802,7 +803,7 @@ class GameControllerTest { updateGameControl(GameControl.fromControlType(ControlStyle.DoubleClick)) doubleClick(3) assertFalse(at(3).isCovered) - at(3).findNeighbors().forEach { + field.filterNeighborsOf(at(3)).forEach { assertTrue(it.isCovered) } @@ -812,7 +813,7 @@ class GameControllerTest { } doubleClick(3) - at(3).findNeighbors().forEach { + field.filterNeighborsOf(at(3)).forEach { if (it.hasMine) { assertTrue(it.isCovered) } else { @@ -830,7 +831,7 @@ class GameControllerTest { assertEquals(0, field.filterNot { it.isCovered }.count()) singleClick(40) assertTrue(hasMines) - at(40).findNeighbors().forEach { assertFalse(it.isCovered) } + field.filterNeighborsOf(at(40)).forEach { assertFalse(it.isCovered) } } } @@ -842,7 +843,7 @@ class GameControllerTest { assertEquals(0, field.filterNot { it.isCovered }.count()) singleClick(40) assertTrue(hasMines) - at(40).findNeighbors().forEach { assertFalse(it.isCovered) } + field.filterNeighborsOf(at(40)).forEach { assertFalse(it.isCovered) } } } } diff --git a/common/src/test/java/dev/lucasnlm/antimine/common/level/logic/MinefieldCreatorTest.kt b/common/src/test/java/dev/lucasnlm/antimine/common/level/logic/MinefieldCreatorTest.kt index 96263873..96659990 100644 --- a/common/src/test/java/dev/lucasnlm/antimine/common/level/logic/MinefieldCreatorTest.kt +++ b/common/src/test/java/dev/lucasnlm/antimine/common/level/logic/MinefieldCreatorTest.kt @@ -9,7 +9,7 @@ class MinefieldCreatorTest { @Test fun testMinefieldCreation() { val creator = MinefieldCreator(Minefield(4, 4, 9), Random(200)) - val map = creator.create(2) + val map = creator.create(2, true) assertEquals( listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), map.map { it.id }.toList() @@ -19,7 +19,7 @@ class MinefieldCreatorTest { @Test fun testMinefieldCreationMines() { val creator = MinefieldCreator(Minefield(5, 5, 99), Random(200)) - val map = creator.create(12) + val map = creator.create(12, true) assertEquals( listOf( 1, 1, 1, 1, 1, @@ -36,7 +36,7 @@ class MinefieldCreatorTest { @Test fun testMinefieldCreationMinesTips() { val creator = MinefieldCreator(Minefield(4, 4, 9), Random(200)) - val map = creator.create(2) + val map = creator.create(2, true) assertEquals( listOf( 0, 2, 0, 0, @@ -50,7 +50,7 @@ class MinefieldCreatorTest { @Test fun testMinefieldCreationPosition() { val creator = MinefieldCreator(Minefield(4, 4, 9), Random(200)) - val map = creator.create(2) + val map = creator.create(2, true) assertEquals( listOf( (0 to 0), (1 to 0), (2 to 0), (3 to 0),