Add abstract game solver
This commit is contained in:
parent
12e7acb8c6
commit
639f1abafb
4 changed files with 33 additions and 12 deletions
|
@ -4,7 +4,7 @@ import dev.lucasnlm.antimine.common.level.database.models.FirstOpen
|
|||
import dev.lucasnlm.antimine.common.level.database.models.Save
|
||||
import dev.lucasnlm.antimine.common.level.database.models.SaveStatus
|
||||
import dev.lucasnlm.antimine.common.level.database.models.Stats
|
||||
import dev.lucasnlm.antimine.common.level.logic.BruteForceSolver
|
||||
import dev.lucasnlm.antimine.common.level.solver.BruteForceSolver
|
||||
import dev.lucasnlm.antimine.common.level.logic.FlagAssistant
|
||||
import dev.lucasnlm.antimine.common.level.logic.MinefieldCreator
|
||||
import dev.lucasnlm.antimine.common.level.logic.MinefieldHandler
|
||||
|
@ -72,7 +72,9 @@ class GameController {
|
|||
val fieldCopy = field.map { it.copy() }.toMutableList()
|
||||
val minefieldHandler = MinefieldHandler(fieldCopy, false)
|
||||
minefieldHandler.openAt(safeId)
|
||||
} while (!BruteForceSolver(minefieldHandler.result().toMutableList()).isSolvable())
|
||||
} while (!BruteForceSolver(
|
||||
minefieldHandler.result().toMutableList()
|
||||
).isSolvable())
|
||||
|
||||
mines = field.filter { it.hasMine }.asSequence()
|
||||
firstOpen = FirstOpen.Position(safeId)
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
package dev.lucasnlm.antimine.common.level.logic
|
||||
package dev.lucasnlm.antimine.common.level.solver
|
||||
|
||||
import dev.lucasnlm.antimine.common.level.logic.MinefieldHandler
|
||||
import dev.lucasnlm.antimine.common.level.models.Area
|
||||
|
||||
class BruteForceSolver(
|
||||
private val minefield: MutableList<Area>
|
||||
) {
|
||||
private val minefieldHandler = MinefieldHandler(minefield, false)
|
||||
minefield: MutableList<Area>
|
||||
): GameSolver(minefield) {
|
||||
private val minefieldHandler =
|
||||
MinefieldHandler(minefield, false)
|
||||
|
||||
fun isSolvable(): Boolean {
|
||||
override fun isSolvable(): Boolean {
|
||||
do {
|
||||
val initialMap = minefield.filter { !it.isCovered && it.minesAround != 0 }
|
||||
initialMap.forEach {
|
|
@ -0,0 +1,9 @@
|
|||
package dev.lucasnlm.antimine.common.level.solver
|
||||
|
||||
import dev.lucasnlm.antimine.common.level.models.Area
|
||||
|
||||
abstract class GameSolver(
|
||||
protected val minefield: MutableList<Area>
|
||||
) {
|
||||
abstract fun isSolvable(): Boolean
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package dev.lucasnlm.antimine.common.level.logic
|
||||
package dev.lucasnlm.antimine.common.level.solver
|
||||
|
||||
import dev.lucasnlm.antimine.common.level.logic.MinefieldCreator
|
||||
import dev.lucasnlm.antimine.common.level.logic.MinefieldHandler
|
||||
import dev.lucasnlm.antimine.common.level.models.Area
|
||||
import dev.lucasnlm.antimine.common.level.models.Minefield
|
||||
import org.junit.Test
|
||||
|
@ -9,9 +11,13 @@ import kotlin.random.Random
|
|||
|
||||
class BruteForceSolverTest {
|
||||
private fun handleMinefield(block: (MinefieldHandler, MutableList<Area>) -> Unit) {
|
||||
val creator = MinefieldCreator(Minefield(9, 9, 12), Random(200))
|
||||
val creator = MinefieldCreator(
|
||||
Minefield(9, 9, 12),
|
||||
Random(200)
|
||||
)
|
||||
val minefield = creator.create(40, true).toMutableList()
|
||||
val minefieldHandler = MinefieldHandler(minefield, false)
|
||||
val minefieldHandler =
|
||||
MinefieldHandler(minefield, false)
|
||||
block(minefieldHandler, minefield)
|
||||
}
|
||||
|
||||
|
@ -19,13 +25,15 @@ class BruteForceSolverTest {
|
|||
fun isSolvable() {
|
||||
handleMinefield { handler, minefield ->
|
||||
handler.openAt(40)
|
||||
val bruteForceSolver = BruteForceSolver(minefield.toMutableList())
|
||||
val bruteForceSolver =
|
||||
BruteForceSolver(minefield.toMutableList())
|
||||
assertTrue(bruteForceSolver.isSolvable())
|
||||
}
|
||||
|
||||
handleMinefield { handler, minefield ->
|
||||
handler.openAt(0)
|
||||
val bruteForceSolver = BruteForceSolver(minefield.toMutableList())
|
||||
val bruteForceSolver =
|
||||
BruteForceSolver(minefield.toMutableList())
|
||||
assertFalse(bruteForceSolver.isSolvable())
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue