document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package com.example.scramble_word.ui.theme
  2.  
  3. import androidx.compose.runtime.getValue
  4. import androidx.compose.runtime.mutableStateOf
  5. import androidx.compose.runtime.setValue
  6. import androidx.lifecycle.ViewModel
  7. import com.example.scramble_word.data.MAX_NO_OF_WORDS
  8. import com.example.scramble_word.data.SCORE_INCREASE
  9. import com.example.scramble_word.data.allWords
  10. import kotlinx.coroutines.flow.MutableStateFlow
  11. import kotlinx.coroutines.flow.StateFlow
  12. import kotlinx.coroutines.flow.asStateFlow
  13. import kotlinx.coroutines.flow.update
  14.  
  15. class GameViewModel : ViewModel() {
  16.  
  17.     // Game UI state
  18.     private val _uiState = MutableStateFlow(GameUiState())
  19.     val uiState: StateFlow<GameUiState> = _uiState.asStateFlow()
  20.  
  21.     var userGuess by mutableStateOf("")
  22.         private set
  23.  
  24.     // Set of words used in the game
  25.     private var usedWords: MutableSet<String> = mutableSetOf()
  26.     private lateinit var currentWord: String
  27.  
  28.     init {
  29.         resetGame()
  30.     }
  31.  
  32.     /*
  33.      * Re-initializes the game data to restart the game.
  34.      */
  35.     fun resetGame() {
  36.         usedWords.clear()
  37.         _uiState.value = GameUiState(currentScrambledWord = pickRandomWordAndShuffle())
  38.     }
  39.  
  40.     /*
  41.      * Update the user\'s guess
  42.      */
  43.     fun updateUserGuess(guessedWord: String){
  44.         userGuess = guessedWord
  45.     }
  46.  
  47.     /*
  48.      * Checks if the user\'s guess is correct.
  49.      * Increases the score accordingly.
  50.      */
  51.     fun checkUserGuess() {
  52.         if (userGuess.equals(currentWord, ignoreCase = true)) {
  53.             // User\'s guess is correct, increase the score
  54.             // and call updateGameState() to prepare the game for next round
  55.             val updatedScore = _uiState.value.score.plus(SCORE_INCREASE)
  56.             updateGameState(updatedScore)
  57.         } else {
  58.             // User\'s guess is wrong, show an error
  59.             _uiState.update { currentState ->
  60.                 currentState.copy(isGuessedWordWrong = true)
  61.             }
  62.         }
  63.         // Reset user guess
  64.         updateUserGuess("")
  65.     }
  66.  
  67.     /*
  68.      * Skip to next word
  69.      */
  70.     fun skipWord() {
  71.         updateGameState(_uiState.value.score)
  72.         // Reset user guess
  73.         updateUserGuess("")
  74.     }
  75.  
  76.     /*
  77.      * Picks a new currentWord and currentScrambledWord and updates UiState according to
  78.      * current game state.
  79.      */
  80.     private fun updateGameState(updatedScore: Int) {
  81.         if (usedWords.size == MAX_NO_OF_WORDS){
  82.             //Last round in the game, update isGameOver to true, don\'t pick a new word
  83.             _uiState.update { currentState ->
  84.                 currentState.copy(
  85.                     isGuessedWordWrong = false,
  86.                     score = updatedScore,
  87.                     isGameOver = true
  88.                 )
  89.             }
  90.         } else{
  91.             // Normal round in the game
  92.             _uiState.update { currentState ->
  93.                 currentState.copy(
  94.                     isGuessedWordWrong = false,
  95.                     currentScrambledWord = pickRandomWordAndShuffle(),
  96.                     currentWordCount = currentState.currentWordCount.inc(),
  97.                     score = updatedScore
  98.                 )
  99.             }
  100.         }
  101.     }
  102.  
  103.     private fun shuffleCurrentWord(word: String): String {
  104.         val tempWord = word.toCharArray()
  105.         // Scramble the word
  106.         tempWord.shuffle()
  107.         while (String(tempWord) == word) {
  108.             tempWord.shuffle()
  109.         }
  110.         return String(tempWord)
  111.     }
  112.  
  113.     private fun pickRandomWordAndShuffle(): String {
  114.         // Continue picking up a new random word until you get one that hasn\'t been used before
  115.         currentWord = allWords.random()
  116.         return if (usedWords.contains(currentWord)) {
  117.             pickRandomWordAndShuffle()
  118.         } else {
  119.             usedWords.add(currentWord)
  120.             shuffleCurrentWord(currentWord)
  121.         }
  122.     }
  123. }
');