Skip to content

Commit 7ee36b3

Browse files
authored
Merge pull request #47 from JayaSuryaT/#46-fix
Fixes #46 ; Introduce and hook in GameSuccessEvaluator
2 parents 2a3537c + 3c094ff commit 7ee36b3

File tree

5 files changed

+63
-44
lines changed

5 files changed

+63
-44
lines changed

minesweeper-engine/src/main/java/com/jayasuryat/minesweeperengine/controller/impl/GameController.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,18 @@ public class GameController(
6969

7070
public fun getDefault(): GameController {
7171
val gridRevealer = GridRevealer()
72+
val successEvaluator = GameSuccessEvaluator()
7273
return GameController(
7374
cellReveler = CellRevealer(
7475
gridRevealer = gridRevealer,
7576
radiallySorter = RadiallySorter(),
77+
successEvaluator = successEvaluator,
7678
),
7779
cellFlagger = CellFlagger(),
78-
valueCellReveler = ValueCellRevealer(gridRevealer = gridRevealer),
80+
valueCellReveler = ValueCellRevealer(
81+
gridRevealer = gridRevealer,
82+
successEvaluator = successEvaluator,
83+
),
7984
)
8085
}
8186
}

minesweeper-engine/src/main/java/com/jayasuryat/minesweeperengine/controller/impl/handler/CellFlagger.kt

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ package com.jayasuryat.minesweeperengine.controller.impl.handler
1818
import com.jayasuryat.minesweeperengine.controller.ActionHandler
1919
import com.jayasuryat.minesweeperengine.controller.model.MinefieldAction
2020
import com.jayasuryat.minesweeperengine.controller.model.MinefieldEvent
21-
import com.jayasuryat.minesweeperengine.model.cell.MineCell
2221
import com.jayasuryat.minesweeperengine.model.cell.RawCell
2322
import com.jayasuryat.minesweeperengine.model.grid.Grid
24-
import com.jayasuryat.minesweeperengine.util.mutate
2523
import com.jayasuryat.util.exhaustive
2624

2725
internal class CellFlagger : ActionHandler<MinefieldAction.OnCellLongPressed> {
@@ -38,37 +36,6 @@ internal class CellFlagger : ActionHandler<MinefieldAction.OnCellLongPressed> {
3836
is RawCell.UnrevealedCell.UnFlaggedCell -> action.cell.asFlagged()
3937
}.exhaustive
4038

41-
val isGameComplete = isGameComplete(
42-
grid = grid,
43-
updatedCell = updatedCell,
44-
)
45-
46-
return if (isGameComplete) MinefieldEvent.OnGameComplete(updatedCells = listOf(updatedCell))
47-
else MinefieldEvent.OnCellsUpdated(updatedCells = listOf(updatedCell))
48-
}
49-
50-
private fun isGameComplete(
51-
grid: Grid,
52-
updatedCell: RawCell,
53-
): Boolean {
54-
55-
val modGrid = grid.mutate { this[updatedCell.position] = updatedCell }
56-
57-
val totalMineCount = grid.totalMines
58-
59-
var totalFlaggedCount = 0
60-
var correctFlaggedCount = 0
61-
62-
modGrid.cells.flatten().forEach { cell ->
63-
64-
if (cell is RawCell.UnrevealedCell.FlaggedCell) {
65-
66-
totalFlaggedCount++
67-
if (totalFlaggedCount > totalMineCount) return false
68-
if (cell.asRevealed().cell is MineCell.Mine) correctFlaggedCount++
69-
}
70-
}
71-
72-
return correctFlaggedCount == totalMineCount
39+
return MinefieldEvent.OnCellsUpdated(updatedCells = listOf(updatedCell))
7340
}
7441
}

