Tugas Minggu 11 PPB I
Nama : Tegar Ganang Satrio Priambodo
NRP : 5025201002
Kelas : PPB I
View Model and State in Compose
Pada pertemuan minggu 11 ini, diminta untuk mencoba membuat view model dan state pada compose dengan membuat game unscramble. aplikasi ini berupa permainan kata yang dimainkan satu player. Aplikasi ini menampilkan kata yang di acak untuk di ditebak oleh pemain. Pemain akan mendapatkan poin jika berhasil menebak kata.
Fokus pada pembuatan aplikasi ini adalah pembuatan view model. berikut untuk 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
/* | |
* Copyright (C) 2023 The Android Open Source Project | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* https://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example.unscramble.ui | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.setValue | |
import androidx.lifecycle.ViewModel | |
import com.example.unscramble.data.MAX_NO_OF_WORDS | |
import com.example.unscramble.data.SCORE_INCREASE | |
import com.example.unscramble.data.allWords | |
import kotlinx.coroutines.flow.MutableStateFlow | |
import kotlinx.coroutines.flow.StateFlow | |
import kotlinx.coroutines.flow.asStateFlow | |
import kotlinx.coroutines.flow.update | |
/** | |
* ViewModel containing the app data and methods to process the data | |
*/ | |
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) | |
} | |
} | |
} |
Komentar
Posting Komentar