Fix tests
|
@ -73,7 +73,12 @@ class ThemeAdapter(
|
|||
areas.forEach { it.alpha = 0.35f }
|
||||
|
||||
label.apply {
|
||||
text = if (position == 0) label.context.getString(R.string.system) else label.context.getString(R.string.amoled)
|
||||
text = if (position == 0) {
|
||||
label.context.getString(R.string.system)
|
||||
} else {
|
||||
label.context.getString(R.string.amoled)
|
||||
}
|
||||
|
||||
setTextColor(
|
||||
with(theme.palette.background) {
|
||||
Color.rgb(255 - Color.red(this), 255 - Color.green(this), 255 - Color.blue(this))
|
||||
|
|
Before Width: | Height: | Size: 664 B After Width: | Height: | Size: 695 B |
Before Width: | Height: | Size: 799 B After Width: | Height: | Size: 807 B |
Before Width: | Height: | Size: 864 B After Width: | Height: | Size: 925 B |
Before Width: | Height: | Size: 969 B After Width: | Height: | Size: 1 KiB |
Before Width: | Height: | Size: 647 B After Width: | Height: | Size: 689 B |
Before Width: | Height: | Size: 799 B After Width: | Height: | Size: 807 B |
Before Width: | Height: | Size: 664 B After Width: | Height: | Size: 695 B |
Before Width: | Height: | Size: 799 B After Width: | Height: | Size: 807 B |
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.6 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.7 KiB |
Before Width: | Height: | Size: 865 B After Width: | Height: | Size: 936 B |
Before Width: | Height: | Size: 1,001 B After Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.7 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 1.7 KiB After Width: | Height: | Size: 1.9 KiB |
Before Width: | Height: | Size: 1.7 KiB After Width: | Height: | Size: 1.6 KiB |
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.6 KiB |
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.9 KiB |
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.9 KiB |
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 1.7 KiB After Width: | Height: | Size: 743 B |
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.2 KiB |
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.7 KiB |
Before Width: | Height: | Size: 2.1 KiB After Width: | Height: | Size: 2.3 KiB |
Before Width: | Height: | Size: 2 KiB After Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.7 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 2 KiB After Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 2.1 KiB After Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 1.7 KiB After Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 2.5 KiB |
Before Width: | Height: | Size: 2 KiB After Width: | Height: | Size: 925 B |
Before Width: | Height: | Size: 635 B After Width: | Height: | Size: 654 B |
Before Width: | Height: | Size: 726 B After Width: | Height: | Size: 743 B |
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 925 B |
Before Width: | Height: | Size: 2.4 KiB After Width: | Height: | Size: 3 KiB |
Before Width: | Height: | Size: 2 KiB After Width: | Height: | Size: 2.6 KiB |
Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 3.1 KiB |
Before Width: | Height: | Size: 2 KiB After Width: | Height: | Size: 2.6 KiB |
|
@ -10,13 +10,11 @@ import dev.lucasnlm.antimine.common.level.logic.MinefieldHandler
|
|||
import dev.lucasnlm.antimine.common.level.logic.filterNeighborsOf
|
||||
import dev.lucasnlm.antimine.common.level.models.Area
|
||||
import dev.lucasnlm.antimine.common.level.models.Difficulty
|
||||
import dev.lucasnlm.antimine.common.level.models.Mark
|
||||
import dev.lucasnlm.antimine.common.level.models.Minefield
|
||||
import dev.lucasnlm.antimine.common.level.models.Score
|
||||
import dev.lucasnlm.antimine.common.level.solver.LimitedBruteForceSolver
|
||||
import dev.lucasnlm.antimine.core.control.ActionResponse
|
||||
import dev.lucasnlm.antimine.core.control.GameControl
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlin.random.Random
|
||||
|
||||
|
@ -53,6 +51,8 @@ class GameController {
|
|||
|
||||
fun field() = field
|
||||
|
||||
fun field(predicate: (Area) -> Boolean) = field.filter(predicate)
|
||||
|
||||
fun mines() = field.filter { it.hasMine }
|
||||
|
||||
fun hasMines() = field.firstOrNull { it.hasMine } != null
|
||||
|
@ -192,7 +192,7 @@ class GameController {
|
|||
|
||||
fun showWrongFlags() {
|
||||
field = field.map {
|
||||
if (it.mark.isNotNone() && !it.hasMine) {
|
||||
if (!it.hasMine && it.mark.isFlag()) {
|
||||
it.copy(mistake = true)
|
||||
} else {
|
||||
it
|
||||
|
|
|
@ -8,14 +8,13 @@ class MinefieldHandler(
|
|||
private val useQuestionMark: Boolean
|
||||
) {
|
||||
fun showAllMines() {
|
||||
field.filter { it.hasMine && it.mark != Mark.Flag}
|
||||
field.filter { it.hasMine && it.mark != Mark.Flag }
|
||||
.forEach { field[it.id] = it.copy(isCovered = false) }
|
||||
}
|
||||
|
||||
fun flagAllMines() {
|
||||
field.filter { it.hasMine }
|
||||
.forEach { field[it.id] = it.copy(mark = Mark.Flag) }
|
||||
|
||||
}
|
||||
|
||||
fun revealAllEmptyAreas() {
|
||||
|
@ -86,16 +85,15 @@ class MinefieldHandler(
|
|||
if (flaggedCount >= minesAround) {
|
||||
neighbors
|
||||
.filter { it.isCovered && it.mark.isNone() }
|
||||
.onEach { openAt(it.id, passive = false, openNeighbors = true) }
|
||||
.count()
|
||||
.forEach { openAt(it.id, passive = false, openNeighbors = true) }
|
||||
} else {
|
||||
val coveredNeighbors = neighbors.filter { it.isCovered }
|
||||
if (coveredNeighbors.count() == minesAround) {
|
||||
coveredNeighbors.filter {
|
||||
it.mark.isNone()
|
||||
}.onEach {
|
||||
}.forEach {
|
||||
switchMarkAt(it.id)
|
||||
}.count()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,9 +9,7 @@ open class BruteForceSolver : GameSolver() {
|
|||
|
||||
do {
|
||||
val initialMap = minefield.filter { !it.isCovered && it.minesAround != 0 }
|
||||
initialMap.forEach {
|
||||
minefieldHandler.openOrFlagNeighborsOf(it.id)
|
||||
}
|
||||
initialMap.forEach { minefieldHandler.openOrFlagNeighborsOf(it.id) }
|
||||
} while (initialMap != minefield.filter { !it.isCovered && it.minesAround != 0 })
|
||||
|
||||
return minefield.count { it.hasMine && !it.mark.isFlag() } == 0
|
||||
|
|
|
@ -7,7 +7,6 @@ import dev.lucasnlm.antimine.common.R
|
|||
import dev.lucasnlm.antimine.common.level.GameController
|
||||
import dev.lucasnlm.antimine.common.level.database.models.FirstOpen
|
||||
import dev.lucasnlm.antimine.common.level.database.models.Save
|
||||
import dev.lucasnlm.antimine.common.level.logic.MinefieldHandler
|
||||
import dev.lucasnlm.antimine.common.level.models.Area
|
||||
import dev.lucasnlm.antimine.common.level.models.Difficulty
|
||||
import dev.lucasnlm.antimine.common.level.models.Event
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package dev.lucasnlm.antimine.core.di
|
||||
|
||||
import android.content.Context
|
||||
import android.view.ViewConfiguration
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
|
@ -31,7 +32,7 @@ class CommonModule {
|
|||
@Provides
|
||||
fun providePreferencesRepository(
|
||||
preferencesManager: PreferencesManager
|
||||
): IPreferencesRepository = PreferencesRepository(preferencesManager)
|
||||
): IPreferencesRepository = PreferencesRepository(preferencesManager, ViewConfiguration.getLongPressTimeout())
|
||||
|
||||
@Provides
|
||||
fun providePreferencesInteractor(
|
||||
|
|
|
@ -37,7 +37,8 @@ interface IPreferencesRepository {
|
|||
}
|
||||
|
||||
class PreferencesRepository(
|
||||
private val preferencesManager: IPreferencesManager
|
||||
private val preferencesManager: IPreferencesManager,
|
||||
private val defaultLongPressTimeout: Int
|
||||
) : IPreferencesRepository {
|
||||
init {
|
||||
migrateOldPreferences()
|
||||
|
@ -151,7 +152,7 @@ class PreferencesRepository(
|
|||
}
|
||||
|
||||
if (!preferencesManager.contains(PREFERENCE_LONG_PRESS_TIMEOUT)) {
|
||||
preferencesManager.putInt(PREFERENCE_LONG_PRESS_TIMEOUT, ViewConfiguration.getLongPressTimeout())
|
||||
preferencesManager.putInt(PREFERENCE_LONG_PRESS_TIMEOUT, defaultLongPressTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -138,25 +138,31 @@ class GameControllerTest {
|
|||
@Test
|
||||
fun testShowWrongFlags() = runBlockingTest {
|
||||
withGameController { controller ->
|
||||
controller.field().first { !it.hasMine }
|
||||
.also { controller.fakeLongPress(it.id) }
|
||||
controller.field().first { !it.hasMine && it.isCovered }.run {
|
||||
controller.fakeLongPress(id)
|
||||
}
|
||||
|
||||
val wrongFlag = controller.field().first { !it.hasMine }
|
||||
controller.mines().first().run {
|
||||
controller.fakeLongPress(id)
|
||||
}
|
||||
|
||||
//val rightFlag = controller.mines().first().apply { controller.fakeLongPress(id) }
|
||||
controller.showWrongFlags()
|
||||
|
||||
val wrongFlag = controller.field().first { !it.hasMine && it.isCovered }
|
||||
val rightFlag = controller.mines().first()
|
||||
|
||||
assertTrue(wrongFlag.mistake)
|
||||
//assertFalse(rightFlag.mistake)
|
||||
assertFalse(rightFlag.mistake)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRevealAllEmptyAreas() = runBlockingTest {
|
||||
withGameController { controller ->
|
||||
val covered = controller.field().filter { it.isCovered }
|
||||
val covered = controller.field { it.isCovered }
|
||||
assertTrue(covered.isNotEmpty())
|
||||
controller.revealAllEmptyAreas()
|
||||
assertEquals(controller.field().filter { it.hasMine }, controller.field().filter { it.isCovered })
|
||||
assertEquals(controller.mines(), controller.field { it.isCovered })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -187,7 +193,14 @@ class GameControllerTest {
|
|||
assertEquals(20, controller.remainingMines())
|
||||
|
||||
repeat(20) { flagCount ->
|
||||
controller.field().filter { it.hasMine }.take(flagCount).forEach { controller.fakeLongPress(it.id) }
|
||||
controller.field()
|
||||
.filter { it.hasMine }
|
||||
.take(flagCount)
|
||||
.forEach {
|
||||
if (!it.mark.isFlag()) {
|
||||
controller.fakeLongPress(it.id)
|
||||
}
|
||||
}
|
||||
assertEquals("flagging $flagCount mines", 20 - flagCount, controller.remainingMines())
|
||||
}
|
||||
}
|
||||
|
@ -235,15 +248,16 @@ class GameControllerTest {
|
|||
withGameController { controller ->
|
||||
assertFalse(controller.checkVictory())
|
||||
|
||||
controller.field()
|
||||
.filter { it.hasMine }
|
||||
.forEach { controller.fakeLongPress(it.id) }
|
||||
controller.mines().forEach { controller.fakeLongPress(it.id) }
|
||||
assertFalse(controller.checkVictory())
|
||||
|
||||
controller.field().filterNot { it.hasMine }.forEach { controller.fakeSingleClick(it.id) }
|
||||
controller.field { !it.hasMine }.forEach { controller.fakeSingleClick(it.id) }
|
||||
assertTrue(controller.checkVictory())
|
||||
|
||||
controller.field().first { it.hasMine }.also { controller.fakeSingleClick(it.id) }
|
||||
controller.mines().first().run {
|
||||
controller.fakeSingleClick(id)
|
||||
controller.fakeSingleClick(id)
|
||||
}
|
||||
assertFalse(controller.checkVictory())
|
||||
}
|
||||
}
|
||||
|
@ -300,16 +314,17 @@ class GameControllerTest {
|
|||
updateGameControl(GameControl.fromControlType(ControlStyle.Standard))
|
||||
fakeSingleClick(14)
|
||||
assertFalse(at(14).isCovered)
|
||||
|
||||
field().filterNeighborsOf(at(14)).forEach {
|
||||
assertTrue(it.isCovered)
|
||||
}
|
||||
|
||||
field().filter { it.hasMine }.forEach {
|
||||
fakeLongPress(it.id)
|
||||
assertTrue(it.mark.isFlag())
|
||||
}
|
||||
mines().forEach { fakeLongPress(it.id) }
|
||||
|
||||
mines().forEach { assertTrue(it.mark.isFlag()) }
|
||||
|
||||
fakeLongPress(14)
|
||||
|
||||
field().filterNeighborsOf(at(14)).forEach {
|
||||
if (it.hasMine) {
|
||||
assertTrue(it.isCovered)
|
||||
|
@ -373,12 +388,16 @@ class GameControllerTest {
|
|||
updateGameControl(GameControl.fromControlType(ControlStyle.FastFlag))
|
||||
fakeLongPress(14)
|
||||
assertFalse(at(14).isCovered)
|
||||
|
||||
field().filterNeighborsOf(at(14)).forEach {
|
||||
assertTrue(it.isCovered)
|
||||
}
|
||||
|
||||
field().filter { it.hasMine }.forEach {
|
||||
mines().forEach {
|
||||
fakeSingleClick(it.id)
|
||||
}
|
||||
|
||||
mines().forEach {
|
||||
assertTrue(it.mark.isFlag())
|
||||
}
|
||||
|
||||
|
@ -456,9 +475,9 @@ class GameControllerTest {
|
|||
assertTrue(it.isCovered)
|
||||
}
|
||||
|
||||
field().filter { it.hasMine }
|
||||
.onEach { fakeSingleClick(it.id) }
|
||||
.onEach { assertTrue(it.mark.isFlag()) }
|
||||
mines().forEach { fakeSingleClick(it.id) }
|
||||
|
||||
mines().forEach { assertTrue(it.mark.isFlag()) }
|
||||
|
||||
fakeDoubleClick(14)
|
||||
field().filterNeighborsOf(at(14)).forEach {
|
||||
|
|
|
@ -73,6 +73,7 @@ class MinefieldFactoryTest {
|
|||
fun testFromDifficultyPresetStandard() {
|
||||
val dimensionRepository: IDimensionRepository = mock {
|
||||
on { areaSize() } doReturn 10.0f
|
||||
on { defaultAreaSize() } doReturn 10.0f
|
||||
on { actionBarSize() } doReturn 10
|
||||
on { displaySize() } doReturn Size(500, 1000)
|
||||
}
|
||||
|
@ -84,7 +85,7 @@ class MinefieldFactoryTest {
|
|||
).run {
|
||||
assertEquals(49, width)
|
||||
assertEquals(96, height)
|
||||
assertEquals(950, mines)
|
||||
assertEquals(940, mines)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package dev.lucasnlm.antimine.common.level.logic
|
||||
|
||||
import dev.lucasnlm.antimine.common.level.models.Area
|
||||
import dev.lucasnlm.antimine.common.level.models.Mark
|
||||
import dev.lucasnlm.antimine.common.level.models.Minefield
|
||||
import org.junit.Assert.assertEquals
|
||||
|
@ -13,138 +12,162 @@ class MinefieldHandlerTest {
|
|||
private fun handleMinefield(
|
||||
useQuestionMark: Boolean = false,
|
||||
useSafeZone: Boolean = false,
|
||||
block: (MinefieldHandler, MutableList<Area>) -> Unit
|
||||
block: (MinefieldHandler) -> Unit
|
||||
) {
|
||||
val creator = MinefieldCreator(Minefield(4, 4, 9), Random(200))
|
||||
val minefield = creator.create(10, useSafeZone).toMutableList()
|
||||
val minefieldHandler = MinefieldHandler(minefield, useQuestionMark)
|
||||
block(minefieldHandler, minefield)
|
||||
block(minefieldHandler)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOpenArea() {
|
||||
handleMinefield { handler, minefield ->
|
||||
assertTrue(minefield[3].isCovered)
|
||||
handleMinefield { handler ->
|
||||
assertTrue(handler.result()[3].isCovered)
|
||||
handler.openAt(3, false, openNeighbors = false)
|
||||
assertFalse(minefield[3].isCovered)
|
||||
assertEquals(Mark.None, minefield[3].mark)
|
||||
assertFalse(handler.result()[3].isCovered)
|
||||
assertEquals(Mark.None, handler.result()[3].mark)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOpenAreaWithSafeZone() {
|
||||
handleMinefield(useSafeZone = true) { handler, minefield ->
|
||||
assertTrue(minefield[3].isCovered)
|
||||
handleMinefield(useSafeZone = true) { handler ->
|
||||
assertTrue(handler.result()[3].isCovered)
|
||||
handler.openAt(3, false, openNeighbors = false)
|
||||
assertFalse(minefield[3].isCovered)
|
||||
assertEquals(Mark.None, minefield[3].mark)
|
||||
assertFalse(handler.result()[3].isCovered)
|
||||
assertEquals(Mark.None, handler.result()[3].mark)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testTurnOffHighlight() {
|
||||
handleMinefield { handler, minefield ->
|
||||
minefield[3] = minefield[3].copy(highlighted = true)
|
||||
handleMinefield { handler ->
|
||||
handler.highlightAt(3)
|
||||
handler.turnOffAllHighlighted()
|
||||
assertFalse(minefield[3].highlighted)
|
||||
assertFalse(handler.result()[3].highlighted)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRemoveMark() {
|
||||
handleMinefield { handler, minefield ->
|
||||
minefield[3] = minefield[3].copy(mark = Mark.Flag)
|
||||
handleMinefield { handler ->
|
||||
handler.switchMarkAt(3)
|
||||
|
||||
handler.removeMarkAt(3)
|
||||
assertTrue(minefield[3].mark == Mark.PurposefulNone)
|
||||
assertTrue(minefield[3].mark.isNone())
|
||||
assertTrue(handler.result()[3].mark == Mark.PurposefulNone)
|
||||
assertTrue(handler.result()[3].mark.isNone())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSwitchMarkWithoutQuestionMark() {
|
||||
handleMinefield { handler, minefield ->
|
||||
assertTrue(minefield[3].mark.isNone())
|
||||
handleMinefield { handler ->
|
||||
assertTrue(handler.result()[3].mark.isNone())
|
||||
|
||||
handler.switchMarkAt(3)
|
||||
assertTrue(minefield[3].mark.isFlag())
|
||||
assertTrue(handler.result()[3].mark.isFlag())
|
||||
|
||||
handler.switchMarkAt(3)
|
||||
assertTrue(minefield[3].mark.isNone())
|
||||
assertTrue(handler.result()[3].mark.isNone())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSwitchMarkWithQuestionMark() {
|
||||
handleMinefield(useQuestionMark = true) { handler, minefield ->
|
||||
assertTrue(minefield[3].mark.isNone())
|
||||
handleMinefield(useQuestionMark = true) { handler ->
|
||||
assertTrue(handler.result()[3].mark.isNone())
|
||||
|
||||
handler.switchMarkAt(3)
|
||||
assertTrue(minefield[3].mark.isFlag())
|
||||
assertTrue(handler.result()[3].mark.isFlag())
|
||||
|
||||
handler.switchMarkAt(3)
|
||||
assertTrue(minefield[3].mark.isQuestion())
|
||||
assertTrue(handler.result()[3].mark.isQuestion())
|
||||
|
||||
handler.switchMarkAt(3)
|
||||
assertTrue(minefield[3].mark.isNone())
|
||||
assertTrue(handler.result()[3].mark.isNone())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHighlight() {
|
||||
handleMinefield(useQuestionMark = true) { handler, minefield ->
|
||||
assertEquals(0, minefield.count { it.highlighted })
|
||||
handleMinefield(useQuestionMark = true) { handler ->
|
||||
assertEquals(0, handler.result().count { it.highlighted })
|
||||
|
||||
// Before open
|
||||
handler.highlightAt(5)
|
||||
assertEquals(0, minefield.count { it.highlighted })
|
||||
assertEquals(0, handler.result().count { it.highlighted })
|
||||
|
||||
// After Open
|
||||
handler.openAt(5, false, openNeighbors = false)
|
||||
val target = minefield.first { it.minesAround != 0 }
|
||||
val target = handler.result().first { it.minesAround != 0 }
|
||||
handler.highlightAt(target.id)
|
||||
assertEquals(5, minefield.count { it.highlighted })
|
||||
assertEquals(listOf(0, 1, 4, 8, 9), minefield.filter { it.highlighted }.map { it.id }.toList())
|
||||
assertEquals(5, handler.result().count { it.highlighted })
|
||||
assertEquals(listOf(0, 1, 4, 8, 9), handler.result().filter { it.highlighted }.map { it.id }.toList())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOpenNeighborsClosedArea() {
|
||||
handleMinefield { handler, minefield ->
|
||||
handleMinefield { handler ->
|
||||
handler.openOrFlagNeighborsOf(3)
|
||||
assertEquals(0, minefield.count { !it.isCovered })
|
||||
assertEquals(0, handler.result().count { !it.isCovered })
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOpenNeighbors() {
|
||||
handleMinefield { handler, minefield ->
|
||||
handleMinefield { handler ->
|
||||
handler.openAt(5, false, openNeighbors = true)
|
||||
handler.openOrFlagNeighborsOf(5)
|
||||
assertEquals(9, minefield.count { !it.isCovered })
|
||||
assertEquals(9, handler.result().count { !it.isCovered })
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOpenNeighborsWithFlags() {
|
||||
handleMinefield { handler, minefield ->
|
||||
handleMinefield { handler ->
|
||||
handler.openAt(5, false, openNeighbors = true)
|
||||
val neighbors = minefield.filterNeighborsOf(minefield.first { it.id == 5 })
|
||||
neighbors.filter { it.hasMine }.forEach { handler.switchMarkAt(it.id) }
|
||||
|
||||
handler.result()
|
||||
.filterNeighborsOf(handler.result().first { it.id == 5 })
|
||||
.filter { it.hasMine }
|
||||
.forEach { handler.switchMarkAt(it.id) }
|
||||
|
||||
handler.openOrFlagNeighborsOf(5)
|
||||
assertEquals(4, minefield.count { !it.isCovered })
|
||||
assertEquals(3, neighbors.count { !it.isCovered })
|
||||
|
||||
val remainCovered =
|
||||
handler.result().count { !it.isCovered }
|
||||
val remainCoveredNeighbors =
|
||||
handler.result().filterNeighborsOf(handler.result().first { it.id == 5 }).count { !it.isCovered }
|
||||
|
||||
assertEquals(4, remainCovered)
|
||||
assertEquals(3, remainCoveredNeighbors)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOpenNeighborsWithQuestionMarks() {
|
||||
handleMinefield(useQuestionMark = true) { handler, minefield ->
|
||||
handler.openAt(5, false, openNeighbors = true)
|
||||
val neighbors = minefield.filterNeighborsOf(minefield.first { it.id == 5 })
|
||||
neighbors
|
||||
handleMinefield(useQuestionMark = true) { handler ->
|
||||
handler.openAt(5, false, openNeighbors = false)
|
||||
|
||||
handler.result()
|
||||
.filterNeighborsOf(handler.result().first { it.id == 5 })
|
||||
.filter { it.hasMine }
|
||||
.forEach {
|
||||
handler.switchMarkAt(it.id)
|
||||
handler.switchMarkAt(it.id)
|
||||
}
|
||||
|
||||
handler.openOrFlagNeighborsOf(5)
|
||||
assertEquals(4, minefield.count { !it.isCovered })
|
||||
assertEquals(3, neighbors.count { !it.isCovered })
|
||||
|
||||
val remainCovered =
|
||||
handler.result().count { !it.isCovered }
|
||||
val remainCoveredNeighbors =
|
||||
handler.result().filterNeighborsOf(handler.result().first { it.id == 5 }).count { !it.isCovered }
|
||||
|
||||
assertEquals(4, remainCovered)
|
||||
assertEquals(3, remainCoveredNeighbors)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ 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.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
|
@ -11,7 +10,7 @@ import org.junit.Test
|
|||
import kotlin.random.Random
|
||||
|
||||
class BruteForceSolverTest {
|
||||
private fun handleMinefield(block: (MinefieldHandler, MutableList<Area>) -> Unit) {
|
||||
private fun handleMinefield(block: (MinefieldHandler) -> Unit) {
|
||||
val creator = MinefieldCreator(
|
||||
Minefield(9, 9, 12),
|
||||
Random(200)
|
||||
|
@ -19,21 +18,21 @@ class BruteForceSolverTest {
|
|||
val minefield = creator.create(40, true).toMutableList()
|
||||
val minefieldHandler =
|
||||
MinefieldHandler(minefield, false)
|
||||
block(minefieldHandler, minefield)
|
||||
block(minefieldHandler)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isSolvable() {
|
||||
handleMinefield { handler, minefield ->
|
||||
handler.openAt(40, passive = false, openNeighbors = false)
|
||||
handleMinefield { handler ->
|
||||
handler.openAt(40, passive = false, openNeighbors = true)
|
||||
val bruteForceSolver = BruteForceSolver()
|
||||
assertTrue(bruteForceSolver.trySolve(minefield.toMutableList()))
|
||||
assertTrue(bruteForceSolver.trySolve(handler.result().toMutableList()))
|
||||
}
|
||||
|
||||
handleMinefield { handler, minefield ->
|
||||
handleMinefield { handler ->
|
||||
handler.openAt(0, passive = false, openNeighbors = false)
|
||||
val bruteForceSolver = BruteForceSolver()
|
||||
assertFalse(bruteForceSolver.trySolve(minefield.toMutableList()))
|
||||
assertFalse(bruteForceSolver.trySolve(handler.result().toMutableList()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ 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.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
|
@ -11,41 +10,40 @@ import java.lang.Thread.sleep
|
|||
import kotlin.random.Random
|
||||
|
||||
class LimitedBruteForceSolverTest {
|
||||
private fun handleMinefield(block: (MinefieldHandler, MutableList<Area>) -> Unit) {
|
||||
private fun handleMinefield(block: (MinefieldHandler) -> Unit) {
|
||||
val creator = MinefieldCreator(
|
||||
Minefield(9, 9, 12),
|
||||
Random(200)
|
||||
)
|
||||
val minefield = creator.create(40, true).toMutableList()
|
||||
val minefieldHandler =
|
||||
MinefieldHandler(minefield, false)
|
||||
block(minefieldHandler, minefield)
|
||||
val minefieldHandler = MinefieldHandler(minefield, false)
|
||||
block(minefieldHandler)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isSolvable() {
|
||||
handleMinefield { handler, minefield ->
|
||||
handler.openAt(40, passive = false, openNeighbors = false)
|
||||
handleMinefield { handler ->
|
||||
handler.openAt(40, passive = false, openNeighbors = true)
|
||||
val bruteForceSolver = LimitedBruteForceSolver()
|
||||
assertTrue(bruteForceSolver.trySolve(minefield.toMutableList()))
|
||||
assertTrue(bruteForceSolver.trySolve(handler.result().toMutableList()))
|
||||
}
|
||||
|
||||
handleMinefield { handler, minefield ->
|
||||
handler.openAt(0, passive = false, openNeighbors = false)
|
||||
handleMinefield { handler ->
|
||||
handler.openAt(0, passive = false, openNeighbors = true)
|
||||
val bruteForceSolver = LimitedBruteForceSolver()
|
||||
assertFalse(bruteForceSolver.trySolve(minefield.toMutableList()))
|
||||
assertFalse(bruteForceSolver.trySolve(handler.result().toMutableList()))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shouldntKeepTryingAfterTimeout() {
|
||||
handleMinefield { handler, _ ->
|
||||
handleMinefield { handler ->
|
||||
handler.openAt(40, passive = false, openNeighbors = false)
|
||||
val bruteForceSolver = LimitedBruteForceSolver(1000L)
|
||||
assertTrue(bruteForceSolver.keepTrying())
|
||||
}
|
||||
|
||||
handleMinefield { handler, _ ->
|
||||
handleMinefield { handler ->
|
||||
handler.openAt(0, passive = false, openNeighbors = false)
|
||||
val bruteForceSolver = LimitedBruteForceSolver(50)
|
||||
sleep(100)
|
||||
|
|
|
@ -37,7 +37,7 @@ class PreferencesRepositoryTest {
|
|||
@Test
|
||||
fun testProgressValue() {
|
||||
val preferenceManager = TestPreferenceManager()
|
||||
val preferencesRepository = PreferencesRepository(preferenceManager)
|
||||
val preferencesRepository = PreferencesRepository(preferenceManager, 400)
|
||||
|
||||
assertEquals(0, preferencesRepository.getProgressiveValue())
|
||||
|
||||
|
@ -63,7 +63,7 @@ class PreferencesRepositoryTest {
|
|||
preferenceManager.putBoolean("preference_double_click_open", true)
|
||||
assertTrue(preferenceManager.values["preference_double_click_open"] as Boolean)
|
||||
|
||||
val preferencesRepository = PreferencesRepository(preferenceManager)
|
||||
val preferencesRepository = PreferencesRepository(preferenceManager, 400)
|
||||
|
||||
assertTrue(preferenceManager.values["preference_double_click_open"] == null)
|
||||
assertEquals(1, preferenceManager.values["preference_control_style"])
|
||||
|
@ -76,7 +76,7 @@ class PreferencesRepositoryTest {
|
|||
preferenceManager.putBoolean("preference_double_click_open", false)
|
||||
assertFalse(preferenceManager.values["preference_double_click_open"] as Boolean)
|
||||
|
||||
val preferencesRepository = PreferencesRepository(preferenceManager)
|
||||
val preferencesRepository = PreferencesRepository(preferenceManager, 400)
|
||||
|
||||
assertTrue(preferenceManager.values["preference_double_click_open"] == null)
|
||||
assertFalse(preferencesRepository.getBoolean("preference_double_click_open", false))
|
||||
|
@ -88,7 +88,7 @@ class PreferencesRepositoryTest {
|
|||
preferenceManager.putBoolean("preference_large_area", true)
|
||||
assertTrue(preferenceManager.values["preference_large_area"] as Boolean)
|
||||
|
||||
val preferencesRepository = PreferencesRepository(preferenceManager)
|
||||
val preferencesRepository = PreferencesRepository(preferenceManager, 400)
|
||||
|
||||
assertTrue(preferenceManager.values["preference_large_area"] == null)
|
||||
assertEquals(63, preferencesRepository.getInt("preference_area_size", -1))
|
||||
|
@ -97,7 +97,7 @@ class PreferencesRepositoryTest {
|
|||
@Test
|
||||
fun testMigrationLargeAreaOff() {
|
||||
val preferenceManager = TestPreferenceManager()
|
||||
val preferencesRepository = PreferencesRepository(preferenceManager)
|
||||
val preferencesRepository = PreferencesRepository(preferenceManager, 400)
|
||||
|
||||
assertTrue(preferenceManager.values["preference_large_area"] == null)
|
||||
assertEquals(50, preferencesRepository.getInt("preference_area_size", -1))
|
||||
|
@ -109,7 +109,7 @@ class PreferencesRepositoryTest {
|
|||
preferenceManager.putBoolean("preference_large_area", false)
|
||||
assertEquals(false, preferenceManager.values["preference_large_area"] as Boolean)
|
||||
|
||||
val preferencesRepository = PreferencesRepository(preferenceManager)
|
||||
val preferencesRepository = PreferencesRepository(preferenceManager, 400)
|
||||
|
||||
assertTrue(preferenceManager.values["preference_large_area"] == null)
|
||||
assertEquals(50, preferencesRepository.getInt("preference_area_size", -1))
|
||||
|
|