feat: visible AI turns with 800ms delay between each move
This commit is contained in:
@@ -13,6 +13,7 @@ import androidx.compose.ui.window.application
|
||||
import com.uno.game.GameEngine
|
||||
import com.uno.game.model.*
|
||||
import com.uno.shared.UnoGameScreen
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
fun main() = application {
|
||||
Window(
|
||||
@@ -29,6 +30,24 @@ fun UnoDesktopApp() {
|
||||
val engine = remember { GameEngine() }
|
||||
var gameState by remember { mutableStateOf(engine.startGame()) }
|
||||
var gameStarted by remember { mutableStateOf(false) }
|
||||
var isAiThinking by remember { mutableStateOf(false) }
|
||||
|
||||
// KI-Züge mit Verzögerung
|
||||
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) {
|
||||
DesktopStartScreen(onStartGame = { playerCount ->
|
||||
@@ -40,11 +59,9 @@ fun UnoDesktopApp() {
|
||||
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) }
|
||||
)
|
||||
@@ -52,20 +69,15 @@ fun UnoDesktopApp() {
|
||||
if (gameState.isFinished) {
|
||||
DesktopGameOverScreen(
|
||||
winner = gameState.winner,
|
||||
onPlayAgain = { gameState = engine.startGame() }
|
||||
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 DesktopStartScreen(onStartGame: (Int) -> Unit) {
|
||||
Column(
|
||||
|
||||
Reference in New Issue
Block a user