feat: UNO Kotlin Multiplatform app (Android, Desktop, Web)
- Full UNO game logic with AI opponents - Compose Multiplatform UI with animations - Shared module (commonMain) with game engine - Desktop app (Compose Desktop) - Android app (Compose Multiplatform) - Web app (Kotlin/Wasm) - Nix shell for reproducible dev environment
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
plugins {
|
||||
alias(libs.plugins.kotlinMultiplatform)
|
||||
alias(libs.plugins.composeMultiplatform)
|
||||
alias(libs.plugins.composeCompiler)
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvm()
|
||||
|
||||
sourceSets {
|
||||
jvmMain.dependencies {
|
||||
implementation(project(":shared"))
|
||||
implementation(compose.desktop.currentOs)
|
||||
implementation(compose.material3)
|
||||
implementation(compose.foundation)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
compose.desktop {
|
||||
application {
|
||||
mainClass = "com.uno.desktop.MainKt"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.uno.desktop
|
||||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.DpSize
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Window
|
||||
import androidx.compose.ui.window.WindowState
|
||||
import androidx.compose.ui.window.application
|
||||
import com.uno.game.GameEngine
|
||||
import com.uno.game.model.*
|
||||
import com.uno.shared.UnoGameScreen
|
||||
|
||||
fun main() = application {
|
||||
Window(
|
||||
onCloseRequest = ::exitApplication,
|
||||
title = "UNO - Kotlin Multiplatform",
|
||||
state = WindowState(size = DpSize(900.dp, 700.dp))
|
||||
) {
|
||||
UnoDesktopApp()
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun UnoDesktopApp() {
|
||||
val engine = remember { GameEngine() }
|
||||
var gameState by remember { mutableStateOf(engine.startGame()) }
|
||||
var gameStarted by remember { mutableStateOf(false) }
|
||||
|
||||
if (!gameStarted) {
|
||||
DesktopStartScreen(onStartGame = { playerCount ->
|
||||
gameState = engine.startGame(playerCount)
|
||||
gameStarted = true
|
||||
})
|
||||
} else {
|
||||
UnoGameScreen(
|
||||
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) {
|
||||
DesktopGameOverScreen(
|
||||
winner = gameState.winner,
|
||||
onPlayAgain = { gameState = engine.startGame() }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
Text("UNO", style = MaterialTheme.typography.displayLarge, color = MaterialTheme.colorScheme.primary)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Text("Desktop Edition", style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
Spacer(modifier = Modifier.height(24.dp))
|
||||
Button(onClick = { onStartGame(4) }) { Text("Spiel starten (4 Spieler)") }
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun DesktopGameOverScreen(winner: Player?, onPlayAgain: () -> Unit) {
|
||||
AlertDialog(
|
||||
onDismissRequest = onPlayAgain,
|
||||
title = { Text("Spiel vorbei!") },
|
||||
text = { Text(if (winner != null) "${winner.name} hat gewonnen!" else "Unentschieden!") },
|
||||
confirmButton = {
|
||||
Button(onClick = onPlayAgain) { Text("Nochmal spielen") }
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user