Samuel_Berkat_Hulu

GameScree

Jun 25th, 2024
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.48 KB | None | 0 0
  1. package com.example.scramble_word.ui.theme
  2.  
  3. import android.app.Activity
  4. import androidx.compose.foundation.background
  5. import androidx.compose.foundation.layout.Arrangement
  6. import androidx.compose.foundation.layout.Column
  7. import androidx.compose.foundation.layout.fillMaxWidth
  8. import androidx.compose.foundation.layout.padding
  9. import androidx.compose.foundation.layout.safeDrawingPadding
  10. import androidx.compose.foundation.layout.statusBarsPadding
  11. import androidx.compose.foundation.layout.wrapContentHeight
  12. import androidx.compose.foundation.rememberScrollState
  13. import androidx.compose.foundation.text.KeyboardActions
  14. import androidx.compose.foundation.text.KeyboardOptions
  15. import androidx.compose.foundation.verticalScroll
  16. import androidx.compose.material3.AlertDialog
  17. import androidx.compose.material3.Button
  18. import androidx.compose.material3.Card
  19. import androidx.compose.material3.CardDefaults
  20. import androidx.compose.material3.MaterialTheme.colorScheme
  21. import androidx.compose.material3.MaterialTheme.shapes
  22. import androidx.compose.material3.MaterialTheme.typography
  23. import androidx.compose.material3.OutlinedButton
  24. import androidx.compose.material3.OutlinedTextField
  25. import androidx.compose.material3.Text
  26. import androidx.compose.material3.TextButton
  27. import androidx.compose.material3.TextFieldDefaults
  28. import androidx.compose.runtime.Composable
  29. import androidx.compose.runtime.collectAsState
  30. import androidx.compose.runtime.getValue
  31. import androidx.compose.ui.Alignment
  32. import androidx.compose.ui.Modifier
  33. import androidx.compose.ui.draw.clip
  34. import androidx.compose.ui.platform.LocalContext
  35. import androidx.compose.ui.res.dimensionResource
  36. import androidx.compose.ui.res.stringResource
  37. import androidx.compose.ui.text.input.ImeAction
  38. import androidx.compose.ui.text.style.TextAlign
  39. import androidx.compose.ui.tooling.preview.Preview
  40. import androidx.compose.ui.unit.dp
  41. import androidx.compose.ui.unit.sp
  42. import androidx.lifecycle.viewmodel.compose.viewModel
  43. import com.example.scramble_word.R
  44. import com.example.scramble_word.ui.theme.Scramble_WordTheme
  45.  
  46. @Composable
  47. fun GameScreen(gameViewModel: GameViewModel = viewModel()) {
  48.     val gameUiState by gameViewModel.uiState.collectAsState()
  49.     val mediumPadding = dimensionResource(R.dimen.padding_medium)
  50.  
  51.     Column(
  52.         modifier = Modifier
  53.             .statusBarsPadding()
  54.             .verticalScroll(rememberScrollState())
  55.             .safeDrawingPadding()
  56.             .padding(mediumPadding),
  57.         verticalArrangement = Arrangement.Center,
  58.         horizontalAlignment = Alignment.CenterHorizontally
  59.     ) {
  60.  
  61.         Text(
  62.             text = stringResource(R.string.app_name),
  63.             style = typography.titleLarge,
  64.         )
  65.         GameLayout(
  66.             onUserGuessChanged = { gameViewModel.updateUserGuess(it) },
  67.             wordCount = gameUiState.currentWordCount,
  68.             userGuess = gameViewModel.userGuess,
  69.             onKeyboardDone = { gameViewModel.checkUserGuess() },
  70.             currentScrambledWord = gameUiState.currentScrambledWord,
  71.             isGuessWrong = gameUiState.isGuessedWordWrong,
  72.             modifier = Modifier
  73.                 .fillMaxWidth()
  74.                 .wrapContentHeight()
  75.                 .padding(mediumPadding)
  76.         )
  77.         Column(
  78.             modifier = Modifier
  79.                 .fillMaxWidth()
  80.                 .padding(mediumPadding),
  81.             verticalArrangement = Arrangement.spacedBy(mediumPadding),
  82.             horizontalAlignment = Alignment.CenterHorizontally
  83.         ) {
  84.  
  85.             Button(
  86.                 modifier = Modifier.fillMaxWidth(),
  87.                 onClick = { gameViewModel.checkUserGuess() }
  88.             ) {
  89.                 Text(
  90.                     text = stringResource(R.string.submit),
  91.                     fontSize = 16.sp
  92.                 )
  93.             }
  94.  
  95.             OutlinedButton(
  96.                 onClick = { gameViewModel.skipWord() },
  97.                 modifier = Modifier.fillMaxWidth()
  98.             ) {
  99.                 Text(
  100.                     text = stringResource(R.string.skip),
  101.                     fontSize = 16.sp
  102.                 )
  103.             }
  104.         }
  105.  
  106.         GameStatus(score = gameUiState.score, modifier = Modifier.padding(20.dp))
  107.  
  108.         if (gameUiState.isGameOver) {
  109.             FinalScoreDialog(
  110.                 score = gameUiState.score,
  111.                 onPlayAgain = { gameViewModel.resetGame() }
  112.             )
  113.         }
  114.     }
  115. }
  116.  
  117. @Composable
  118. fun GameStatus(score: Int, modifier: Modifier = Modifier) {
  119.     Card(
  120.         modifier = modifier
  121.     ) {
  122.         Text(
  123.             text = stringResource(R.string.score, score),
  124.             style = typography.headlineMedium,
  125.             modifier = Modifier.padding(8.dp)
  126.         )
  127.  
  128.     }
  129. }
  130.  
  131. @Composable
  132. fun GameLayout(
  133.     currentScrambledWord: String,
  134.     wordCount: Int,
  135.     isGuessWrong: Boolean,
  136.     userGuess: String,
  137.     onUserGuessChanged: (String) -> Unit,
  138.     onKeyboardDone: () -> Unit,
  139.     modifier: Modifier = Modifier
  140. ) {
  141.     val mediumPadding = dimensionResource(R.dimen.padding_medium)
  142.  
  143.     Card(
  144.         modifier = modifier,
  145.         elevation = CardDefaults.cardElevation(defaultElevation = 5.dp)
  146.     ) {
  147.         Column(
  148.             verticalArrangement = Arrangement.spacedBy(mediumPadding),
  149.             horizontalAlignment = Alignment.CenterHorizontally,
  150.             modifier = Modifier.padding(mediumPadding)
  151.         ) {
  152.             Text(
  153.                 modifier = Modifier
  154.                     .clip(shapes.medium)
  155.                     .background(colorScheme.surfaceTint)
  156.                     .padding(horizontal = 10.dp, vertical = 4.dp)
  157.                     .align(alignment = Alignment.End),
  158.                 text = stringResource(R.string.word_count, wordCount),
  159.                 style = typography.titleMedium,
  160.                 color = colorScheme.onPrimary
  161.             )
  162.             Text(
  163.                 text = currentScrambledWord,
  164.                 style = typography.displayMedium
  165.             )
  166.             Text(
  167.                 text = stringResource(R.string.instructions),
  168.                 textAlign = TextAlign.Center,
  169.                 style = typography.titleMedium
  170.             )
  171.             OutlinedTextField(
  172.                 value = userGuess,
  173.                 singleLine = true,
  174.                 shape = shapes.large,
  175.                 modifier = Modifier.fillMaxWidth(),
  176.                 colors = TextFieldDefaults.colors(
  177.                     focusedContainerColor = colorScheme.surface,
  178.                     unfocusedContainerColor = colorScheme.surface,
  179.                     disabledContainerColor = colorScheme.surface,
  180.                 ),
  181.                 onValueChange = onUserGuessChanged,
  182.                 label = {
  183.                     if (isGuessWrong) {
  184.                         Text(stringResource(R.string.wrong_guess))
  185.                     } else {
  186.                         Text(stringResource(R.string.enter_your_word))
  187.                     }
  188.                 },
  189.                 isError = isGuessWrong,
  190.                 keyboardOptions = KeyboardOptions.Default.copy(
  191.                     imeAction = ImeAction.Done
  192.                 ),
  193.                 keyboardActions = KeyboardActions(
  194.                     onDone = { onKeyboardDone() }
  195.                 )
  196.             )
  197.         }
  198.     }
  199. }
  200.  
  201. /*
  202.  * Creates and shows an AlertDialog with final score.
  203.  */
  204. @Composable
  205. private fun FinalScoreDialog(
  206.     score: Int,
  207.     onPlayAgain: () -> Unit,
  208.     modifier: Modifier = Modifier
  209. ) {
  210.     val activity = (LocalContext.current as Activity)
  211.  
  212.     AlertDialog(
  213.         onDismissRequest = {
  214.             // Dismiss the dialog when the user clicks outside the dialog or on the back
  215.             // button. If you want to disable that functionality, simply use an empty
  216.             // onCloseRequest.
  217.         },
  218.         title = { Text(text = stringResource(R.string.congratulations)) },
  219.         text = { Text(text = stringResource(R.string.you_scored, score)) },
  220.         modifier = modifier,
  221.         dismissButton = {
  222.             TextButton(
  223.                 onClick = {
  224.                     activity.finish()
  225.                 }
  226.             ) {
  227.                 Text(text = stringResource(R.string.exit))
  228.             }
  229.         },
  230.         confirmButton = {
  231.             TextButton(onClick = onPlayAgain) {
  232.                 Text(text = stringResource(R.string.play_again))
  233.             }
  234.         }
  235.     )
  236. }
  237.  
  238. @Preview(showBackground = true)
  239. @Composable
  240. fun GameScreenPreview() {
  241.     Scramble_WordTheme {
  242.         GameScreen()
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment