Katan: playable screen, setup options, dev-preview catalogue gate
Adds the Katan screen on top of the WALA-119 engine, built in separable layers so the look, wording, layout and interaction can each be changed alone: - CatanStyle: every colour and proportion (light/dark), via LocalCatanStyle - CatanText: every word on screen (pure functions of state) - CatanGeometry: lattice -> pixels and tap hit-testing (pure, unit-tested) - CatanInteraction: what is tappable and what a tap means, derived only from the engine's legal-move list (pure, unit-tested) - CatanBoardView / CatanPanels / CatanDialogs: stateless composables - CatanScreen: wiring to MatchController, portrait and landscape layouts - CatanPreviews: Android Studio previews of real positions - docs/UI-catan.md: how to adapt each layer, rules of the road, known gaps Wiring: PlayScreen branch, GameSetupScreen options (3-4 players, game to 5/8/10/12), and a GameDescriptor.devPreview flag so dev builds can open Katan while production still hides it until two-device play is verified. Tests: CatanGeometryTest, CatanInteractionTest, CatanTextTest, CatalogTest. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JdMNbY5hLR7FjtfEE712xG
This commit is contained in:
@@ -121,6 +121,20 @@ class CatalogTest {
|
||||
assertEquals(listOf(GameCatalog.byId(GameCatalog.CATAN)!!), hiddenFromProduction)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a preview game opens on a dev build and nowhere else`() {
|
||||
val preview = GameCatalog.all.filter { it.devPreview }
|
||||
assertTrue("Katan is the preview game", preview.map { it.id } == listOf(GameCatalog.CATAN))
|
||||
preview.forEach { game ->
|
||||
assertFalse("${game.id}: preview is for games that are not yet available", game.available)
|
||||
assertTrue("${game.id} should open on a dev build", game.playableIn(devBuild = true))
|
||||
assertFalse("${game.id} must not open in production", game.playableIn(devBuild = false))
|
||||
}
|
||||
// An ordinary finished game opens everywhere.
|
||||
val chess = GameCatalog.byId(GameCatalog.CHESS)!!
|
||||
assertTrue(chess.playableIn(devBuild = true) && chess.playableIn(devBuild = false))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hiding unfinished games still leaves something to play in every menu`() {
|
||||
// A menu filtered down to nothing would leave a stranded heading, and a
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package org.prolibertate.games.ui.game.catan
|
||||
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.prolibertate.games.game.GameCatalog
|
||||
import org.prolibertate.games.game.catan.BuildingType
|
||||
import org.prolibertate.games.game.catan.CatanBuilding
|
||||
import org.prolibertate.games.game.catan.CatanOptions
|
||||
import org.prolibertate.games.game.catan.CatanPhase
|
||||
import org.prolibertate.games.game.catan.CatanRules
|
||||
import org.prolibertate.games.game.catan.CatanState
|
||||
import org.prolibertate.games.game.catan.Resource
|
||||
import org.prolibertate.games.game.engine.PlayerKind
|
||||
import org.prolibertate.games.game.engine.PlayerSlot
|
||||
import org.prolibertate.games.game.engine.TableConfig
|
||||
|
||||
/** Shared fixtures for the Katan UI-logic tests: real islands, bent to taste. */
|
||||
internal object CatanFixtures {
|
||||
|
||||
private val json = Json { encodeDefaults = true }
|
||||
|
||||
fun config(players: Int = 3, seed: Long = 7L) = TableConfig(
|
||||
gameId = GameCatalog.CATAN,
|
||||
seats = (0 until players).map { PlayerSlot(seat = it, name = "P$it", kind = PlayerKind.AI, team = it) },
|
||||
optionsJson = json.encodeToString(CatanOptions(playerCount = players)),
|
||||
seed = seed,
|
||||
)
|
||||
|
||||
/** A fresh table, on the first setup placement. */
|
||||
fun start(players: Int = 3, seed: Long = 7L): CatanState =
|
||||
CatanRules.initialState(config(players, seed))
|
||||
|
||||
/** A mid-game state on [turn]'s turn, then adjusted by [adjust]. */
|
||||
fun playing(
|
||||
turn: Int = 0,
|
||||
players: Int = 3,
|
||||
adjust: CatanState.() -> CatanState = { this },
|
||||
): CatanState = start(players)
|
||||
.copy(phase = CatanPhase.PLAYING, turn = turn, turnNumber = 1)
|
||||
.adjust()
|
||||
|
||||
fun hand(vararg cards: Pair<Resource, Int>): Map<Resource, Int> =
|
||||
Resource.entries.associateWith { cards.toMap()[it] ?: 0 }
|
||||
|
||||
fun CatanState.withHand(seat: Int, hand: Map<Resource, Int>): CatanState =
|
||||
copy(resources = resources.toMutableList().also { it[seat] = hand })
|
||||
|
||||
fun CatanState.withBuilding(vertex: Int, type: BuildingType, owner: Int): CatanState =
|
||||
copy(buildings = buildings.toMutableList().also { it[vertex] = CatanBuilding(type, owner) })
|
||||
|
||||
fun CatanState.withRoad(edge: Int, owner: Int): CatanState =
|
||||
copy(roads = roads.toMutableList().also { it[edge] = owner })
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package org.prolibertate.games.ui.game.catan
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.prolibertate.games.game.catan.CatanBoard
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.sqrt
|
||||
|
||||
class CatanGeometryTest {
|
||||
|
||||
private val sizes = listOf(360f to 800f, 800f to 360f, 500f to 500f, 1000f to 1000f / CatanGeometry.ASPECT)
|
||||
|
||||
private fun dist(a: Pt, b: Pt): Float = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y))
|
||||
|
||||
@Test
|
||||
fun `the whole island fits inside the box at every shape`() {
|
||||
for ((w, h) in sizes) {
|
||||
val layout = CatanGeometry.fit(w, h)
|
||||
for (v in 0 until CatanBoard.VERTEX_COUNT) {
|
||||
val p = layout.vertex(v)
|
||||
assertTrue("vertex $v x=${p.x} outside 0..$w", p.x in 0f..w)
|
||||
assertTrue("vertex $v y=${p.y} outside 0..$h", p.y in 0f..h)
|
||||
}
|
||||
for (e in CatanBoard.PORT_POSITIONS) {
|
||||
val p = layout.portAnchor(e)
|
||||
assertTrue("harbour marker x=${p.x} outside 0..$w at ${w}x$h", p.x in 0f..w)
|
||||
assertTrue("harbour marker y=${p.y} outside 0..$h at ${w}x$h", p.y in 0f..h)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the layout is as large as the box allows`() {
|
||||
// Tall and narrow is limited by width, short and wide by height; either
|
||||
// way the island should use most of the limiting side.
|
||||
val tall = CatanGeometry.fit(360f, 800f)
|
||||
val wide = CatanGeometry.fit(800f, 360f)
|
||||
assertTrue(tall.radius > 20f)
|
||||
assertTrue(wide.radius > 20f)
|
||||
val xs = (0 until CatanBoard.VERTEX_COUNT).map { tall.vertex(it).x }
|
||||
assertTrue("island should span most of the width", xs.max() - xs.min() > 360f * 0.7f)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `every edge is one hex radius long and every hex corner is one radius from its centre`() {
|
||||
val layout = CatanGeometry.fit(500f, 500f)
|
||||
for (e in 0 until CatanBoard.EDGE_COUNT) {
|
||||
val (a, b) = layout.edgeEnds(e)
|
||||
assertEquals("edge $e length", layout.radius, dist(a, b), layout.radius * 0.01f)
|
||||
}
|
||||
for (h in 0 until CatanBoard.HEX_COUNT) {
|
||||
val centre = layout.hexCenter(h)
|
||||
for (corner in layout.hexCorners(h)) {
|
||||
assertEquals("hex $h corner", layout.radius, dist(centre, corner), layout.radius * 0.01f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `all fifty-four vertices sit at distinct places`() {
|
||||
val layout = CatanGeometry.fit(500f, 500f)
|
||||
val places = (0 until CatanBoard.VERTEX_COUNT).map {
|
||||
val p = layout.vertex(it)
|
||||
Math.round(p.x) to Math.round(p.y)
|
||||
}.toSet()
|
||||
assertEquals(CatanBoard.VERTEX_COUNT, places.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a tap on a vertex finds that vertex, and a tap far away finds nothing`() {
|
||||
val layout = CatanGeometry.fit(500f, 500f)
|
||||
val all = (0 until CatanBoard.VERTEX_COUNT).toList()
|
||||
for (v in all) {
|
||||
val p = layout.vertex(v)
|
||||
assertEquals(v, layout.nearestVertex(p, layout.radius * 0.5f, all))
|
||||
// A slightly clumsy tap still lands on it.
|
||||
assertEquals(v, layout.nearestVertex(Pt(p.x + layout.radius * 0.2f, p.y - layout.radius * 0.15f), layout.radius * 0.55f, all))
|
||||
}
|
||||
assertNull(layout.nearestVertex(Pt(-100f, -100f), layout.radius * 0.5f, all))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hit testing only considers the candidates it is given`() {
|
||||
val layout = CatanGeometry.fit(500f, 500f)
|
||||
val at = layout.vertex(5)
|
||||
assertNull(layout.nearestVertex(at, layout.radius * 0.3f, listOf(6)))
|
||||
assertEquals(6, layout.nearestVertex(layout.vertex(6), layout.radius * 0.3f, listOf(6)))
|
||||
assertEquals(3, layout.nearestEdge(layout.edgeMidpoint(3), 5f, listOf(3, 4)))
|
||||
assertEquals(7, layout.nearestHex(layout.hexCenter(7), 5f, listOf(7, 8)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the nearest candidate wins when several are in reach`() {
|
||||
val layout = CatanGeometry.fit(500f, 500f)
|
||||
val a = 10
|
||||
val b = CatanBoard.VERTEX_NEIGHBORS[a].first()
|
||||
val pa = layout.vertex(a)
|
||||
val pb = layout.vertex(b)
|
||||
// A point two-thirds of the way toward b is nearer b, though both are in reach.
|
||||
val at = Pt(pa.x + (pb.x - pa.x) * 0.66f, pa.y + (pb.y - pa.y) * 0.66f)
|
||||
assertEquals(b, layout.nearestVertex(at, layout.radius * 2f, listOf(a, b)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `harbour markers sit outside the coast`() {
|
||||
val layout = CatanGeometry.fit(500f, 500f)
|
||||
val centre = Pt(500f / 2f, 500f / 2f)
|
||||
for (e in CatanBoard.PORT_POSITIONS) {
|
||||
val anchor = layout.portAnchor(e)
|
||||
val mid = layout.edgeMidpoint(e)
|
||||
assertTrue("harbour on edge $e should be past the coast", dist(anchor, centre) > dist(mid, centre))
|
||||
assertTrue("and not floating far from it", abs(dist(anchor, mid) - layout.radius * 0.62f) < 1f)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,325 @@
|
||||
package org.prolibertate.games.ui.game.catan
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.prolibertate.games.game.catan.BuildCity
|
||||
import org.prolibertate.games.game.catan.BuildRoad
|
||||
import org.prolibertate.games.game.catan.BuildSettlement
|
||||
import org.prolibertate.games.game.catan.BuildingType
|
||||
import org.prolibertate.games.game.catan.CatanBoard
|
||||
import org.prolibertate.games.game.catan.CatanPhase
|
||||
import org.prolibertate.games.game.catan.CatanRules
|
||||
import org.prolibertate.games.game.catan.DevCard
|
||||
import org.prolibertate.games.game.catan.DevCardType
|
||||
import org.prolibertate.games.game.catan.Discard
|
||||
import org.prolibertate.games.game.catan.MaritimeTrade
|
||||
import org.prolibertate.games.game.catan.MoveRobber
|
||||
import org.prolibertate.games.game.catan.PlayKnight
|
||||
import org.prolibertate.games.game.catan.PlayMonopoly
|
||||
import org.prolibertate.games.game.catan.PlayYearOfPlenty
|
||||
import org.prolibertate.games.game.catan.Resource
|
||||
import org.prolibertate.games.game.catan.SetupPlace
|
||||
import org.prolibertate.games.ui.game.catan.CatanFixtures.hand
|
||||
import org.prolibertate.games.ui.game.catan.CatanFixtures.playing
|
||||
import org.prolibertate.games.ui.game.catan.CatanFixtures.start
|
||||
import org.prolibertate.games.ui.game.catan.CatanFixtures.withBuilding
|
||||
import org.prolibertate.games.ui.game.catan.CatanFixtures.withHand
|
||||
import org.prolibertate.games.ui.game.catan.CatanFixtures.withRoad
|
||||
|
||||
class CatanInteractionTest {
|
||||
|
||||
private val rich = hand(
|
||||
Resource.LUMBER to 5, Resource.BRICK to 5, Resource.WOOL to 5,
|
||||
Resource.GRAIN to 5, Resource.ORE to 5,
|
||||
)
|
||||
|
||||
// --- Setup ---------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
fun `setup first offers every settlement spot, then only the roads beside the chosen one`() {
|
||||
val state = start()
|
||||
val legal = CatanRules.legalMoves(state, 0)
|
||||
val setup = legal.filterIsInstance<SetupPlace>()
|
||||
|
||||
val first = CatanInteraction.targets(state, legal, null, null)
|
||||
assertEquals(setup.map { it.vertex }.toSet(), first.vertices)
|
||||
assertTrue(first.edges.isEmpty() && first.hexes.isEmpty())
|
||||
|
||||
val vertex = first.vertices.first()
|
||||
val second = CatanInteraction.targets(state, legal, null, vertex)
|
||||
assertTrue(second.vertices.isEmpty())
|
||||
assertEquals(setup.filter { it.vertex == vertex }.map { it.edge }.toSet(), second.edges)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a setup placement is two taps - the settlement, then its road`() {
|
||||
val state = start()
|
||||
val legal = CatanRules.legalMoves(state, 0)
|
||||
val vertex = CatanInteraction.targets(state, legal, null, null).vertices.first()
|
||||
val edge = CatanInteraction.targets(state, legal, null, vertex).edges.first()
|
||||
|
||||
assertEquals(TapOutcome.PickSetupVertex(vertex), CatanInteraction.onVertexTap(vertex, legal, null, null))
|
||||
assertEquals(TapOutcome.Submit(SetupPlace(vertex, edge)), CatanInteraction.onEdgeTap(edge, legal, null, vertex))
|
||||
// A road tapped before any settlement is chosen means nothing.
|
||||
assertEquals(TapOutcome.Ignored, CatanInteraction.onEdgeTap(edge, legal, null, null))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a road that does not touch the chosen settlement is refused`() {
|
||||
val state = start()
|
||||
val legal = CatanRules.legalMoves(state, 0)
|
||||
val vertex = CatanInteraction.targets(state, legal, null, null).vertices.first()
|
||||
val beside = CatanBoard.VERTEX_EDGES[vertex].toSet()
|
||||
val far = (0 until CatanBoard.EDGE_COUNT).first { it !in beside }
|
||||
assertEquals(TapOutcome.Ignored, CatanInteraction.onEdgeTap(far, legal, null, vertex))
|
||||
}
|
||||
|
||||
// --- Building -------------------------------------------------------------
|
||||
|
||||
/** Seat 0 owns a settlement at vertex 0 and a road two edges long leading away from it. */
|
||||
private fun established(): Triple<org.prolibertate.games.game.catan.CatanState, Int, Int> {
|
||||
val e1 = CatanBoard.VERTEX_EDGES[0][0]
|
||||
val w = CatanBoard.otherEnd(e1, 0)
|
||||
val e2 = CatanBoard.VERTEX_EDGES[w].first { it != e1 }
|
||||
val x = CatanBoard.otherEnd(e2, w)
|
||||
val state = playing {
|
||||
withHand(0, rich).withBuilding(0, BuildingType.SETTLEMENT, 0).withRoad(e1, 0).withRoad(e2, 0)
|
||||
}
|
||||
return Triple(state, x, e2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `nothing on the board is tappable until a build mode is chosen`() {
|
||||
val (state, _, _) = established()
|
||||
val legal = CatanRules.legalMoves(state, 0)
|
||||
assertTrue(CatanInteraction.targets(state, legal, null, null).isEmpty)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `build modes target exactly what the engine allows`() {
|
||||
val (state, x, _) = established()
|
||||
val legal = CatanRules.legalMoves(state, 0)
|
||||
|
||||
assertEquals(
|
||||
legal.filterIsInstance<BuildRoad>().map { it.edge }.toSet(),
|
||||
CatanInteraction.targets(state, legal, BuildKind.ROAD, null).edges,
|
||||
)
|
||||
assertTrue(x in CatanInteraction.targets(state, legal, BuildKind.SETTLEMENT, null).vertices)
|
||||
assertEquals(setOf(0), CatanInteraction.targets(state, legal, BuildKind.CITY, null).vertices)
|
||||
assertEquals(setOf(BuildKind.ROAD, BuildKind.SETTLEMENT, BuildKind.CITY), CatanInteraction.canBuild(legal))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a tap builds only when the matching mode is armed and the spot is legal`() {
|
||||
val (state, x, _) = established()
|
||||
val legal = CatanRules.legalMoves(state, 0)
|
||||
val road = legal.filterIsInstance<BuildRoad>().first().edge
|
||||
|
||||
assertEquals(TapOutcome.Submit(BuildSettlement(x)), CatanInteraction.onVertexTap(x, legal, BuildKind.SETTLEMENT, null))
|
||||
assertEquals(TapOutcome.Submit(BuildCity(0)), CatanInteraction.onVertexTap(0, legal, BuildKind.CITY, null))
|
||||
assertEquals(TapOutcome.Submit(BuildRoad(road)), CatanInteraction.onEdgeTap(road, legal, BuildKind.ROAD, null))
|
||||
|
||||
// No mode armed: nothing happens.
|
||||
assertEquals(TapOutcome.Ignored, CatanInteraction.onVertexTap(x, legal, null, null))
|
||||
assertEquals(TapOutcome.Ignored, CatanInteraction.onEdgeTap(road, legal, null, null))
|
||||
// A settlement on an occupied crossing is not offered.
|
||||
assertEquals(TapOutcome.Ignored, CatanInteraction.onVertexTap(0, legal, BuildKind.SETTLEMENT, null))
|
||||
// The wrong mode for the spot.
|
||||
assertEquals(TapOutcome.Ignored, CatanInteraction.onVertexTap(x, legal, BuildKind.CITY, null))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `without the cards, nothing can be built`() {
|
||||
val (state, _, _) = established()
|
||||
val broke = state.withHand(0, hand())
|
||||
val legal = CatanRules.legalMoves(broke, 0)
|
||||
assertTrue(CatanInteraction.canBuild(legal).isEmpty())
|
||||
assertFalse(CatanInteraction.canBuyDevCard(legal))
|
||||
assertTrue(CatanInteraction.canEndTurn(legal))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `free roads from a road building card need no build mode`() {
|
||||
val (state, _, _) = established()
|
||||
val free = state.copy(phase = CatanPhase.FREE_ROADS, pendingFreeRoads = 2)
|
||||
val legal = CatanRules.legalMoves(free, 0)
|
||||
val targets = CatanInteraction.targets(free, legal, null, null)
|
||||
assertTrue(targets.edges.isNotEmpty())
|
||||
val edge = targets.edges.first()
|
||||
assertEquals(TapOutcome.Submit(BuildRoad(edge)), CatanInteraction.onEdgeTap(edge, legal, null, null))
|
||||
assertTrue(CatanInteraction.canEndFreeRoads(legal))
|
||||
}
|
||||
|
||||
// --- The robber ---------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
fun `the robber may go to any hex but the one it is on`() {
|
||||
val state = playing { copy(phase = CatanPhase.ROBBER) }
|
||||
val legal = CatanRules.legalMoves(state, 0)
|
||||
val targets = CatanInteraction.targets(state, legal, null, null)
|
||||
assertEquals((0 until CatanBoard.HEX_COUNT).filter { it != state.robber }.toSet(), targets.hexes)
|
||||
|
||||
val hex = targets.hexes.first()
|
||||
// Nobody has built anything, so there is nobody to rob: the move goes straight through.
|
||||
assertEquals(
|
||||
TapOutcome.Submit(MoveRobber(CatanBoard.HEXES[hex], null)),
|
||||
CatanInteraction.onHexTap(hex, legal),
|
||||
)
|
||||
assertEquals(TapOutcome.Ignored, CatanInteraction.onHexTap(state.robber, legal))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a hex with two possible victims asks which`() {
|
||||
val base = playing { copy(phase = CatanPhase.ROBBER) }
|
||||
val hex = (0 until CatanBoard.HEX_COUNT).first { it != base.robber }
|
||||
val corners = CatanBoard.HEX_CORNERS[hex]
|
||||
val state = base
|
||||
.withBuilding(corners[0], BuildingType.SETTLEMENT, 1)
|
||||
.withBuilding(corners[3], BuildingType.SETTLEMENT, 2)
|
||||
.withHand(1, hand(Resource.ORE to 1))
|
||||
.withHand(2, hand(Resource.GRAIN to 1))
|
||||
val legal = CatanRules.legalMoves(state, 0)
|
||||
|
||||
val outcome = CatanInteraction.onHexTap(hex, legal)
|
||||
assertTrue(outcome is TapOutcome.PickRobberVictim)
|
||||
outcome as TapOutcome.PickRobberVictim
|
||||
assertEquals(CatanBoard.HEXES[hex], outcome.hex)
|
||||
assertEquals(setOf(1, 2), outcome.victims.toSet())
|
||||
}
|
||||
|
||||
// --- Discarding -----------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
fun `a seven costs half the hand, rounded down`() {
|
||||
assertEquals(4, CatanInteraction.discardQuota(9))
|
||||
assertEquals(4, CatanInteraction.discardQuota(8))
|
||||
assertEquals(3, CatanInteraction.discardQuota(7))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a discard is found only when it names exactly an allowed set of cards`() {
|
||||
val state = playing {
|
||||
copy(phase = CatanPhase.DISCARDING, discardSeat = 0)
|
||||
.withHand(0, hand(Resource.LUMBER to 3, Resource.BRICK to 3, Resource.WOOL to 3))
|
||||
}
|
||||
val legal = CatanRules.legalMoves(state, 0)
|
||||
assertTrue(legal.all { it is Discard })
|
||||
|
||||
val good = listOf(Resource.LUMBER, Resource.LUMBER, Resource.BRICK, Resource.WOOL)
|
||||
assertNotNull(CatanInteraction.discardMove(legal, good))
|
||||
// Order does not matter.
|
||||
assertNotNull(CatanInteraction.discardMove(legal, good.reversed()))
|
||||
// Too few, and more than are held.
|
||||
assertNull(CatanInteraction.discardMove(legal, good.take(3)))
|
||||
assertNull(CatanInteraction.discardMove(legal, List(4) { Resource.LUMBER }))
|
||||
}
|
||||
|
||||
// --- Trading ---------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
fun `bank trades list what can be given and what it can buy`() {
|
||||
val state = playing { withHand(0, hand(Resource.GRAIN to 4)) }
|
||||
val legal = CatanRules.legalMoves(state, 0)
|
||||
assertTrue(legal.any { it is MaritimeTrade })
|
||||
|
||||
val trades = CatanInteraction.bankTrades(legal)
|
||||
assertEquals(setOf(Resource.GRAIN), trades.keys)
|
||||
assertEquals(Resource.entries.filter { it != Resource.GRAIN }, trades.getValue(Resource.GRAIN))
|
||||
assertEquals(4, CatanInteraction.bankRatio(state, 0, Resource.GRAIN))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an offer is allowed only to another player, for something different, from cards held`() {
|
||||
val state = playing { withHand(0, hand(Resource.LUMBER to 2)) }
|
||||
val lumber = listOf(Resource.LUMBER)
|
||||
val grain = listOf(Resource.GRAIN)
|
||||
|
||||
assertTrue(CatanInteraction.offerAllowed(state, 0, 1, lumber, grain))
|
||||
assertFalse("to oneself", CatanInteraction.offerAllowed(state, 0, 0, lumber, grain))
|
||||
assertFalse("nothing offered", CatanInteraction.offerAllowed(state, 0, 1, emptyList(), grain))
|
||||
assertFalse("nothing asked", CatanInteraction.offerAllowed(state, 0, 1, lumber, emptyList()))
|
||||
assertFalse("like for like", CatanInteraction.offerAllowed(state, 0, 1, lumber, lumber))
|
||||
assertFalse("more than held", CatanInteraction.offerAllowed(state, 0, 1, List(3) { Resource.LUMBER }, grain))
|
||||
assertFalse("not their turn", CatanInteraction.offerAllowed(state, 1, 0, lumber, grain))
|
||||
}
|
||||
|
||||
// --- Development cards -------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
fun `a development card is playable unless it was bought this turn or is a victory point`() {
|
||||
val state = playing {
|
||||
copy(
|
||||
devHands = listOf(
|
||||
listOf(
|
||||
DevCard(1, DevCardType.KNIGHT, boughtTurn = 0),
|
||||
DevCard(2, DevCardType.VICTORY_POINT, boughtTurn = 0),
|
||||
DevCard(3, DevCardType.KNIGHT, boughtTurn = 1),
|
||||
),
|
||||
emptyList(),
|
||||
emptyList(),
|
||||
),
|
||||
)
|
||||
}
|
||||
val legal = CatanRules.legalMoves(state, 0)
|
||||
val hand = CatanInteraction.devHand(state, 0, legal)
|
||||
|
||||
// Sorted by type then id: the two knights, then the victory point.
|
||||
assertEquals(listOf(1, 3, 2), hand.map { it.card.id })
|
||||
assertEquals(listOf(true, false, false), hand.map { it.playable })
|
||||
assertEquals(listOf<Any>(PlayKnight(1)), hand.first().plays)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `monopoly offers one play per resource`() {
|
||||
val plays = Resource.entries.map { PlayMonopoly(9, it) }
|
||||
val choices = CatanInteraction.monopolyChoices(plays)
|
||||
assertEquals(Resource.entries.toSet(), choices.keys)
|
||||
assertEquals(PlayMonopoly(9, Resource.ORE), choices.getValue(Resource.ORE))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `year of plenty narrows to the cards that still complete a take`() {
|
||||
val plays = listOf(
|
||||
PlayYearOfPlenty(4, listOf(Resource.LUMBER, Resource.BRICK)),
|
||||
PlayYearOfPlenty(4, listOf(Resource.LUMBER, Resource.LUMBER)),
|
||||
PlayYearOfPlenty(4, listOf(Resource.ORE)),
|
||||
)
|
||||
assertEquals(
|
||||
setOf(Resource.LUMBER, Resource.BRICK, Resource.ORE),
|
||||
CatanInteraction.plentyAvailable(plays, emptyList()),
|
||||
)
|
||||
assertEquals(
|
||||
setOf(Resource.LUMBER, Resource.BRICK),
|
||||
CatanInteraction.plentyAvailable(plays, listOf(Resource.LUMBER)),
|
||||
)
|
||||
assertEquals(setOf(Resource.LUMBER), CatanInteraction.plentyAvailable(plays, listOf(Resource.BRICK)))
|
||||
assertTrue(CatanInteraction.plentyAvailable(plays, listOf(Resource.LUMBER, Resource.LUMBER)).isEmpty())
|
||||
|
||||
assertEquals(plays[0], CatanInteraction.plentyMove(plays, listOf(Resource.BRICK, Resource.LUMBER)))
|
||||
assertEquals(plays[2], CatanInteraction.plentyMove(plays, listOf(Resource.ORE)))
|
||||
assertNull(CatanInteraction.plentyMove(plays, listOf(Resource.GRAIN)))
|
||||
}
|
||||
|
||||
// --- Trade replies ------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
fun `the addressee of an offer is given accept and decline`() {
|
||||
val base = playing {
|
||||
withHand(0, hand(Resource.LUMBER to 1)).withHand(1, hand(Resource.GRAIN to 1))
|
||||
}
|
||||
val offer = org.prolibertate.games.game.catan.TradeOffer(
|
||||
by = 0, to = 1, give = listOf(Resource.LUMBER), get = listOf(Resource.GRAIN),
|
||||
)
|
||||
val pending = base.copy(phase = CatanPhase.TRADE_PENDING, pendingTrade = offer)
|
||||
val legal = CatanRules.legalMoves(pending, 1)
|
||||
assertEquals(true to true, CatanInteraction.tradeResponses(legal))
|
||||
|
||||
// If the addressee no longer holds what was asked, only decline remains.
|
||||
val short = pending.withHand(1, hand())
|
||||
assertEquals(false to true, CatanInteraction.tradeResponses(CatanRules.legalMoves(short, 1)))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package org.prolibertate.games.ui.game.catan
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.prolibertate.games.game.catan.CatanPhase
|
||||
import org.prolibertate.games.game.catan.Port
|
||||
import org.prolibertate.games.game.catan.Resource
|
||||
import org.prolibertate.games.ui.game.catan.CatanFixtures.hand
|
||||
import org.prolibertate.games.ui.game.catan.CatanFixtures.playing
|
||||
import org.prolibertate.games.ui.game.catan.CatanFixtures.start
|
||||
import org.prolibertate.games.ui.game.catan.CatanFixtures.withHand
|
||||
|
||||
class CatanTextTest {
|
||||
|
||||
private val names = listOf("Ann", "Bo", "Cy")
|
||||
|
||||
@Test
|
||||
fun `seats are named, with You for the local player and a number when a name is unknown`() {
|
||||
val seats = CatanSeats(me = 1, names = names)
|
||||
assertEquals("You", seats.label(1))
|
||||
assertEquals("Ann", seats.label(0))
|
||||
assertEquals("you", seats.lower(1))
|
||||
assertEquals("Seat 4", seats.label(3))
|
||||
assertEquals("Seat 1", CatanSeats(me = 1).label(0))
|
||||
assertEquals("Seat 1", CatanSeats(me = 1, names = listOf(" ")).label(0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `setup prompts say what to place next`() {
|
||||
val state = start()
|
||||
val mine = CatanSeats(0, names)
|
||||
assertTrue(CatanText.prompt(state, mine, setupVertexChosen = false, building = null).contains("Place a settlement"))
|
||||
assertTrue(CatanText.prompt(state, mine, setupVertexChosen = true, building = null).contains("road"))
|
||||
assertTrue(CatanText.prompt(state, CatanSeats(1, names), false, null).startsWith("Ann is placing"))
|
||||
val second = state.copy(phase = CatanPhase.SETUP_SECOND, setupIndex = 3)
|
||||
assertTrue(CatanText.prompt(second, CatanSeats(2, names), false, null).contains("second settlement"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the turn prompts follow the phase`() {
|
||||
val roll = playing().copy(phase = CatanPhase.AWAIT_ROLL)
|
||||
assertTrue(CatanText.prompt(roll, CatanSeats(0, names), false, null).contains("Roll the dice"))
|
||||
assertEquals("Ann is rolling…", CatanText.prompt(roll, CatanSeats(1, names), false, null))
|
||||
|
||||
val turn = playing()
|
||||
assertTrue(CatanText.prompt(turn, CatanSeats(0, names), false, null).contains("Your turn"))
|
||||
assertEquals("Tap an edge to build a road.", CatanText.prompt(turn, CatanSeats(0, names), false, BuildKind.ROAD))
|
||||
assertEquals("Bo is playing…", CatanText.prompt(playing(turn = 1), CatanSeats(0, names), false, null))
|
||||
|
||||
val robber = playing().copy(phase = CatanPhase.ROBBER)
|
||||
assertTrue(CatanText.prompt(robber, CatanSeats(0, names), false, null).contains("robber"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the discard prompt names the quota`() {
|
||||
val state = playing {
|
||||
copy(phase = CatanPhase.DISCARDING, discardSeat = 0)
|
||||
.withHand(0, hand(Resource.LUMBER to 9))
|
||||
}
|
||||
assertTrue(CatanText.prompt(state, CatanSeats(0, names), false, null).contains("discard 4"))
|
||||
assertTrue(CatanText.prompt(state, CatanSeats(1, names), false, null).contains("Ann must discard half"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `road building counts down in the singular and the plural`() {
|
||||
val two = playing().copy(phase = CatanPhase.FREE_ROADS, pendingFreeRoads = 2)
|
||||
val one = two.copy(pendingFreeRoads = 1)
|
||||
assertTrue(CatanText.prompt(two, CatanSeats(0, names), false, null).endsWith("2 more roads."))
|
||||
assertTrue(CatanText.prompt(one, CatanSeats(0, names), false, null).endsWith("1 more road."))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the end of the game names the winner`() {
|
||||
val over = playing().copy(phase = CatanPhase.GAME_OVER, winner = 1)
|
||||
assertEquals("Bo won the game.", CatanText.prompt(over, CatanSeats(0, names), false, null))
|
||||
assertEquals("You won!", CatanText.winnerHeadline(over, CatanSeats(1, names)))
|
||||
assertEquals("Bo won", CatanText.winnerHeadline(over, CatanSeats(0, names)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `cards read as a sorted tally`() {
|
||||
assertEquals(
|
||||
"2 Lumber + 1 Grain",
|
||||
CatanText.cards(listOf(Resource.GRAIN, Resource.LUMBER, Resource.LUMBER)),
|
||||
)
|
||||
assertEquals("nothing", CatanText.cards(emptyList()))
|
||||
assertEquals(
|
||||
"Ann offers 1 Lumber to you for 2 Ore",
|
||||
CatanText.tradeSummary("Ann", "you", listOf(Resource.LUMBER), listOf(Resource.ORE, Resource.ORE)),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `costs and harbours read as the rules state them`() {
|
||||
assertEquals("1 lumber + 1 brick", CatanText.ROAD_COST)
|
||||
assertEquals("2 grain + 3 ore", CatanText.CITY_COST)
|
||||
assertEquals("2:1 ore", CatanText.portName(Port(edge = 0, resource = Resource.ORE, ratio = 2)))
|
||||
assertEquals("3:1 any", CatanText.portName(Port(edge = 0, resource = null, ratio = 3)))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user