Some vars renamed
This commit is contained in:
parent
9521cae0f1
commit
a100af63e1
|
|
@ -18,7 +18,7 @@ class GameChecks(private val player: Boolean, private val gridData: List<Ficha>)
|
||||||
}
|
}
|
||||||
result = 0
|
result = 0
|
||||||
for (gridId in start..(stage * 2 + start) step stage) {
|
for (gridId in start..(stage * 2 + start) step stage) {
|
||||||
if (gridData[gridId].player == player) {
|
if (gridData[gridId].playerMark == player) {
|
||||||
result++
|
result++
|
||||||
}
|
}
|
||||||
if (result == 3) return true
|
if (result == 3) return true
|
||||||
|
|
@ -32,7 +32,7 @@ class GameChecks(private val player: Boolean, private val gridData: List<Ficha>)
|
||||||
for (start in 0..2) {
|
for (start in 0..2) {
|
||||||
result = 0
|
result = 0
|
||||||
for (gridId in start..start + 6 step 3) {
|
for (gridId in start..start + 6 step 3) {
|
||||||
if (gridData[gridId].player == player) {
|
if (gridData[gridId].playerMark == player) {
|
||||||
result++
|
result++
|
||||||
}
|
}
|
||||||
if (result == 3) return true
|
if (result == 3) return true
|
||||||
|
|
@ -46,7 +46,7 @@ class GameChecks(private val player: Boolean, private val gridData: List<Ficha>)
|
||||||
for (start in 0..6 step 3) {
|
for (start in 0..6 step 3) {
|
||||||
result = 0
|
result = 0
|
||||||
for (gridId in start..start + 2) {
|
for (gridId in start..start + 2) {
|
||||||
if (gridData[gridId].player == player) {
|
if (gridData[gridId].playerMark == player) {
|
||||||
result++
|
result++
|
||||||
}
|
}
|
||||||
if (result == 3) return true
|
if (result == 3) return true
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
package es.genol.tictactoe.data.model
|
package es.genol.tictactoe.data.model
|
||||||
|
|
||||||
data class Ficha(var player: Boolean? = null)
|
data class Ficha(var playerMark: Boolean? = null)
|
||||||
|
|
@ -2,7 +2,6 @@ package es.genol.tictactoe.ui.elements
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.material3.Button
|
import androidx.compose.material3.Button
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
|
|
@ -16,21 +15,13 @@ import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
import es.genol.tictactoe.ui.state.GameState
|
import es.genol.tictactoe.ui.state.GameStateVM
|
||||||
import es.genol.tictactoe.ui.theme.TicTacToeTheme
|
import es.genol.tictactoe.ui.theme.TicTacToeTheme
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun AppContent() {
|
fun AppContent() {
|
||||||
val viewModel: GameState = viewModel()
|
val viewModel: GameStateVM = viewModel()
|
||||||
|
|
||||||
if (viewModel.isWinner) {
|
|
||||||
WinnerDialog(winner = viewModel.currentPlayer, behavior = { viewModel.cleanGrid() })
|
|
||||||
}
|
|
||||||
|
|
||||||
if (viewModel.moveNumber == 9) {
|
|
||||||
DrawDialog(behavior = { viewModel.cleanGrid() })
|
|
||||||
}
|
|
||||||
|
|
||||||
TicTacToeTheme {
|
TicTacToeTheme {
|
||||||
Surface(
|
Surface(
|
||||||
|
|
@ -42,7 +33,7 @@ fun AppContent() {
|
||||||
TopAppBar(
|
TopAppBar(
|
||||||
title = { Text(text = "TicTacToe") },
|
title = { Text(text = "TicTacToe") },
|
||||||
actions = {
|
actions = {
|
||||||
if (viewModel.moveNumber != null) {
|
if (viewModel.gridMarkCount != null) {
|
||||||
Button(onClick = { viewModel.cleanGrid() }) {
|
Button(onClick = { viewModel.cleanGrid() }) {
|
||||||
Text(text = "REINICIAR")
|
Text(text = "REINICIAR")
|
||||||
}
|
}
|
||||||
|
|
@ -54,15 +45,15 @@ fun AppContent() {
|
||||||
Modifier
|
Modifier
|
||||||
.padding(it)
|
.padding(it)
|
||||||
.padding(vertical = 15.dp)
|
.padding(vertical = 15.dp)
|
||||||
.fillMaxWidth(),
|
.fillMaxSize(),
|
||||||
horizontalAlignment = Alignment.CenterHorizontally
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
) {
|
) {
|
||||||
GameBoard(
|
GameBoard(
|
||||||
boardSize = 3,
|
boardSize = 3,
|
||||||
playerValue = { gridId ->
|
playerValue = { gridId ->
|
||||||
viewModel.gridState[gridId].player
|
viewModel.gridState[gridId].playerMark
|
||||||
},
|
},
|
||||||
buttonEnabled = !viewModel.isWinner && viewModel.moveNumber != 9
|
buttonEnabled = !viewModel.isWinner && viewModel.gridMarkCount != 9
|
||||||
) { gridId ->
|
) { gridId ->
|
||||||
viewModel.playerMarkGrid(gridId)
|
viewModel.playerMarkGrid(gridId)
|
||||||
}
|
}
|
||||||
|
|
@ -70,5 +61,14 @@ fun AppContent() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (viewModel.isWinner) {
|
||||||
|
WinnerDialog(winner = viewModel.currentPlayer, behavior = { viewModel.cleanGrid() })
|
||||||
|
}
|
||||||
|
|
||||||
|
if (viewModel.gridMarkCount == 9) {
|
||||||
|
DrawDialog(behavior = { viewModel.cleanGrid() })
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.aspectRatio
|
import androidx.compose.foundation.layout.aspectRatio
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.foundation.layout.width
|
import androidx.compose.foundation.layout.width
|
||||||
|
|
@ -48,9 +49,9 @@ fun GameBoard(
|
||||||
) {
|
) {
|
||||||
playerValue(grid[row][col])?.let { player ->
|
playerValue(grid[row][col])?.let { player ->
|
||||||
if (player) {
|
if (player) {
|
||||||
CircleIcon()
|
CircleIcon(modifier = Modifier.fillMaxSize())
|
||||||
} else {
|
} else {
|
||||||
CrossIcon()
|
CrossIcon(modifier = Modifier.fillMaxSize())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,9 @@ fun WinnerDialog(
|
||||||
title = { Text(text = "Ganador!!!")},
|
title = { Text(text = "Ganador!!!")},
|
||||||
text = {
|
text = {
|
||||||
if (winner) {
|
if (winner) {
|
||||||
CircleIcon()
|
CircleIcon(modifier = Modifier.fillMaxSize())
|
||||||
} else {
|
} else {
|
||||||
CrossIcon()
|
CrossIcon(modifier = Modifier.fillMaxSize())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
@ -44,7 +44,7 @@ fun DrawDialog(
|
||||||
Modifier.fillMaxSize(.95f),
|
Modifier.fillMaxSize(.95f),
|
||||||
title = { Text(text = "Empate")},
|
title = { Text(text = "Empate")},
|
||||||
text = {
|
text = {
|
||||||
DrawIcon()
|
DrawIcon(modifier = Modifier.fillMaxSize())
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import es.genol.tictactoe.GameChecks
|
||||||
import es.genol.tictactoe.data.model.Ficha
|
import es.genol.tictactoe.data.model.Ficha
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
|
|
||||||
class GameState : ViewModel() {
|
class GameStateVM : ViewModel() {
|
||||||
private var _gridState = MutableList(9) { Ficha() }.toMutableStateList()
|
private var _gridState = MutableList(9) { Ficha() }.toMutableStateList()
|
||||||
val gridState get() = _gridState.toList()
|
val gridState get() = _gridState.toList()
|
||||||
|
|
||||||
|
|
@ -19,15 +19,15 @@ class GameState : ViewModel() {
|
||||||
private var _isWinner by mutableStateOf(false)
|
private var _isWinner by mutableStateOf(false)
|
||||||
val isWinner get() = _isWinner
|
val isWinner get() = _isWinner
|
||||||
|
|
||||||
private var _moveNumber by mutableStateOf<Int?>(null)
|
private var _gridMarkCount by mutableStateOf<Int?>(null)
|
||||||
val moveNumber get() = _moveNumber
|
val gridMarkCount get() = _gridMarkCount
|
||||||
|
|
||||||
|
|
||||||
fun playerMarkGrid(gridId: Int) {
|
fun playerMarkGrid(gridId: Int) {
|
||||||
if (_gridState[gridId].player == null) {
|
if (_gridState[gridId].playerMark == null) {
|
||||||
_gridState[gridId] = _gridState[gridId].copy(player = _currentPlayer)
|
_gridState[gridId] = _gridState[gridId].copy(playerMark = _currentPlayer)
|
||||||
_isWinner = GameChecks(_currentPlayer, gridData = gridState).playerWinnerCheck()
|
_isWinner = GameChecks(_currentPlayer, gridData = gridState).playerWinnerCheck()
|
||||||
_moveNumber = (_gridState.count { it.player != null })
|
_gridMarkCount = (_gridState.count { it.playerMark != null })
|
||||||
_currentPlayer = !_currentPlayer
|
_currentPlayer = !_currentPlayer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -36,7 +36,7 @@ class GameState : ViewModel() {
|
||||||
repeat(9) { _gridState[it] = Ficha() }
|
repeat(9) { _gridState[it] = Ficha() }
|
||||||
_currentPlayer = ramdomPlayer()
|
_currentPlayer = ramdomPlayer()
|
||||||
_isWinner = false
|
_isWinner = false
|
||||||
_moveNumber = null
|
_gridMarkCount = null
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun ramdomPlayer() = (Random.nextBits(bitCount = 1) > 0)
|
private fun ramdomPlayer() = (Random.nextBits(bitCount = 1) > 0)
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
package es.genol.tictactoe.ui.theme
|
package es.genol.tictactoe.ui.theme
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.filled.Close
|
import androidx.compose.material.icons.filled.Close
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
|
|
@ -10,31 +9,31 @@ import androidx.compose.ui.res.painterResource
|
||||||
import es.genol.tictactoe.R
|
import es.genol.tictactoe.R
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun CrossIcon() {
|
fun CrossIcon(modifier: Modifier = Modifier) {
|
||||||
Icon(
|
Icon(
|
||||||
Icons.Default.Close,
|
Icons.Default.Close,
|
||||||
contentDescription = "Cross red icon",
|
contentDescription = "Cross red icon",
|
||||||
modifier = Modifier.fillMaxSize(),
|
modifier = modifier,
|
||||||
tint = CustomRed
|
tint = CustomRed
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun CircleIcon() {
|
fun CircleIcon(modifier: Modifier = Modifier) {
|
||||||
Icon(
|
Icon(
|
||||||
painter = painterResource(id = R.drawable.outline_circle_24),
|
painter = painterResource(id = R.drawable.outline_circle_24),
|
||||||
contentDescription = "Cross red icon",
|
contentDescription = "Cross red icon",
|
||||||
modifier = Modifier.fillMaxSize(),
|
modifier = modifier,
|
||||||
tint = CustomGreen
|
tint = CustomGreen
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun DrawIcon() {
|
fun DrawIcon(modifier: Modifier = Modifier) {
|
||||||
Icon(
|
Icon(
|
||||||
painter = painterResource(id = R.drawable.baseline_handshake_24),
|
painter = painterResource(id = R.drawable.baseline_handshake_24),
|
||||||
contentDescription = "Draw blue icon",
|
contentDescription = "Draw blue icon",
|
||||||
modifier = Modifier.fillMaxSize(),
|
modifier = modifier,
|
||||||
tint = CustomBlue
|
tint = CustomBlue
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue