Tugas 7 - PPB F
Nama: Nabila Zakiyah Khansa Machrus
NRP: 5025201139
Kelas: PPB F
NRP: 5025201139
Kelas: PPB F
ViewModel and State in Compose
Source code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.scramble_word.ui.theme | |
/** | |
* Data class that represents the game UI state | |
*/ | |
data class GameUiState( | |
val currentScrambledWord: String = "", | |
val currentWordCount: Int = 1, | |
val score: Int = 0, | |
val isGuessedWordWrong: Boolean = false, | |
val isGameOver: Boolean = false | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GameViewModel : ViewModel() { | |
// Game UI state | |
private val _uiState = MutableStateFlow(GameUiState()) | |
val uiState: StateFlow<GameUiState> = _uiState.asStateFlow() | |
var userGuess by mutableStateOf("") | |
private set | |
// Set of words used in the game | |
private var usedWords: MutableSet<String> = mutableSetOf() | |
private lateinit var currentWord: String | |
init { | |
resetGame() | |
} | |
/* | |
* Re-initializes the game data to restart the game. | |
*/ | |
fun resetGame() { | |
usedWords.clear() | |
_uiState.value = GameUiState(currentScrambledWord = pickRandomWordAndShuffle()) | |
} | |
/* | |
* Update the user's guess | |
*/ | |
fun updateUserGuess(guessedWord: String){ | |
userGuess = guessedWord | |
} | |
/* | |
* Checks if the user's guess is correct. | |
* Increases the score accordingly. | |
*/ | |
fun checkUserGuess() { | |
if (userGuess.equals(currentWord, ignoreCase = true)) { | |
// User's guess is correct, increase the score | |
// and call updateGameState() to prepare the game for next round | |
val updatedScore = _uiState.value.score.plus(SCORE_INCREASE) | |
updateGameState(updatedScore) | |
} else { | |
// User's guess is wrong, show an error | |
_uiState.update { currentState -> | |
currentState.copy(isGuessedWordWrong = true) | |
} | |
} | |
// Reset user guess | |
updateUserGuess("") | |
} | |
/* | |
* Skip to next word | |
*/ | |
fun skipWord() { | |
updateGameState(_uiState.value.score) | |
// Reset user guess | |
updateUserGuess("") | |
} | |
/* | |
* Picks a new currentWord and currentScrambledWord and updates UiState according to | |
* current game state. | |
*/ | |
private fun updateGameState(updatedScore: Int) { | |
if (usedWords.size == MAX_NO_OF_WORDS){ | |
//Last round in the game, update isGameOver to true, don't pick a new word | |
_uiState.update { currentState -> | |
currentState.copy( | |
isGuessedWordWrong = false, | |
score = updatedScore, | |
isGameOver = true | |
) | |
} | |
} else{ | |
// Normal round in the game | |
_uiState.update { currentState -> | |
currentState.copy( | |
isGuessedWordWrong = false, | |
currentScrambledWord = pickRandomWordAndShuffle(), | |
currentWordCount = currentState.currentWordCount.inc(), | |
score = updatedScore | |
) | |
} | |
} | |
} | |
private fun shuffleCurrentWord(word: String): String { | |
val tempWord = word.toCharArray() | |
// Scramble the word | |
tempWord.shuffle() | |
while (String(tempWord) == word) { | |
tempWord.shuffle() | |
} | |
return String(tempWord) | |
} | |
private fun pickRandomWordAndShuffle(): String { | |
// Continue picking up a new random word until you get one that hasn't been used before | |
currentWord = allWords.random() | |
return if (usedWords.contains(currentWord)) { | |
pickRandomWordAndShuffle() | |
} else { | |
usedWords.add(currentWord) | |
shuffleCurrentWord(currentWord) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun GameScreen(gameViewModel: GameViewModel = viewModel()) { | |
val gameUiState by gameViewModel.uiState.collectAsState() | |
val mediumPadding = dimensionResource(R.dimen.padding_medium) | |
Column( | |
modifier = Modifier | |
.statusBarsPadding() | |
.verticalScroll(rememberScrollState()) | |
.safeDrawingPadding() | |
.padding(mediumPadding), | |
verticalArrangement = Arrangement.Center, | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
Text( | |
text = stringResource(R.string.app_name), | |
style = typography.titleLarge, | |
) | |
GameLayout( | |
onUserGuessChanged = { gameViewModel.updateUserGuess(it) }, | |
wordCount = gameUiState.currentWordCount, | |
userGuess = gameViewModel.userGuess, | |
onKeyboardDone = { gameViewModel.checkUserGuess() }, | |
currentScrambledWord = gameUiState.currentScrambledWord, | |
isGuessWrong = gameUiState.isGuessedWordWrong, | |
modifier = Modifier | |
.fillMaxWidth() | |
.wrapContentHeight() | |
.padding(mediumPadding) | |
) | |
Column( | |
modifier = Modifier | |
.fillMaxWidth() | |
.padding(mediumPadding), | |
verticalArrangement = Arrangement.spacedBy(mediumPadding), | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
Button( | |
modifier = Modifier.fillMaxWidth(), | |
onClick = { gameViewModel.checkUserGuess() } | |
) { | |
Text( | |
text = stringResource(R.string.submit), | |
fontSize = 16.sp | |
) | |
} | |
OutlinedButton( | |
onClick = { gameViewModel.skipWord() }, | |
modifier = Modifier.fillMaxWidth() | |
) { | |
Text( | |
text = stringResource(R.string.skip), | |
fontSize = 16.sp | |
) | |
} | |
} | |
GameStatus(score = gameUiState.score, modifier = Modifier.padding(20.dp)) | |
if (gameUiState.isGameOver) { | |
FinalScoreDialog( | |
score = gameUiState.score, | |
onPlayAgain = { gameViewModel.resetGame() } | |
) | |
} | |
} | |
} | |
@Composable | |
fun GameStatus(score: Int, modifier: Modifier = Modifier) { | |
Card( | |
modifier = modifier | |
) { | |
Text( | |
text = stringResource(R.string.score, score), | |
style = typography.headlineMedium, | |
modifier = Modifier.padding(8.dp) | |
) | |
} | |
} | |
@Composable | |
fun GameLayout( | |
currentScrambledWord: String, | |
wordCount: Int, | |
isGuessWrong: Boolean, | |
userGuess: String, | |
onUserGuessChanged: (String) -> Unit, | |
onKeyboardDone: () -> Unit, | |
modifier: Modifier = Modifier | |
) { | |
val mediumPadding = dimensionResource(R.dimen.padding_medium) | |
Card( | |
modifier = modifier, | |
elevation = CardDefaults.cardElevation(defaultElevation = 5.dp) | |
) { | |
Column( | |
verticalArrangement = Arrangement.spacedBy(mediumPadding), | |
horizontalAlignment = Alignment.CenterHorizontally, | |
modifier = Modifier.padding(mediumPadding) | |
) { | |
Text( | |
modifier = Modifier | |
.clip(shapes.medium) | |
.background(colorScheme.surfaceTint) | |
.padding(horizontal = 10.dp, vertical = 4.dp) | |
.align(alignment = Alignment.End), | |
text = stringResource(R.string.word_count, wordCount), | |
style = typography.titleMedium, | |
color = colorScheme.onPrimary | |
) | |
Text( | |
text = currentScrambledWord, | |
style = typography.displayMedium | |
) | |
Text( | |
text = stringResource(R.string.instructions), | |
textAlign = TextAlign.Center, | |
style = typography.titleMedium | |
) | |
OutlinedTextField( | |
value = userGuess, | |
singleLine = true, | |
shape = shapes.large, | |
modifier = Modifier.fillMaxWidth(), | |
colors = TextFieldDefaults.colors( | |
focusedContainerColor = colorScheme.surface, | |
unfocusedContainerColor = colorScheme.surface, | |
disabledContainerColor = colorScheme.surface, | |
), | |
onValueChange = onUserGuessChanged, | |
label = { | |
if (isGuessWrong) { | |
Text(stringResource(R.string.wrong_guess)) | |
} else { | |
Text(stringResource(R.string.enter_your_word)) | |
} | |
}, | |
isError = isGuessWrong, | |
keyboardOptions = KeyboardOptions.Default.copy( | |
imeAction = ImeAction.Done | |
), | |
keyboardActions = KeyboardActions( | |
onDone = { onKeyboardDone() } | |
) | |
) | |
} | |
} | |
} | |
/* | |
* Creates and shows an AlertDialog with final score. | |
*/ | |
@Composable | |
private fun FinalScoreDialog( | |
score: Int, | |
onPlayAgain: () -> Unit, | |
modifier: Modifier = Modifier | |
) { | |
val activity = (LocalContext.current as Activity) | |
AlertDialog( | |
onDismissRequest = { | |
// Dismiss the dialog when the user clicks outside the dialog or on the back | |
// button. If you want to disable that functionality, simply use an empty | |
// onCloseRequest. | |
}, | |
title = { Text(text = stringResource(R.string.congratulations)) }, | |
text = { Text(text = stringResource(R.string.you_scored, score)) }, | |
modifier = modifier, | |
dismissButton = { | |
TextButton( | |
onClick = { | |
activity.finish() | |
} | |
) { | |
Text(text = stringResource(R.string.exit)) | |
} | |
}, | |
confirmButton = { | |
TextButton(onClick = onPlayAgain) { | |
Text(text = stringResource(R.string.play_again)) | |
} | |
} | |
) | |
} | |
@Preview(showBackground = true) | |
@Composable | |
fun GameScreenPreview() { | |
Scramble_WordTheme { | |
GameScreen() | |
} | |
} |
Komentar
Posting Komentar