Add solvable maps to settings

This commit is contained in:
Lucas Lima 2020-07-16 23:51:50 -03:00
parent 6e1c0ec60b
commit d7ea60d895
No known key found for this signature in database
GPG key ID: C5EEF4C30BFBF8D7
5 changed files with 33 additions and 12 deletions

View file

@ -33,6 +33,7 @@ class GameController {
private var gameControl: GameControl = GameControl.Standard
private var mines: Sequence<Area> = emptySequence()
private var useQuestionMark = true
private var useSolverAlgorithms = false
var hasMines = false
private set
@ -67,14 +68,16 @@ class GameController {
private fun getArea(id: Int) = field.first { it.id == id }
private fun plantMinesExcept(safeId: Int) {
do {
field = minefieldCreator.create(safeId, false)
val fieldCopy = field.map { it.copy() }.toMutableList()
val minefieldHandler = MinefieldHandler(fieldCopy, false)
minefieldHandler.openAt(safeId)
} while (!BruteForceSolver(
minefieldHandler.result().toMutableList()
).isSolvable())
if (useSolverAlgorithms) {
do {
field = minefieldCreator.create(safeId, false)
val fieldCopy = field.map { it.copy() }.toMutableList()
val minefieldHandler = MinefieldHandler(fieldCopy, false)
minefieldHandler.openAt(safeId)
} while (!BruteForceSolver(minefieldHandler.result().toMutableList()).isSolvable())
} else {
field = minefieldCreator.create(safeId, true)
}
mines = field.filter { it.hasMine }.asSequence()
firstOpen = FirstOpen.Position(safeId)
@ -266,4 +269,8 @@ class GameController {
fun useQuestionMark(useQuestionMark: Boolean) {
this.useQuestionMark = useQuestionMark
}
fun useSolverAlgorithms(useSolverAlgorithms: Boolean) {
this.useSolverAlgorithms = useSolverAlgorithms
}
}

View file

@ -342,12 +342,13 @@ class GameViewModel @ViewModelInject constructor(
}
fun refreshUserPreferences() {
val questionMark = preferencesRepository.useQuestionMark()
val controlType = preferencesRepository.controlStyle()
val gameControl = GameControl.fromControlType(controlType)
gameController.apply {
val controlType = preferencesRepository.controlStyle()
val gameControl = GameControl.fromControlType(controlType)
updateGameControl(gameControl)
useQuestionMark(questionMark)
useQuestionMark(preferencesRepository.useQuestionMark())
useSolverAlgorithms(preferencesRepository.useSolverAlgorithms())
}
}

View file

@ -24,6 +24,7 @@ interface IPreferencesRepository {
fun useAnimations(): Boolean
fun useQuestionMark(): Boolean
fun isSoundEffectsEnabled(): Boolean
fun useSolverAlgorithms(): Boolean
}
class PreferencesRepository(
@ -77,6 +78,9 @@ class PreferencesRepository(
override fun isSoundEffectsEnabled(): Boolean =
getBoolean(PREFERENCE_SOUND_EFFECTS, false)
override fun useSolverAlgorithms(): Boolean =
getBoolean(PREFERENCE_USE_SOLVER_ALGORITHMS, false)
override fun controlStyle(): ControlStyle {
val index = getInt(PREFERENCE_CONTROL_STYLE, -1)
return ControlStyle.values().getOrNull(index) ?: ControlStyle.Standard
@ -116,5 +120,6 @@ class PreferencesRepository(
private const val PREFERENCE_CUSTOM_GAME_MINES = "preference_custom_game_mines"
private const val PREFERENCE_SOUND_EFFECTS = "preference_sound"
private const val PREFERENCE_STATS_BASE = "preference_stats_base"
private const val PREFERENCE_USE_SOLVER_ALGORITHMS = "preference_use_solver_algorithms"
}
}

View file

@ -48,6 +48,7 @@
<string name="quit">Quit</string>
<string name="are_you_sure">Are you sure?</string>
<string name="enable_automatic_flags">Enable automatic placing of flags</string>
<string name="enable_no_guessing_maps">Find solvable maps</string>
<string name="open_areas">Open Areas</string>
<string name="total_time">Total Time</string>
<string name="average_time">Average Time</string>

View file

@ -21,6 +21,13 @@
android:title="@string/enable_automatic_flags"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
android:checked="false"
android:defaultValue="false"
android:key="preference_use_solver_algorithms"
android:title="@string/enable_no_guessing_maps"
app:iconSpaceReserved="false" />
</PreferenceCategory>
<PreferenceCategory