diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/src/main/java/com/a404m/mine_game/ui/page/Game.kt | 41 | 
1 files changed, 35 insertions, 6 deletions
diff --git a/app/src/main/java/com/a404m/mine_game/ui/page/Game.kt b/app/src/main/java/com/a404m/mine_game/ui/page/Game.kt index 3ea7ada..1bdb3a8 100644 --- a/app/src/main/java/com/a404m/mine_game/ui/page/Game.kt +++ b/app/src/main/java/com/a404m/mine_game/ui/page/Game.kt @@ -47,6 +47,7 @@ import com.a404m.mine_game.ui.utils.ActionChooser  import com.a404m.mine_game.ui.utils.CustomScaffold  import com.a404m.mine_game.ui.utils.modifier.freeScrollWithTransformGesture  import com.a404m.mine_game.ui.utils.modifier.model.rememberFreeScrollState +import com.a404m.mine_game.ui.utils.showToast  import com.a404m.mine_game.utils.PersianDate  import kotlinx.coroutines.delay @@ -83,6 +84,12 @@ fun GamePage(              gameState.isWon()          }      } +    val gameFinished = isWon || isLost +    val isHintEnabled by remember { +        derivedStateOf { +            !gameFinished && StorageGame.hintCount > 0 +        } +    }      var action by rememberSaveable { mutableStateOf(StorageGame.primaryAction) } @@ -92,7 +99,7 @@ fun GamePage(      LaunchedEffect(Unit) {          val startTime = PersianDate()          val startingMillis = gameState.millis -        while (!isWon && !isLost) { +        while (!gameFinished) {              delay(50)              gameState.millis = startingMillis + PersianDate().getTime() - startTime.getTime()          } @@ -144,7 +151,19 @@ fun GamePage(                  }                  IconButton(                      modifier = Modifier.padding(end = 10.dp), -                    onClick = onBack, +                    enabled = isHintEnabled, +                    onClick = { +                        val mineCells = arrayListOf<GameState.Cell>() +                        for (items in gameState.matrix) { +                            mineCells.addAll(items.filter { it.isBomb && !it.isFlag && !it.isOpened }) +                        } +                        if (mineCells.isEmpty()) { +                            showToast("No mine left") +                        } else { +                            mineCells.random().isFlag = true +                            StorageGame.hintCount -= 1 +                        } +                    },                  ) {                      Row(                          modifier = Modifier, @@ -152,7 +171,7 @@ fun GamePage(                      ) {                          Text(                              modifier = Modifier, -                            text = "6", +                            text = StorageGame.hintCount.toString(),                              style = MaterialTheme.typography.bodySmall,                          )                          Icon( @@ -181,9 +200,9 @@ fun GamePage(                                  zoomOverall = 0.1f                              }                              rotationOverall += rotation -                            if(rotationOverall >= 360f){ +                            if (rotationOverall >= 360f) {                                  rotationOverall -= 360 -                            }else if(rotationOverall < 0f){ +                            } else if (rotationOverall < 0f) {                                  rotationOverall += 360f                              }                              Log.d( @@ -205,7 +224,7 @@ fun GamePage(                                  cell = cell,                                  zoom = zoomOverall,                                  onSelect = { -                                    if (isLost || isWon) { +                                    if (gameFinished) {                                          return@Cell                                      }                                      when (action) { @@ -232,6 +251,9 @@ fun GamePage(                                              cell.isFlag = !cell.isFlag                                          }                                      } +                                    if(gameState.isWon()){ +                                        StorageGame.hintCount += 1 +                                    }                                  },                              )                          } @@ -347,6 +369,13 @@ fun GamePage(                          }",                          textAlign = TextAlign.Center,                      ) +                    Text( +                        modifier = Modifier +                            .fillMaxWidth() +                            .padding(bottom = 10.dp), +                        text = "You earned 1 hint!", +                        textAlign = TextAlign.Center, +                    )                      ElevatedButton(                          modifier = Modifier                              .fillMaxWidth()  |