diff --git a/app/src/main/java/es/genol/tictactoe/data/model/Ficha.kt b/app/src/main/java/es/genol/tictactoe/data/model/Ficha.kt new file mode 100644 index 0000000..7ba8455 --- /dev/null +++ b/app/src/main/java/es/genol/tictactoe/data/model/Ficha.kt @@ -0,0 +1,3 @@ +package es.genol.tictactoe.data.model + +data class Ficha(val row: Int, val col: Int, var player: Boolean?) \ No newline at end of file diff --git a/app/src/main/java/es/genol/tictactoe/ui/elements/AppContent.kt b/app/src/main/java/es/genol/tictactoe/ui/elements/AppContent.kt index 72f2846..c045266 100644 --- a/app/src/main/java/es/genol/tictactoe/ui/elements/AppContent.kt +++ b/app/src/main/java/es/genol/tictactoe/ui/elements/AppContent.kt @@ -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) diff --git a/app/src/main/java/es/genol/tictactoe/ui/elements/GameBoard.kt b/app/src/main/java/es/genol/tictactoe/ui/elements/GameBoard.kt index 5a7096e..996bc3f 100644 --- a/app/src/main/java/es/genol/tictactoe/ui/elements/GameBoard.kt +++ b/app/src/main/java/es/genol/tictactoe/ui/elements/GameBoard.kt @@ -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() } } diff --git a/app/src/main/java/es/genol/tictactoe/ui/state/TicTacToeViewModel.kt b/app/src/main/java/es/genol/tictactoe/ui/state/TicTacToeViewModel.kt index f1e6356..7bfe580 100644 --- a/app/src/main/java/es/genol/tictactoe/ui/state/TicTacToeViewModel.kt +++ b/app/src/main/java/es/genol/tictactoe/ui/state/TicTacToeViewModel.kt @@ -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() 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) - horizontalCheck(player = playerChange) - playerChange = !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