minesweeper-engine/src/main/java/com/jayasuryat/minesweeperengine/controller/impl/handler/CellRevealer.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.jayasuryat.util.exhaustive
2828
internal class CellRevealer(
2929
private val gridRevealer: GridRevealer,
3030
private val radiallySorter: RadiallySorter,
31+
private val successEvaluator: GameSuccessEvaluator,
3132
) : ActionHandler<MinefieldAction.OnCellClicked> {
3233

3334
override suspend fun onAction(
@@ -47,7 +48,7 @@ internal class CellRevealer(
4748
)
4849

4950
if (isGameComplete) MinefieldEvent.OnGameComplete(updatedCells = listOf(revealed))
50-
else MinefieldEvent.OnCellsUpdated(updatedCells = listOf(revealed),)
51+
else MinefieldEvent.OnCellsUpdated(updatedCells = listOf(revealed))
5152
}
5253

5354
is MineCell.ValuedCell.EmptyCell -> {
@@ -71,14 +72,7 @@ internal class CellRevealer(
7172
grid: Grid,
7273
updatedCell: RawCell,
7374
): Boolean {
74-
7575
val modGrid = grid.mutate { this[updatedCell.position] = updatedCell }
76-
77-
val totalCount = grid.gridSize.rows * grid.gridSize.columns
78-
val nonMineCellsCount = totalCount - grid.totalMines
79-
80-
val revealedCellsCount = modGrid.cells.flatten().count { it is RawCell.RevealedCell }
81-
82-
return revealedCellsCount == nonMineCellsCount
76+
return successEvaluator.isGameComplete(modGrid)
8377
}
8478
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2022 Jaya Surya Thotapalli
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.jayasuryat.minesweeperengine.controller.impl.handler
17+
18+
import android.util.Log
19+
import com.jayasuryat.minesweeperengine.model.cell.RawCell
20+
import com.jayasuryat.minesweeperengine.model.grid.Grid
21+
22+
internal class GameSuccessEvaluator {
23+
24+
fun isGameComplete(
25+
grid: Grid,
26+
): Boolean {
27+
28+
val totalCount = grid.gridSize.rows * grid.gridSize.columns
29+
val nonMineCellsCount = totalCount - grid.totalMines
30+
31+
val revealedCellsCount = grid.cells.flatten().count { it is RawCell.RevealedCell }
32+
Log.d("Im alive", "Im alive, (revealedCellsCount) $revealedCellsCount == $nonMineCellsCount (nonMineCellsCount)")
33+
return revealedCellsCount == nonMineCellsCount
34+
}
35+
}

minesweeper-engine/src/main/java/com/jayasuryat/minesweeperengine/controller/impl/handler/ValueCellRevealer.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ import com.jayasuryat.minesweeperengine.model.block.Position
2323
import com.jayasuryat.minesweeperengine.model.cell.MineCell
2424
import com.jayasuryat.minesweeperengine.model.cell.RawCell
2525
import com.jayasuryat.minesweeperengine.model.grid.Grid
26+
import com.jayasuryat.minesweeperengine.util.mutate
2627

2728
internal class ValueCellRevealer(
2829
private val gridRevealer: GridRevealer,
30+
private val successEvaluator: GameSuccessEvaluator,
2931
) : ActionHandler<MinefieldAction.OnValueCellClicked> {
3032

3133
override suspend fun onAction(
@@ -72,6 +74,12 @@ internal class ValueCellRevealer(
7274
}
7375
}.flatten().distinctBy { it.position }
7476

77+
val isGameComplete = isGameComplete(
78+
grid = grid,
79+
updatedCells = updatedCells
80+
)
81+
82+
if (isGameComplete) MinefieldEvent.OnGameComplete(updatedCells = updatedCells)
7583
return MinefieldEvent.OnCellsUpdated(updatedCells = updatedCells)
7684
}
7785

@@ -88,4 +96,14 @@ internal class ValueCellRevealer(
8896
}
8997
}.flatten().filterNotNull()
9098
}
99+
100+
private fun isGameComplete(
101+
grid: Grid,
102+
updatedCells: List<RawCell>,
103+
): Boolean {
104+
val modGrid = grid.mutate {
105+
updatedCells.forEach { cell -> this[cell.position] = cell }
106+
}
107+
return successEvaluator.isGameComplete(modGrid)
108+
}
91109
}

0 commit comments

Comments
 (0)