Implementando avisos de ganador, y empate

This commit is contained in:
Salatiel Genol 2023-05-18 23:42:05 +02:00
parent 1bc97ebdff
commit 19df47972a
3 changed files with 41 additions and 6 deletions

View File

@ -8,10 +8,15 @@ import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.platform.LocalConfiguration
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
@ -25,6 +30,7 @@ fun AppContent() {
val configuration = LocalConfiguration.current val configuration = LocalConfiguration.current
val orientationHeight: Float val orientationHeight: Float
val orientationWidth: Float val orientationWidth: Float
val snackBarState = remember { SnackbarHostState() }
when (configuration.orientation) { when (configuration.orientation) {
Configuration.ORIENTATION_PORTRAIT -> { Configuration.ORIENTATION_PORTRAIT -> {
@ -38,6 +44,16 @@ fun AppContent() {
} }
} }
if (viewModel.isWinner) {
LaunchedEffect(key1 = viewModel.isWinner) {
viewModel.resetFromSnackBar(snackBarState.showSnackbar(
message = "Ganador",
actionLabel = "Reiniciar",
duration = SnackbarDuration.Indefinite
))
}
}
TicTacToeTheme { TicTacToeTheme {
Surface( Surface(
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
@ -51,7 +67,7 @@ fun AppContent() {
Text(text = "REINICIAR") Text(text = "REINICIAR")
} }
}) })
}) { }, snackbarHost = { SnackbarHost(hostState = snackBarState) }) {
Column(Modifier.padding(it)) { Column(Modifier.padding(it)) {
GameBoard( GameBoard(
boardSize = 3, boardSize = 3,
@ -59,7 +75,8 @@ fun AppContent() {
orientationWidth = orientationWidth, orientationWidth = orientationWidth,
playerValue = { row, col -> playerValue = { row, col ->
viewModel.getPlayer(row, col) viewModel.getPlayer(row, col)
} },
buttonEnabled = !viewModel.isWinner
) { row, col -> ) { row, col ->
viewModel.printPosition(row, col) viewModel.printPosition(row, col)
} }

View File

@ -20,6 +20,7 @@ fun GameBoard(
orientationHeight: Float, orientationHeight: Float,
orientationWidth: Float, orientationWidth: Float,
playerValue: (Int, Int) -> Boolean?, playerValue: (Int, Int) -> Boolean?,
buttonEnabled: Boolean,
onButtonClick: (Int, Int) -> Unit onButtonClick: (Int, Int) -> Unit
) { ) {
Column( Column(
@ -41,12 +42,13 @@ fun GameBoard(
Button( Button(
onClick = { onButtonClick(row, col) }, onClick = { onButtonClick(row, col) },
modifier = Modifier.size(75.dp), modifier = Modifier.size(75.dp),
enabled = buttonEnabled,
shape = CircleShape shape = CircleShape
) { ) {
playerValue(row, col).let { player -> playerValue(row, col)?.let { player ->
if (player == true){ if (player){
CircleIcon() CircleIcon()
}else if(player == false){ }else if(!player){
CrossIcon() CrossIcon()
} }
} }

View File

@ -1,6 +1,10 @@
package es.genol.tictactoe.ui.state package es.genol.tictactoe.ui.state
import androidx.compose.material3.SnackbarResult
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import es.genol.tictactoe.data.model.Ficha import es.genol.tictactoe.data.model.Ficha
import kotlin.random.Random import kotlin.random.Random
@ -9,6 +13,10 @@ class TicTacToeViewModel : ViewModel() {
private var buttonStateList = mutableStateListOf<Ficha>() private var buttonStateList = mutableStateListOf<Ficha>()
private var playerChange = ramdomPlayer() private var playerChange = ramdomPlayer()
var isWinner by mutableStateOf(false)
private set
init { init {
fillBoardGame() fillBoardGame()
} }
@ -17,7 +25,7 @@ class TicTacToeViewModel : ViewModel() {
val index = buttonStateList.indexOf(buttonStateList.find { (it.row == row && it.col == col) }) val index = buttonStateList.indexOf(buttonStateList.find { (it.row == row && it.col == col) })
if (buttonStateList[index].player == null) { if (buttonStateList[index].player == null) {
buttonStateList[index] = buttonStateList[index].copy(player = playerChange) buttonStateList[index] = buttonStateList[index].copy(player = playerChange)
horizontalCheck(player = playerChange) isWinner = horizontalCheck(player = playerChange)
playerChange = !playerChange playerChange = !playerChange
} }
} }
@ -29,6 +37,7 @@ class TicTacToeViewModel : ViewModel() {
fun boardReboot() { fun boardReboot() {
buttonStateList.clear() buttonStateList.clear()
playerChange = ramdomPlayer() playerChange = ramdomPlayer()
isWinner = false
fillBoardGame() fillBoardGame()
} }
@ -57,5 +66,12 @@ class TicTacToeViewModel : ViewModel() {
} }
return false return false
} }
fun resetFromSnackBar(result: SnackbarResult){
when (result) {
SnackbarResult.Dismissed -> {}
SnackbarResult.ActionPerformed -> { boardReboot() }
}
}
} }