Cambios en el ViewModel

This commit is contained in:
Salatiel Genol 2023-05-18 18:18:14 +02:00
parent 6dbc0cc97c
commit 1bc97ebdff
4 changed files with 17 additions and 14 deletions

View File

@ -0,0 +1,3 @@
package es.genol.tictactoe.data.model
data class Ficha(val row: Int, val col: Int, var player: Boolean?)

View File

@ -58,7 +58,7 @@ fun AppContent() {
orientationHeight = orientationHeight,
orientationWidth = orientationWidth,
playerValue = { row, col ->
viewModel.getValue(row, col)
viewModel.getPlayer(row, col)
}
) { row, col ->
viewModel.printPosition(row, col)

View File

@ -43,10 +43,10 @@ fun GameBoard(
modifier = Modifier.size(75.dp),
shape = CircleShape
) {
playerValue(row, col).let {
if (it == true){
playerValue(row, col).let { player ->
if (player == true){
CircleIcon()
}else if(it == false){
}else if(player == false){
CrossIcon()
}
}

View File

@ -2,10 +2,9 @@ package es.genol.tictactoe.ui.state
import androidx.compose.runtime.mutableStateListOf
import androidx.lifecycle.ViewModel
import es.genol.tictactoe.data.model.Ficha
import kotlin.random.Random
data class Ficha(val row: Int, val col: Int, var status: Boolean?)
class TicTacToeViewModel : ViewModel() {
private var buttonStateList = mutableStateListOf<Ficha>()
private var playerChange = ramdomPlayer()
@ -15,15 +14,16 @@ class TicTacToeViewModel : ViewModel() {
}
fun printPosition(row: Int, col: Int) {
val tempIndex =
buttonStateList.indexOf(buttonStateList.find { (it.row == row && it.col == col) })
buttonStateList[tempIndex] = buttonStateList[tempIndex].copy(status = playerChange)
val index = buttonStateList.indexOf(buttonStateList.find { (it.row == row && it.col == col) })
if (buttonStateList[index].player == null) {
buttonStateList[index] = buttonStateList[index].copy(player = playerChange)
horizontalCheck(player = playerChange)
playerChange = !playerChange
}
}
fun getValue(row: Int, col: Int): Boolean? {
return buttonStateList.find { (it.row == row && it.col == col) }?.status
fun getPlayer(row: Int, col: Int): Boolean? {
return buttonStateList.find { (it.row == row && it.col == col) }?.player
}
fun boardReboot() {
@ -49,7 +49,7 @@ class TicTacToeViewModel : ViewModel() {
for (i in 0..6 step 3) {
result = 0
for (n in i..i + 2) {
if (buttonStateList[n].status == player){
if (buttonStateList[n].player == player){
result++
}
if (result == 3) return true