Skip to content

Commit

Permalink
Limit fixed size mines
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasnlm committed Dec 14, 2023
1 parent 0c2f1ae commit b36015d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.animation.AnimationUtils
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.core.widget.doAfterTextChanged
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.core.models.Difficulty
import dev.lucasnlm.antimine.custom.viewmodel.CreateGameViewModel
import dev.lucasnlm.antimine.custom.viewmodel.CustomEvent
Expand Down Expand Up @@ -127,56 +125,6 @@ class CustomLevelDialogFragment : AppCompatDialogFragment() {
}.create()
}

private fun checkLimitFeedbacks(): Boolean {
val wantedWidth = binding.mapWidth.text.toString().toIntOrNull()
var allValid = true
if (wantedWidth == null) {
binding.mapWidth.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fast_shake))
allValid = false
} else if (wantedWidth >= MAX_WIDTH) {
binding.mapWidth.setText(MAX_WIDTH.toString())
binding.mapWidth.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fast_shake))
allValid = false
} else if (wantedWidth <= MIN_WIDTH) {
binding.mapWidth.setText(MIN_WIDTH.toString())
binding.mapWidth.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fast_shake))
allValid = false
}

val wantedHeight = binding.mapHeight.text.toString().toIntOrNull()
if (wantedHeight == null) {
binding.mapHeight.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fast_shake))
allValid = false
} else if (wantedHeight >= MAX_HEIGHT) {
binding.mapHeight.setText(MAX_HEIGHT.toString())
binding.mapHeight.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fast_shake))
allValid = false
} else if (wantedHeight <= MIN_HEIGHT) {
binding.mapHeight.setText(MIN_HEIGHT.toString())
binding.mapHeight.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fast_shake))
allValid = false
}

if (allValid && wantedWidth != null && wantedHeight != null) {
val wantedMines = binding.mapMines.text.toString().toIntOrNull()
val maxMines = wantedWidth * wantedHeight - MIN_SAFE_AREA
if (wantedMines == null) {
binding.mapMines.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fast_shake))
allValid = false
} else if (wantedMines >= maxMines) {
binding.mapMines.setText((wantedWidth * wantedHeight - MIN_SAFE_AREA).toString())
binding.mapMines.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fast_shake))
allValid = false
} else if (wantedMines <= MIN_MINES) {
binding.mapMines.setText(MIN_MINES.toString())
binding.mapMines.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fast_shake))
allValid = false
}
}

return allValid
}

override fun onDismiss(dialog: DialogInterface) {
if (activity is DialogInterface.OnDismissListener) {
(activity as DialogInterface.OnDismissListener).onDismiss(dialog)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ class StatsViewModel(
private val expertSize = sizeOf(Difficulty.Expert)
private val intermediateSize = sizeOf(Difficulty.Intermediate)
private val beginnerSize = sizeOf(Difficulty.Beginner)
private val standardSize = minefieldRepository.baseStandardSize(dimensionRepository, 0)
private val standardSize =
minefieldRepository.baseStandardSize(
dimensionRepository = dimensionRepository,
progressiveMines = 0,
limitToMax = false,
)

private fun sizeOf(difficulty: Difficulty): Minefield {
return minefieldRepository.fromDifficulty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class FixedMinefieldRepository : MinefieldRepository {
override fun baseStandardSize(
dimensionRepository: DimensionRepository,
progressiveMines: Int,
limitToMax: Boolean,
): Minefield = Minefield(9, 9, 9 + progressiveMines)

override fun fromDifficulty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class StatsViewModelTest : IntentViewModelTest() {
override fun setup() {
super.setup()
every { prefsRepository.isPremiumEnabled() } returns false
every { minefieldRepository.baseStandardSize(any(), any()) } returns Minefield(6, 12, 9)
every { minefieldRepository.baseStandardSize(any(), any(), any()) } returns Minefield(6, 12, 9)
every { minefieldRepository.fromDifficulty(any(), any(), any()) } returns Minefield(6, 12, 9)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface MinefieldRepository {
fun baseStandardSize(
dimensionRepository: DimensionRepository,
progressiveMines: Int,
limitToMax: Boolean,
): Minefield

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import dev.lucasnlm.antimine.core.models.Difficulty
import dev.lucasnlm.antimine.core.repository.DimensionRepository
import dev.lucasnlm.antimine.preferences.PreferencesRepository
import dev.lucasnlm.antimine.preferences.models.Minefield
import java.lang.Integer.min

class MinefieldRepositoryImpl : MinefieldRepository {
override fun baseStandardSize(
dimensionRepository: DimensionRepository,
progressiveMines: Int,
limitToMax: Boolean,
): Minefield {
val fieldSize = dimensionRepository.areaSize()
val horizontalGap =
Expand All @@ -33,7 +35,13 @@ class MinefieldRepositoryImpl : MinefieldRepository {
val fitWidth = calculatedWidth.coerceAtLeast(MIN_STANDARD_WIDTH)
val fitHeight = calculatedHeight.coerceAtLeast(MIN_STANDARD_HEIGHT)
val fieldArea = fitWidth * fitHeight
val fitMines = ((fieldArea * CUSTOM_LEVEL_MINE_RATIO).toInt() + progressiveMines)
val maxMines =
if (limitToMax) {
(fieldArea * 0.75).toInt()
} else {
Int.MAX_VALUE
}
val fitMines = min(maxMines, ((fieldArea * CUSTOM_LEVEL_MINE_RATIO).toInt() + progressiveMines))
return Minefield(fitWidth, fitHeight, fitMines)
}

Expand All @@ -49,7 +57,12 @@ class MinefieldRepositoryImpl : MinefieldRepository {
Difficulty.Expert -> expertMinefield
Difficulty.Master -> masterMinefield
Difficulty.Legend -> legendMinefield
Difficulty.FixedSize -> baseStandardSize(dimensionRepository, preferencesRepository.getProgressiveValue())
Difficulty.FixedSize ->
baseStandardSize(
dimensionRepository = dimensionRepository,
progressiveMines = preferencesRepository.getProgressiveValue(),
limitToMax = true,
)
Difficulty.Custom -> preferencesRepository.customGameMode()
}

Expand All @@ -59,8 +72,9 @@ class MinefieldRepositoryImpl : MinefieldRepository {
): Minefield {
var result: Minefield =
baseStandardSize(
dimensionRepository,
preferencesRepository.getProgressiveValue(),
dimensionRepository = dimensionRepository,
progressiveMines = preferencesRepository.getProgressiveValue(),
limitToMax = false,
)
var resultWidth = result.width
var resultHeight = result.height
Expand Down

0 comments on commit b36015d

Please sign in to comment.