Add President and Golf
Two more games on the existing framework — no changes were needed to the lobby, networking, settings or update layers, which is what the shared GameRules/GameAi contract was for. President - Threes low, twos high. Lead any number of one rank; follow with the same count of a higher rank, or pass. When everyone has passed the pile comes down and the last player to play leads again. - Toggles: player count, rounds, twos clear the pile, four of a kind bombs, and the between-round card exchange. - Plays are offered as whole sets rather than by selecting cards. A set is the unit of the game, so choosing from the legal sets is both easier to operate and impossible to get wrong. Golf - 4, 6 or 9 card grids. Draw from stock or discard, then either swap the card in or — only if it came off the stock — throw it and turn one of your own over. A card taken from the discard pile has to be used. - Matching columns cancel to nothing; on the 3x3 board rows do too, but not on the shallower grids where three cards are not a line. - Face-down cards are hidden from everyone including their owner, which is the point of the game: viewFor masks them with a fixed placeholder so the real card never reaches the device that has not turned it over. Both engines are pure Kotlin and unit tested — 71 tests now, covering ranking, legal moves, pile and hole resolution, scoring, redaction, wire round-trips, and full random and AI playthroughs. The Golf test also checks the AI actually beats random play rather than merely being legal. Also widened the setup screen's option chips to wrap: the reveal-count row carries up to ten, and a chip off the edge of the screen is a setting that cannot be changed — the same failure as the Euchre Pass button. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_015qjfMiPUDshdKEUa6vxqzN
This commit is contained in:
@@ -2,6 +2,8 @@ package org.prolibertate.games.ui
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||
import androidx.compose.foundation.layout.FlowRow
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
@@ -29,6 +31,8 @@ import org.prolibertate.games.game.engine.PlayerKind
|
||||
import org.prolibertate.games.game.engine.PlayerSlot
|
||||
import org.prolibertate.games.game.engine.TableConfig
|
||||
import org.prolibertate.games.game.euchre.EuchreOptions
|
||||
import org.prolibertate.games.game.golf.GolfOptions
|
||||
import org.prolibertate.games.game.president.PresidentOptions
|
||||
import org.prolibertate.games.game.sequence.SequenceOptions
|
||||
|
||||
private val setupJson = Json { encodeDefaults = true }
|
||||
@@ -49,15 +53,21 @@ fun GameSetupScreen(
|
||||
) {
|
||||
var euchre by remember { mutableStateOf(EuchreOptions()) }
|
||||
var sequence by remember { mutableStateOf(SequenceOptions()) }
|
||||
var president by remember { mutableStateOf(PresidentOptions()) }
|
||||
var golf by remember { mutableStateOf(GolfOptions()) }
|
||||
|
||||
val optionsJson = when (descriptor.id) {
|
||||
GameCatalog.EUCHRE -> setupJson.encodeToString(euchre)
|
||||
GameCatalog.SEQUENCE -> setupJson.encodeToString(sequence)
|
||||
GameCatalog.PRESIDENT -> setupJson.encodeToString(president)
|
||||
GameCatalog.GOLF -> setupJson.encodeToString(golf)
|
||||
else -> "{}"
|
||||
}
|
||||
val seatCount = when (descriptor.id) {
|
||||
GameCatalog.EUCHRE -> 4
|
||||
GameCatalog.SEQUENCE -> sequence.playerCount
|
||||
GameCatalog.PRESIDENT -> president.playerCount
|
||||
GameCatalog.GOLF -> golf.playerCount
|
||||
else -> descriptor.minPlayers
|
||||
}
|
||||
|
||||
@@ -74,6 +84,8 @@ fun GameSetupScreen(
|
||||
when (descriptor.id) {
|
||||
GameCatalog.EUCHRE -> EuchreOptionsEditor(euchre) { euchre = it }
|
||||
GameCatalog.SEQUENCE -> SequenceOptionsEditor(sequence) { sequence = it }
|
||||
GameCatalog.PRESIDENT -> PresidentOptionsEditor(president) { president = it }
|
||||
GameCatalog.GOLF -> GolfOptionsEditor(golf) { golf = it }
|
||||
else -> Text("No options yet for this game.")
|
||||
}
|
||||
|
||||
@@ -121,8 +133,9 @@ private fun offlineConfig(
|
||||
|
||||
fun teamForSeat(gameId: String, seat: Int): Int = when (gameId) {
|
||||
// Partners sit opposite each other.
|
||||
GameCatalog.EUCHRE -> seat % 2
|
||||
else -> seat % 2
|
||||
GameCatalog.EUCHRE, GameCatalog.SEQUENCE -> seat % 2
|
||||
// Everyone else plays for themselves.
|
||||
else -> seat
|
||||
}
|
||||
|
||||
@Composable
|
||||
@@ -197,6 +210,95 @@ private fun SequenceOptionsEditor(options: SequenceOptions, onChange: (SequenceO
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PresidentOptionsEditor(
|
||||
options: PresidentOptions,
|
||||
onChange: (PresidentOptions) -> Unit,
|
||||
) {
|
||||
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
ChipRow(
|
||||
label = "Players",
|
||||
values = listOf(3, 4, 5, 6, 7),
|
||||
selected = options.playerCount,
|
||||
display = { "$it" },
|
||||
onSelect = { onChange(options.copy(playerCount = it)) },
|
||||
)
|
||||
ChipRow(
|
||||
label = "Rounds",
|
||||
values = listOf(3, 5, 7),
|
||||
selected = options.roundsToPlay,
|
||||
display = { "$it" },
|
||||
onSelect = { onChange(options.copy(roundsToPlay = it)) },
|
||||
)
|
||||
ToggleRow(
|
||||
title = "Twos clear the pile",
|
||||
subtitle = "A two beats anything and takes the pile down",
|
||||
checked = options.twosClear,
|
||||
onChange = { onChange(options.copy(twosClear = it)) },
|
||||
)
|
||||
ToggleRow(
|
||||
title = "Four of a kind bombs",
|
||||
subtitle = "Four matching cards beat anything, whatever the pile",
|
||||
checked = options.fourOfAKindBomb,
|
||||
onChange = { onChange(options.copy(fourOfAKindBomb = it)) },
|
||||
)
|
||||
ToggleRow(
|
||||
title = "Card exchange",
|
||||
subtitle = "After each round the Scum hands their best cards to the President",
|
||||
checked = options.cardExchange,
|
||||
onChange = { onChange(options.copy(cardExchange = it)) },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GolfOptionsEditor(options: GolfOptions, onChange: (GolfOptions) -> Unit) {
|
||||
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
ChipRow(
|
||||
label = "Players",
|
||||
values = listOf(2, 3, 4, 5, 6),
|
||||
selected = options.playerCount,
|
||||
display = { "$it" },
|
||||
onSelect = { onChange(options.copy(playerCount = it)) },
|
||||
)
|
||||
ChipRow(
|
||||
label = "Cards each",
|
||||
values = listOf(4, 6, 9),
|
||||
selected = options.gridSize,
|
||||
display = { "$it" },
|
||||
onSelect = { size ->
|
||||
// Keep the opening reveals inside the new grid.
|
||||
onChange(
|
||||
options.copy(
|
||||
gridSize = size,
|
||||
startingReveals = options.startingReveals.coerceAtMost(size),
|
||||
)
|
||||
)
|
||||
},
|
||||
)
|
||||
ChipRow(
|
||||
label = "Holes",
|
||||
values = listOf(3, 6, 9),
|
||||
selected = options.holes,
|
||||
display = { "$it" },
|
||||
onSelect = { onChange(options.copy(holes = it)) },
|
||||
)
|
||||
ChipRow(
|
||||
label = "Seen at the start",
|
||||
values = (0..options.gridSize).toList(),
|
||||
selected = options.startingReveals,
|
||||
display = { "$it" },
|
||||
onSelect = { onChange(options.copy(startingReveals = it)) },
|
||||
)
|
||||
Text(
|
||||
text = "${options.rows} rows of ${options.cols}. Matching columns cancel out" +
|
||||
if (options.gridSize == 9) ", and so do matching rows." else ".",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalLayoutApi::class)
|
||||
@Composable
|
||||
private fun <T> ChipRow(
|
||||
label: String,
|
||||
@@ -207,7 +309,12 @@ private fun <T> ChipRow(
|
||||
) {
|
||||
Column {
|
||||
Text(label, style = MaterialTheme.typography.labelLarge)
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
// Wraps rather than running off the edge: some of these rows carry ten
|
||||
// chips, and a chip you cannot see is a setting you cannot change.
|
||||
FlowRow(
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(4.dp),
|
||||
) {
|
||||
values.forEach { value ->
|
||||
FilterChip(
|
||||
selected = value == selected,
|
||||
|
||||
@@ -12,6 +12,16 @@ import org.prolibertate.games.game.euchre.EuchreMove
|
||||
import org.prolibertate.games.game.euchre.EuchrePhase
|
||||
import org.prolibertate.games.game.euchre.EuchreRules
|
||||
import org.prolibertate.games.game.euchre.EuchreState
|
||||
import org.prolibertate.games.game.golf.GolfAi
|
||||
import org.prolibertate.games.game.golf.GolfMove
|
||||
import org.prolibertate.games.game.golf.GolfPhase
|
||||
import org.prolibertate.games.game.golf.GolfRules
|
||||
import org.prolibertate.games.game.golf.GolfState
|
||||
import org.prolibertate.games.game.president.PresidentAi
|
||||
import org.prolibertate.games.game.president.PresidentMove
|
||||
import org.prolibertate.games.game.president.PresidentPhase
|
||||
import org.prolibertate.games.game.president.PresidentRules
|
||||
import org.prolibertate.games.game.president.PresidentState
|
||||
import org.prolibertate.games.game.sequence.SequenceAi
|
||||
import org.prolibertate.games.game.sequence.SequenceMove
|
||||
import org.prolibertate.games.game.sequence.SequenceRules
|
||||
@@ -19,6 +29,8 @@ import org.prolibertate.games.game.sequence.SequenceState
|
||||
import org.prolibertate.games.net.MatchController
|
||||
import org.prolibertate.games.settings.Settings
|
||||
import org.prolibertate.games.ui.game.EuchreScreen
|
||||
import org.prolibertate.games.ui.game.GolfScreen
|
||||
import org.prolibertate.games.ui.game.PresidentScreen
|
||||
import org.prolibertate.games.ui.game.TRICK_HOLD_MILLIS
|
||||
import org.prolibertate.games.ui.game.SequenceScreen
|
||||
|
||||
@@ -96,6 +108,50 @@ fun PlayScreen(
|
||||
SequenceScreen(controller = controller, localSeat = route.localSeat, onExit = onExit)
|
||||
}
|
||||
|
||||
GameCatalog.PRESIDENT -> {
|
||||
val controller = remember(route) {
|
||||
MatchController<PresidentState, PresidentMove>(
|
||||
rules = PresidentRules,
|
||||
ai = PresidentAi(),
|
||||
config = route.config,
|
||||
scope = scope,
|
||||
role = role,
|
||||
localSeats = setOf(route.localSeat),
|
||||
primarySeat = route.localSeat,
|
||||
advanceIdle = { state ->
|
||||
if (state.phase == PresidentPhase.ROUND_OVER) {
|
||||
PresidentRules.nextRound(state)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
},
|
||||
aiThinkingMillis = { settings.scaled(700L) },
|
||||
)
|
||||
}
|
||||
StartMatch(env, controller, route)
|
||||
PresidentScreen(controller = controller, localSeat = route.localSeat, onExit = onExit)
|
||||
}
|
||||
|
||||
GameCatalog.GOLF -> {
|
||||
val controller = remember(route) {
|
||||
MatchController<GolfState, GolfMove>(
|
||||
rules = GolfRules,
|
||||
ai = GolfAi(),
|
||||
config = route.config,
|
||||
scope = scope,
|
||||
role = role,
|
||||
localSeats = setOf(route.localSeat),
|
||||
primarySeat = route.localSeat,
|
||||
advanceIdle = { state ->
|
||||
if (state.phase == GolfPhase.HOLE_OVER) GolfRules.nextHole(state) else null
|
||||
},
|
||||
aiThinkingMillis = { settings.scaled(700L) },
|
||||
)
|
||||
}
|
||||
StartMatch(env, controller, route)
|
||||
GolfScreen(controller = controller, localSeat = route.localSeat, onExit = onExit)
|
||||
}
|
||||
|
||||
else -> Text("That game isn't playable yet.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,335 @@
|
||||
package org.prolibertate.games.ui.game
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.horizontalScroll
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.BoxWithConstraints
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.FilterChip
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import androidx.compose.ui.unit.dp
|
||||
import org.prolibertate.games.game.golf.DiscardAndFlip
|
||||
import org.prolibertate.games.game.golf.DrawFromDiscard
|
||||
import org.prolibertate.games.game.golf.DrawFromStock
|
||||
import org.prolibertate.games.game.golf.GolfMove
|
||||
import org.prolibertate.games.game.golf.GolfPhase
|
||||
import org.prolibertate.games.game.golf.GolfState
|
||||
import org.prolibertate.games.game.golf.ReplaceCard
|
||||
import org.prolibertate.games.game.golf.golfValue
|
||||
import org.prolibertate.games.net.MatchController
|
||||
import org.prolibertate.games.ui.PrimaryAction
|
||||
import org.prolibertate.games.ui.ScreenScaffold
|
||||
import org.prolibertate.games.ui.common.CardBackView
|
||||
import org.prolibertate.games.ui.common.PlayingCardView
|
||||
import org.prolibertate.games.ui.theme.FeltGreenDark
|
||||
|
||||
/** What tapping one of your own cards will do once you're holding a card. */
|
||||
private enum class PlaceMode(val label: String) {
|
||||
SWAP("Swap it in"),
|
||||
THROW("Throw it and turn one over"),
|
||||
}
|
||||
|
||||
/**
|
||||
* The Golf table.
|
||||
*
|
||||
* The grid is the game, so it gets the space: your own cards are large and
|
||||
* tappable, everyone else's are small. Face-down cards are drawn as backs —
|
||||
* including your own, which you genuinely have not seen.
|
||||
*/
|
||||
@Composable
|
||||
fun GolfScreen(
|
||||
controller: MatchController<GolfState, GolfMove>,
|
||||
localSeat: Int,
|
||||
onExit: () -> Unit,
|
||||
) {
|
||||
val state by controller.state.collectAsState()
|
||||
val legal by controller.legalMoves.collectAsState()
|
||||
var mode by remember { mutableStateOf(PlaceMode.SWAP) }
|
||||
|
||||
val current = state
|
||||
if (current == null) {
|
||||
ScreenScaffold(title = "Golf", onBack = onExit) { modifier ->
|
||||
Box(modifier, contentAlignment = Alignment.Center) { Text("Dealing…") }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ScreenScaffold(title = "Golf", onBack = onExit) { modifier ->
|
||||
BoxWithConstraints(modifier = modifier) {
|
||||
val shortest = minOf(maxWidth, maxHeight)
|
||||
val ownCard = (shortest * 0.16f).coerceIn(40.dp, 84.dp)
|
||||
val otherCard = (shortest * 0.07f).coerceIn(20.dp, 40.dp)
|
||||
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
) {
|
||||
ScoreLine(current, localSeat)
|
||||
|
||||
Box(
|
||||
modifier = Modifier.fillMaxWidth().background(FeltGreenDark).padding(6.dp),
|
||||
) {
|
||||
StockAndDiscard(current, localSeat, legal, ownCard) { controller.submit(it) }
|
||||
}
|
||||
|
||||
OpponentGrids(current, localSeat, otherCard)
|
||||
|
||||
if (current.phase == GolfPhase.PLACE && legal.isNotEmpty()) {
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
PlaceMode.entries.forEach { option ->
|
||||
val enabled = option == PlaceMode.SWAP ||
|
||||
legal.any { it is DiscardAndFlip }
|
||||
FilterChip(
|
||||
selected = mode == option,
|
||||
enabled = enabled,
|
||||
onClick = { mode = option },
|
||||
label = { Text(option.label) },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
when (current.phase) {
|
||||
GolfPhase.HOLE_OVER -> Text(
|
||||
text = "Hole ${current.hole + 1}: " + current.holeScores
|
||||
.mapIndexed { seat, s -> "seat $seat $s" }.joinToString(", "),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
|
||||
GolfPhase.GAME_OVER -> {
|
||||
Text("Game over — lowest score wins.", fontWeight = FontWeight.Bold)
|
||||
PrimaryAction(text = "Back to the menu") { onExit() }
|
||||
}
|
||||
|
||||
else -> Unit
|
||||
}
|
||||
|
||||
OwnGrid(
|
||||
state = current,
|
||||
localSeat = localSeat,
|
||||
cardWidth = ownCard,
|
||||
legal = legal,
|
||||
mode = mode,
|
||||
onMove = { controller.submit(it); mode = PlaceMode.SWAP },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ScoreLine(state: GolfState, localSeat: Int) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Column {
|
||||
Text("Hole ${state.hole + 1} of ${state.options.holes}", fontWeight = FontWeight.Bold)
|
||||
Text(
|
||||
text = "Your total: ${state.scores[localSeat]} (lowest wins)",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
}
|
||||
state.closedBy?.let {
|
||||
Text(
|
||||
text = if (it == localSeat) "You closed it out" else "Seat $it is out",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun StockAndDiscard(
|
||||
state: GolfState,
|
||||
localSeat: Int,
|
||||
legal: List<GolfMove>,
|
||||
cardWidth: Dp,
|
||||
onMove: (GolfMove) -> Unit,
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(16.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Column(horizontalAlignment = Alignment.CenterHorizontally) {
|
||||
Text("Stock", style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onPrimary)
|
||||
CardBackView(width = cardWidth)
|
||||
if (legal.contains(DrawFromStock)) {
|
||||
Button(onClick = { onMove(DrawFromStock) }) { Text("Draw") }
|
||||
}
|
||||
}
|
||||
|
||||
Column(horizontalAlignment = Alignment.CenterHorizontally) {
|
||||
Text("Discard", style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onPrimary)
|
||||
state.discard.lastOrNull()?.let { PlayingCardView(card = it, width = cardWidth) }
|
||||
if (legal.contains(DrawFromDiscard)) {
|
||||
Button(onClick = { onMove(DrawFromDiscard) }) { Text("Take") }
|
||||
}
|
||||
}
|
||||
|
||||
state.drawn?.let { drawn ->
|
||||
Column(horizontalAlignment = Alignment.CenterHorizontally) {
|
||||
Text("Holding", style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onPrimary)
|
||||
PlayingCardView(card = drawn, width = cardWidth, selected = true)
|
||||
Text(
|
||||
text = "worth ${golfValue(drawn)}",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onPrimary,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (state.phase == GolfPhase.DRAW && state.turn != localSeat) {
|
||||
Text(
|
||||
text = "Seat ${state.turn} is playing…",
|
||||
color = MaterialTheme.colorScheme.onPrimary,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun OwnGrid(
|
||||
state: GolfState,
|
||||
localSeat: Int,
|
||||
cardWidth: Dp,
|
||||
legal: List<GolfMove>,
|
||||
mode: PlaceMode,
|
||||
onMove: (GolfMove) -> Unit,
|
||||
) {
|
||||
val options = state.options
|
||||
val replacements = legal.filterIsInstance<ReplaceCard>().map { it.index }.toSet()
|
||||
val flips = legal.filterIsInstance<DiscardAndFlip>().map { it.index }.toSet()
|
||||
|
||||
Column {
|
||||
Text(
|
||||
text = when {
|
||||
state.phase == GolfPhase.PLACE && mode == PlaceMode.SWAP ->
|
||||
"Tap a card to swap it out"
|
||||
|
||||
state.phase == GolfPhase.PLACE ->
|
||||
"Tap a face-down card to turn it over"
|
||||
|
||||
else -> "Your cards"
|
||||
},
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
)
|
||||
for (row in 0 until options.rows) {
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(4.dp)) {
|
||||
for (col in 0 until options.cols) {
|
||||
val index = row * options.cols + col
|
||||
val tappable = when (mode) {
|
||||
PlaceMode.SWAP -> index in replacements
|
||||
PlaceMode.THROW -> index in flips
|
||||
}
|
||||
GridCard(
|
||||
card = state.grids[localSeat].getOrNull(index),
|
||||
faceUp = state.revealed[localSeat].getOrElse(index) { false },
|
||||
width = cardWidth,
|
||||
tappable = tappable,
|
||||
onClick = {
|
||||
when (mode) {
|
||||
PlaceMode.SWAP -> onMove(ReplaceCard(index))
|
||||
PlaceMode.THROW -> onMove(DiscardAndFlip(index))
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GridCard(
|
||||
card: org.prolibertate.games.game.cards.Card?,
|
||||
faceUp: Boolean,
|
||||
width: Dp,
|
||||
tappable: Boolean,
|
||||
onClick: () -> Unit,
|
||||
) {
|
||||
val shape = RoundedCornerShape(width * 0.12f)
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.clip(shape)
|
||||
.border(
|
||||
width = if (tappable) 3.dp else 0.dp,
|
||||
color = if (tappable) {
|
||||
MaterialTheme.colorScheme.tertiary
|
||||
} else {
|
||||
androidx.compose.ui.graphics.Color.Transparent
|
||||
},
|
||||
shape = shape,
|
||||
)
|
||||
.then(if (tappable) Modifier.clickable { onClick() } else Modifier)
|
||||
.padding(2.dp),
|
||||
) {
|
||||
if (faceUp && card != null) {
|
||||
PlayingCardView(card = card, width = width)
|
||||
} else {
|
||||
CardBackView(width = width)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun OpponentGrids(state: GolfState, localSeat: Int, cardWidth: Dp) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().horizontalScroll(rememberScrollState()),
|
||||
horizontalArrangement = Arrangement.spacedBy(14.dp),
|
||||
) {
|
||||
(0 until state.options.playerCount)
|
||||
.filter { it != localSeat }
|
||||
.forEach { seat ->
|
||||
Column(horizontalAlignment = Alignment.CenterHorizontally) {
|
||||
Text(
|
||||
text = "Seat $seat",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
fontWeight = if (seat == state.turn) FontWeight.Bold else FontWeight.Normal,
|
||||
)
|
||||
for (row in 0 until state.options.rows) {
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(2.dp)) {
|
||||
for (col in 0 until state.options.cols) {
|
||||
val index = row * state.options.cols + col
|
||||
val faceUp = state.revealed[seat].getOrElse(index) { false }
|
||||
if (faceUp) {
|
||||
PlayingCardView(
|
||||
card = state.grids[seat][index],
|
||||
width = cardWidth,
|
||||
)
|
||||
} else {
|
||||
CardBackView(width = cardWidth)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,265 @@
|
||||
package org.prolibertate.games.ui.game
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.horizontalScroll
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.BoxWithConstraints
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||
import androidx.compose.foundation.layout.FlowRow
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import androidx.compose.ui.unit.dp
|
||||
import org.prolibertate.games.game.president.PassTurn
|
||||
import org.prolibertate.games.game.president.PlayCards
|
||||
import org.prolibertate.games.game.president.PresidentMove
|
||||
import org.prolibertate.games.game.president.PresidentPhase
|
||||
import org.prolibertate.games.game.president.PresidentState
|
||||
import org.prolibertate.games.game.president.titleFor
|
||||
import org.prolibertate.games.net.MatchController
|
||||
import org.prolibertate.games.ui.PrimaryAction
|
||||
import org.prolibertate.games.ui.ScreenScaffold
|
||||
import org.prolibertate.games.ui.common.CardBackView
|
||||
import org.prolibertate.games.ui.common.PlayingCardView
|
||||
import org.prolibertate.games.ui.theme.FeltGreenDark
|
||||
|
||||
/**
|
||||
* The President table.
|
||||
*
|
||||
* Plays are offered as whole sets rather than by selecting individual cards.
|
||||
* A set is the unit of the game — you never play four cards of different
|
||||
* ranks — so picking from the legal sets is both simpler to operate and
|
||||
* impossible to get wrong.
|
||||
*/
|
||||
@OptIn(ExperimentalLayoutApi::class)
|
||||
@Composable
|
||||
fun PresidentScreen(
|
||||
controller: MatchController<PresidentState, PresidentMove>,
|
||||
localSeat: Int,
|
||||
onExit: () -> Unit,
|
||||
) {
|
||||
val state by controller.state.collectAsState()
|
||||
val legal by controller.legalMoves.collectAsState()
|
||||
|
||||
val current = state
|
||||
if (current == null) {
|
||||
ScreenScaffold(title = "President", onBack = onExit) { modifier ->
|
||||
Box(modifier, contentAlignment = Alignment.Center) { Text("Dealing…") }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ScreenScaffold(title = "President", onBack = onExit) { modifier ->
|
||||
BoxWithConstraints(modifier = modifier) {
|
||||
val cardWidth = (minOf(maxWidth, maxHeight) * 0.15f).coerceIn(44.dp, 88.dp)
|
||||
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
) {
|
||||
StandingsBar(current, localSeat)
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.weight(1f)
|
||||
.background(FeltGreenDark),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
PileView(current, cardWidth)
|
||||
}
|
||||
|
||||
OpponentRow(current, localSeat)
|
||||
|
||||
when (current.phase) {
|
||||
PresidentPhase.PLAYING -> PlayChoices(current, localSeat, legal) {
|
||||
controller.submit(it)
|
||||
}
|
||||
|
||||
PresidentPhase.ROUND_OVER -> Text(
|
||||
text = "Round over — " + current.finishedOrder
|
||||
.mapIndexed { position, seat ->
|
||||
"${titleFor(position, current.options.playerCount)}: seat $seat"
|
||||
}
|
||||
.joinToString(", "),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
|
||||
PresidentPhase.GAME_OVER -> {
|
||||
Text("Game over.", fontWeight = FontWeight.Bold)
|
||||
PrimaryAction(text = "Back to the menu") { onExit() }
|
||||
}
|
||||
}
|
||||
|
||||
HandView(current, localSeat, cardWidth)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun StandingsBar(state: PresidentState, localSeat: Int) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Column {
|
||||
Text("Round ${state.roundNumber + 1} of ${state.options.roundsToPlay}",
|
||||
fontWeight = FontWeight.Bold)
|
||||
Text(
|
||||
text = "Your score: ${state.scores[localSeat]}",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
}
|
||||
Column(horizontalAlignment = Alignment.End) {
|
||||
val position = state.finishedOrder.indexOf(localSeat)
|
||||
Text(
|
||||
text = if (position >= 0) {
|
||||
"You are out — ${titleFor(position, state.options.playerCount)}"
|
||||
} else {
|
||||
"${state.hands[localSeat].size} cards in hand"
|
||||
},
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PileView(state: PresidentState, cardWidth: Dp) {
|
||||
if (state.setSize == 0) {
|
||||
Text(
|
||||
text = "Pile is clear — anything leads",
|
||||
color = MaterialTheme.colorScheme.onPrimary,
|
||||
)
|
||||
return
|
||||
}
|
||||
Column(horizontalAlignment = Alignment.CenterHorizontally) {
|
||||
Text(
|
||||
text = "On the pile",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onPrimary,
|
||||
)
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
state.pile.takeLast(state.setSize).forEach { card ->
|
||||
PlayingCardView(card = card, width = cardWidth)
|
||||
}
|
||||
}
|
||||
Text(
|
||||
text = "Beat it with ${state.setSize} card(s)",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onPrimary,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** Everyone else, as card counts — their hands are never sent to this device. */
|
||||
@Composable
|
||||
private fun OpponentRow(state: PresidentState, localSeat: Int) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().horizontalScroll(rememberScrollState()),
|
||||
horizontalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
(0 until state.options.playerCount)
|
||||
.filter { it != localSeat }
|
||||
.forEach { seat ->
|
||||
val count = state.handCounts.getOrElse(seat) { 0 }
|
||||
Column(horizontalAlignment = Alignment.CenterHorizontally) {
|
||||
Text(
|
||||
text = "Seat $seat",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
fontWeight = if (seat == state.turn) FontWeight.Bold else FontWeight.Normal,
|
||||
)
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(1.dp)) {
|
||||
repeat(count.coerceAtMost(6)) { CardBackView(width = 14.dp) }
|
||||
}
|
||||
Text(
|
||||
text = if (count == 0) "out" else "$count",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalLayoutApi::class)
|
||||
@Composable
|
||||
private fun PlayChoices(
|
||||
state: PresidentState,
|
||||
localSeat: Int,
|
||||
legal: List<PresidentMove>,
|
||||
onMove: (PresidentMove) -> Unit,
|
||||
) {
|
||||
if (legal.isEmpty()) {
|
||||
Text(
|
||||
text = if (state.isOut(localSeat)) "You're out — watching." else "Seat ${state.turn} to play…",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
val plays = legal.filterIsInstance<PlayCards>()
|
||||
val canPass = legal.contains(PassTurn)
|
||||
|
||||
Column(verticalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
Text(
|
||||
text = if (state.setSize == 0) "Lead a set" else "Play or pass",
|
||||
fontWeight = FontWeight.Bold,
|
||||
)
|
||||
FlowRow(
|
||||
horizontalArrangement = Arrangement.spacedBy(6.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(6.dp),
|
||||
) {
|
||||
plays.forEach { play ->
|
||||
Button(onClick = { onMove(play) }) {
|
||||
Text(play.cards.joinToString(" ") { it.label })
|
||||
}
|
||||
}
|
||||
}
|
||||
if (canPass) {
|
||||
OutlinedButton(
|
||||
onClick = { onMove(PassTurn) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) {
|
||||
Text("Pass")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun HandView(state: PresidentState, localSeat: Int, cardWidth: Dp) {
|
||||
val hand = state.hands.getOrNull(localSeat).orEmpty()
|
||||
Column {
|
||||
Text("Your hand", style = MaterialTheme.typography.labelLarge)
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.horizontalScroll(rememberScrollState())
|
||||
.padding(vertical = 4.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(4.dp),
|
||||
) {
|
||||
if (hand.isEmpty()) {
|
||||
Text("—", style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
hand.forEach { card ->
|
||||
PlayingCardView(card = card, width = cardWidth * 0.8f)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user