Skip to content

Commit

Permalink
Merge pull request #31 from Efimj/enhancement/architecture
Browse files Browse the repository at this point in the history
ui + architecture
  • Loading branch information
Efimj authored Aug 17, 2024
2 parents 4730049 + 465e758 commit 7d863bb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ data class GameScreenStates(
val stepNumber: Int = 0,
val gameSettings: GameSettings = settings.gameSettings,
val currentStep: List<List<GameOfLifeUnitState>> = List(gameSettings.rows) { List(gameSettings.cols) { GameOfLifeUnitState.Empty } },
val previousStepsHash: List<Int> = emptyList(),
val previousSteps: HashSet<List<List<GameOfLifeUnitState>>> = HashSet(),
val gameResult: GameOfLifeResult? = null,
)

Expand Down Expand Up @@ -58,7 +58,7 @@ class GameScreenViewModel : ViewModel() {
revivals = 0,
isSimulationRunning = false,
stepNumber = 0,
previousStepsHash = emptyList(),
previousSteps = HashSet(),
gameResult = null,
)
}
Expand All @@ -79,8 +79,8 @@ class GameScreenViewModel : ViewModel() {
)

val gameResult = checkIsGameFinishedResult(
nextState = nextStep,
previousStepsHash = states.value.previousStepsHash,
nextStep = nextStep,
previousSteps = states.value.previousSteps,
previousStep = states.value.currentStep
)

Expand All @@ -93,7 +93,7 @@ class GameScreenViewModel : ViewModel() {
val aliveCount = countAlive(nextStep)
var revives = states.value.revivals
var deaths = states.value.deaths
if (states.value.previousStepsHash.isNotEmpty()) {
if (states.value.previousSteps.isNotEmpty()) {
revives += countRevives(nextStep, states.value.currentStep)
deaths += countDeaths(nextStep, states.value.currentStep)
}
Expand All @@ -105,33 +105,34 @@ class GameScreenViewModel : ViewModel() {
previousState = states.value.currentStep
)


_states.value = states.value.copy(
currentStep = nextStep,
alive = aliveCount,
deaths = deaths,
revivals = revives,
previousStepsHash = states.value.previousStepsHash + nextStep.hashCode(),
stepNumber = states.value.stepNumber + 1
)
states.value.previousSteps.add(nextStep)
}
}
}

private fun checkIsGameFinishedResult(
nextState: List<List<GameOfLifeUnitState>>,
previousStepsHash: List<Int>,
previousStep: List<List<GameOfLifeUnitState>>
nextStep: List<List<GameOfLifeUnitState>>,
previousStep: List<List<GameOfLifeUnitState>>,
previousSteps: HashSet<List<List<GameOfLifeUnitState>>>,
): GameOfLifeResult? {
if (previousStepsHash.isEmpty()) return null
if (previousSteps.isEmpty()) return null
var stableCombination = true
var noSurvived = true

for (row in nextState.indices) {
for (col in nextState[row].indices) {
if (nextState[row][col] != previousStep[row][col]) {
for (row in nextStep.indices) {
for (col in nextStep[row].indices) {
if (nextStep[row][col] != previousStep[row][col]) {
stableCombination = false
}
if (nextState[row][col] == GameOfLifeUnitState.Alive) {
if (nextStep[row][col] == GameOfLifeUnitState.Alive) {
noSurvived = false
}
}
Expand All @@ -142,8 +143,7 @@ class GameScreenViewModel : ViewModel() {

if (states.value.gameSettings.loopDetecting.not()) return null

val currentStateHash = nextState.hashCode()
if (previousStepsHash.contains(currentStateHash)) {
if (previousSteps.contains(nextStep)) {
return GameOfLifeResult.Loop
}

Expand Down Expand Up @@ -214,7 +214,7 @@ class GameScreenViewModel : ViewModel() {
stepNumber = 0,
alive = states.value.gameSettings.cols * states.value.gameSettings.rows,
deaths = 0,
previousStepsHash = emptyList()
previousSteps = HashSet()
)
}

Expand All @@ -230,7 +230,7 @@ class GameScreenViewModel : ViewModel() {
stepNumber = 0,
alive = 0,
deaths = states.value.gameSettings.cols * states.value.gameSettings.rows,
previousStepsHash = emptyList()
previousSteps = HashSet()
)
}

Expand All @@ -247,7 +247,7 @@ class GameScreenViewModel : ViewModel() {
deaths = 0,
gameResult = null,
stepNumber = 0,
previousStepsHash = emptyList(),
previousSteps = HashSet(),
)
}

Expand Down Expand Up @@ -351,7 +351,7 @@ class GameScreenViewModel : ViewModel() {
deaths = 0,
revivals = 0,
stepNumber = 0,
previousStepsHash = emptyList(),
previousSteps = HashSet(),
)
} else {
_states.value = states.value.copy(
Expand All @@ -365,7 +365,7 @@ class GameScreenViewModel : ViewModel() {
deaths = 0,
revivals = 0,
stepNumber = 0,
previousStepsHash = emptyList(),
previousSteps = HashSet(),
currentStep = rules.firstStep
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,9 @@ fun GameActions(viewModel: GameScreenViewModel) {
onCheckedChange = { viewModel.switchFreeSoulMode() },
)
}
Spacer(modifier = Modifier.height(5.dp))
}

SettingsGroup(headline = stringResource(id = R.string.rules)) {
val ruleSetDialogVisible = remember { mutableStateOf(false) }

LaunchedEffect(ruleSetDialogVisible.value) {
Expand Down Expand Up @@ -308,9 +309,8 @@ fun GameActions(viewModel: GameScreenViewModel) {
selectedRules = selectedRules.value,
onClick = { rules -> viewModel.setRules(rules) })
}
}
Spacer(modifier = Modifier.height(5.dp))

SettingsGroup(headline = stringResource(id = R.string.rules)) {
val scrollRevivingButtons = rememberScrollState()

SettingsItemWrapper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fun SettingsScreen(navController: NavHostController) {
.fillMaxSize()
.background(MaterialTheme.colorScheme.surface),
state = collapsingToolbarScaffold,
scrollStrategy = ScrollStrategy.ExitUntilCollapsed,
scrollStrategy = ScrollStrategy.EnterAlways,
enabledWhenBodyUnfilled = false,
snapConfig = SnapConfig(), // "collapseThreshold = 0.5" by default
toolbar = {
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
versionName = "1.1.0"
versionCode = "4"
versionName = "1.2.0"
versionCode = "5"

androidMinSdk = "27"
androidTargetSdk = "35"
Expand Down

0 comments on commit 7d863bb

Please sign in to comment.