Skip to content

Commit

Permalink
Fix missing deletion of possibles when revealing one cell
Browse files Browse the repository at this point in the history
  • Loading branch information
meikpiep committed Nov 17, 2023
1 parent 6b3c6bf commit 5440066
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- If there are two values entered in one row or column, only count the wrong value as a single mistake.
- When reavealing the value of a cell, the possible numbers of this cell are now cleared.
- Revealing of selected cell or selected cage was not working reliable.

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import android.view.MenuItem
import androidx.appcompat.widget.Toolbar
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.piepmeyer.gauguin.R
import org.piepmeyer.gauguin.game.Game
import org.piepmeyer.gauguin.options.ApplicationPreferencesImpl
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class BottomAppBarItemClickListener(
private val mainConstraintLayout: ConstraintLayout,
Expand All @@ -32,12 +32,13 @@ class BottomAppBarItemClickListener(
}

R.id.menu_reveal_cell -> {
game.revealSelectedCell()
mainActivity.cheatedOnGame()
if (game.revealSelectedCell()) {
mainActivity.cheatedOnGame()
}
}

R.id.menu_reveal_cage -> {
if (game.solveSelectedCage()) {
if (game.revealSelectedCage()) {
mainActivity.cheatedOnGame()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package org.piepmeyer.gauguin.ui.main
import android.view.MenuItem
import android.view.View
import androidx.core.view.iterator
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.piepmeyer.gauguin.R
import org.piepmeyer.gauguin.databinding.ActivityMainBinding
import org.piepmeyer.gauguin.game.Game
import org.piepmeyer.gauguin.undo.UndoListener
import org.piepmeyer.gauguin.undo.UndoManager
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class MainBottomAppBarService(
private val mainActivity: MainActivity,
Expand Down Expand Up @@ -71,7 +71,4 @@ class MainBottomAppBarService(
eraserButton?.visibility = View.VISIBLE
}
}



}
14 changes: 7 additions & 7 deletions gauguin-core/src/main/kotlin/org/piepmeyer/gauguin/game/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ data class Game(
private var removePencils: Boolean = false
private var solvedListener: GameSolvedListener? = null

private val gridSolveService = GridSolveService(grid)

private val gridCreationListeners = mutableListOf<GridCreationListener>()

fun addGridCreationListener(gridCreationListener: GridCreationListener) {
Expand Down Expand Up @@ -158,21 +156,23 @@ data class Game(
gridUI.invalidate()
}

fun solveSelectedCage(): Boolean {
fun revealSelectedCage(): Boolean {
grid.selectedCell ?: return false
gridSolveService.solveSelectedCage()
GridSolveService(grid).revealSelectedCage()
gridUI.invalidate()
return true
}

fun solveGrid() {
gridSolveService.solveGrid()
GridSolveService(grid).solveGrid()
gridUI.invalidate()
}

fun revealSelectedCell() {
gridSolveService.revealSelectedCell()
fun revealSelectedCell(): Boolean {
grid.selectedCell ?: return false
GridSolveService(grid).revealSelectedCell()
gridUI.invalidate()
return true
}

fun markInvalidChoices(showDupedDigits: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,32 @@ package org.piepmeyer.gauguin.grid
class GridSolveService(
private val grid: Grid
) {
fun solveSelectedCage() {
fun revealSelectedCage() {
grid.selectedCell?.let {
it.cage().cells.forEach { cell ->
if (!cell.isUserValueCorrect) {
cell.clearPossibles()
cell.setUserValueIntern(cell.value)
cell.isCheated = true
}
}
it.cage().cells.forEach { revealCell(it) }
it.isSelected = false
it.cage().setSelected(false)
}
}

fun solveGrid() {
for (cell in grid.cells) {
if (!cell.isUserValueCorrect) {
cell.clearPossibles()
cell.setUserValueIntern(cell.value)
cell.isCheated = true
}
}
grid.cells.forEach { revealCell(it) }

grid.selectedCell?.let {
it.isSelected = false
it.cage().setSelected(false)
}
}

fun revealSelectedCell() {
val selectedCell = grid.selectedCell ?: return
grid.selectedCell?.let { revealCell(it) }
}

selectedCell.setUserValueIntern(selectedCell.value)
selectedCell.clearPossibles()
selectedCell.isCheated = true
private fun revealCell(cell: GridCell) {
if (!cell.isUserValueCorrect) {
cell.clearPossibles()
cell.setUserValueIntern(cell.value)
cell.isCheated = true
}
}
}

0 comments on commit 5440066

Please sign in to comment.