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, orientationHeight = orientationHeight,
orientationWidth = orientationWidth, orientationWidth = orientationWidth,
playerValue = { row, col -> playerValue = { row, col ->
viewModel.getValue(row, col) viewModel.getPlayer(row, col)
} }
) { row, col -> ) { row, col ->
viewModel.printPosition(row, col) viewModel.printPosition(row, col)

View File

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

View File

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