feat: visible AI turns with 800ms delay between each move

This commit is contained in:
2026-08-02 18:33:35 +02:00
parent 2b2816de3c
commit 2187bf2f47
3 changed files with 89 additions and 76 deletions
@@ -13,6 +13,7 @@ import androidx.compose.ui.unit.dp
import com.uno.game.GameEngine
import com.uno.game.model.*
import com.uno.shared.UnoGameScreen
import kotlinx.coroutines.delay
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@@ -33,6 +34,23 @@ fun UnoApp() {
val engine = remember { GameEngine() }
var gameState by remember { mutableStateOf(engine.startGame()) }
var gameStarted by remember { mutableStateOf(false) }
var isAiThinking by remember { mutableStateOf(false) }
LaunchedEffect(gameState.currentPlayerIndex, gameState.phase) {
if (gameState.currentPlayerIndex != 0
&& gameState.phase == GamePhase.PLAYING
&& !gameState.isFinished
) {
isAiThinking = true
var currentState = gameState
while (currentState.currentPlayerIndex != 0 && !currentState.isFinished) {
delay(800)
currentState = engine.aiPlay(currentState, currentState.currentPlayerIndex)
gameState = currentState
}
isAiThinking = false
}
}
if (!gameStarted) {
StartScreen(onStartGame = { playerCount ->
@@ -44,29 +62,22 @@ fun UnoApp() {
state = gameState,
onPlayCard = { card, color ->
gameState = engine.playCard(gameState, 0, card, color)
gameState = runAiTurns(engine, gameState)
},
onDrawCard = {
gameState = engine.drawCard(gameState, 0)
gameState = runAiTurns(engine, gameState)
},
onCallUno = { gameState = engine.callUno(gameState, 0) }
)
if (gameState.isFinished) {
GameOverScreen(winner = gameState.winner, onPlayAgain = { gameState = engine.startGame() })
GameOverScreen(winner = gameState.winner, onPlayAgain = {
gameState = engine.startGame()
gameStarted = false
})
}
}
}
private fun runAiTurns(engine: GameEngine, state: GameState): GameState {
var currentState = state
while (currentState.currentPlayerIndex != 0 && !currentState.isFinished) {
currentState = engine.aiPlay(currentState, currentState.currentPlayerIndex)
}
return currentState
}
@Composable
fun StartScreen(onStartGame: (Int) -> Unit) {
Column(