From 1fde9ed37c43f562ec531fa90c21803e2052e3ef Mon Sep 17 00:00:00 2001 From: Mike Isom <103458985+MikeIsom@users.noreply.github.com> Date: Tue, 27 Oct 2026 16:33:50 -0600 Subject: [PATCH] Add an optional two-tiles-per-turn variant MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With twoTiles on, hands hold 8 tiles and each turn places two, each fully resolved (mergers and bank tiles included) before the next. A turn founds at most one chain and never both founds and merges; dead tiles are replaced at the start of the turn. Buying, announcing and auto-play follow rules.md §4.1. Co-Authored-By: Claude Opus 5.5 --- docs/admin/gameinfo.json | 6 + docs/admin/rules.md | 15 ++ packages/engine/index.ts | 1 + packages/engine/src/bank.ts | 7 +- packages/engine/src/board.ts | 6 +- packages/engine/src/config.ts | 1 + packages/engine/src/moves.ts | 13 +- packages/engine/src/options.ts | 13 +- packages/engine/src/position.ts | 7 +- packages/engine/src/setup.ts | 6 +- packages/engine/src/sim.spec.ts | 27 ++- packages/engine/src/simple.ts | 16 +- packages/engine/src/turn.ts | 49 +++- packages/engine/src/two-tiles.spec.ts | 260 ++++++++++++++++++++++ packages/engine/src/two-tiles.ts | 41 ++++ packages/viewer/src/dev.ts | 2 +- packages/viewer/src/lib/Board.svelte | 37 +-- packages/viewer/src/lib/Hand.svelte | 45 +++- packages/viewer/src/lib/decisions.spec.ts | 44 +++- packages/viewer/src/lib/decisions.ts | 15 ++ packages/viewer/src/lib/format.ts | 3 +- packages/viewer/src/lib/locales/da.ts | 4 + packages/viewer/src/lib/locales/de.ts | 4 + packages/viewer/src/lib/locales/el.ts | 4 + packages/viewer/src/lib/locales/en.ts | 4 + packages/viewer/src/lib/locales/fa.ts | 4 + packages/viewer/src/lib/locales/fr.ts | 4 + packages/viewer/src/lib/locales/hi.ts | 4 + packages/viewer/src/lib/locales/it.ts | 4 + packages/viewer/src/lib/locales/ko.ts | 4 + packages/viewer/src/lib/locales/nl.ts | 4 + packages/viewer/src/lib/locales/pl.ts | 4 + packages/viewer/src/lib/locales/pt-BR.ts | 4 + packages/viewer/src/lib/locales/ro.ts | 4 + packages/viewer/src/lib/locales/ru.ts | 4 + packages/viewer/src/lib/locales/vi.ts | 4 + packages/viewer/src/lib/locales/zh-CN.ts | 4 + packages/viewer/src/lib/locales/zh-TW.ts | 4 + rules.md | 22 ++ 39 files changed, 633 insertions(+), 71 deletions(-) create mode 100644 packages/engine/src/two-tiles.spec.ts create mode 100644 packages/engine/src/two-tiles.ts diff --git a/docs/admin/gameinfo.json b/docs/admin/gameinfo.json index 1cd15ae..a32bc06 100644 --- a/docs/admin/gameinfo.json +++ b/docs/admin/gameinfo.json @@ -121,6 +121,12 @@ "label": "Unlimited" } ] + }, + { + "name": "twoTiles", + "label": "Two tiles per turn (hands of 8, optional variant)", + "type": "checkbox", + "default": false } ], "settings": [], diff --git a/docs/admin/rules.md b/docs/admin/rules.md index 59ac4db..fea53fc 100644 --- a/docs/admin/rules.md +++ b/docs/admin/rules.md @@ -37,6 +37,20 @@ A chain of 11 or more tiles is **safe**: it can never be taken over. - A tile that would found an 8th chain can't be played while all 7 chains are on the board. You keep it; it becomes playable when a chain disappears. - If none of your tiles can be played, you skip placing and may still buy stock. +### Two tiles per turn + +If the game uses **Two tiles per turn**, each player holds 8 tiles instead of 6 and places two tiles every turn. + +At the start of your turn, swap out any dead tiles. Then place your first tile and settle anything it causes, including a full merger with everyone's sell, trade or keep decisions, before placing your second. After both tiles, buy up to 3 shares at the prices on the board, then draw back up to 8. + +You may found only one chain per turn, and you can't found a chain and cause a merger in the same turn, in either order. You can cause two mergers, one with each tile. Example: your first tile merges Luxor into Tower, so your second tile can merge again or grow a chain, but it can't found a new one. + +You must play a second tile if any tile in your hand is legal after the first. If none is, go straight to buying. A tile that becomes dead during your turn, for example because your first tile made a chain safe, can't be your second tile; it's swapped out at the start of your next turn. + +You can announce the end of the game before your first tile, between your two tiles, or while buying, as long as an end condition holds at that moment. + +In a two-player game, the bank's tiles are placed right after the merger that drew them, so between your two tiles if your first tile caused it. Mergers caused by the bank's tiles don't count toward your two mergers or the founding rule. If a player leaves, the game places their first two legal tiles for them. + ## Mergers The **largest** chain survives and takes over the others. If chains tie for largest, the player who placed the tile chooses the survivor. Sizes are counted before the merging tile is placed. @@ -116,6 +130,7 @@ Your tiles are private. Unless the game was created with **public holdings**, ea ## Game options +- **Two tiles per turn:** hands of 8 tiles and two tiles placed every turn (see above). - **Stock swap:** optional 1:1 exchanges with the bank before buying; Off / 3 / 5 / 7 / Unlimited shares per turn (see above). - **Public holdings:** everyone's cash and shares are visible. - **Team play** (4 players only): seats 1 and 3 play against seats 2 and 4; each team's money is added together at the end. diff --git a/packages/engine/index.ts b/packages/engine/index.ts index bac41dd..5d96a39 100644 --- a/packages/engine/index.ts +++ b/packages/engine/index.ts @@ -18,3 +18,4 @@ export * from "./src/view.js"; export * from "./src/replay.js"; export * from "./src/position.js"; export * from "./src/simple.js"; +export { blockedThisTurn, secondPlacementDue, turnPlacements } from "./src/two-tiles.js"; diff --git a/packages/engine/src/bank.ts b/packages/engine/src/bank.ts index dee6a58..f8f7d08 100644 --- a/packages/engine/src/bank.ts +++ b/packages/engine/src/bank.ts @@ -1,7 +1,7 @@ import { connectedLoose, placementEffect, tileLegality, chainSize } from "./board.js"; import { startMerger } from "./merger.js"; import { tileColumn } from "./tiles.js"; -import { drawTile, enterBuyPhase } from "./turn.js"; +import { afterPlacement, drawTile } from "./turn.js"; import type { BankHolding, GameState } from "./types.js"; /** rules.md §12/§15: the bank is a third shareholder with exactly 2 players and twoPlayerMode "bank". */ @@ -31,14 +31,15 @@ export function bankHolding(state: GameState, fate: "wait" | "setAside"): Omit tileLegality(state.board, tile) === "playable"); + return (state.seats[seat]?.hand ?? []).filter( + (tile) => tileLegality(state.board, tile) === "playable" && !blockedThisTurn(state, tile) + ); } /** The tile plus every loose tile connected to it through other loose tiles. */ diff --git a/packages/engine/src/config.ts b/packages/engine/src/config.ts index c980e20..2beb1a6 100644 --- a/packages/engine/src/config.ts +++ b/packages/engine/src/config.ts @@ -21,6 +21,7 @@ export const BOARD_ROWS = 9; export const TILE_COUNT = BOARD_COLUMNS * BOARD_ROWS; export const STARTING_CASH = 6000; export const HAND_SIZE = 6; +export const TWO_TILE_HAND_SIZE = 8; export const SHARES_PER_CHAIN = 25; export const SAFE_SIZE = 11; export const END_SIZE = 41; diff --git a/packages/engine/src/moves.ts b/packages/engine/src/moves.ts index 3fb4f5d..ec9e3fd 100644 --- a/packages/engine/src/moves.ts +++ b/packages/engine/src/moves.ts @@ -15,7 +15,8 @@ import { maxStockSwap, swapStock } from "./stock-swap.js"; import { sharePrice } from "./prices.js"; import { canAnnounce, endGame } from "./scoring.js"; import { parseTile, tileLabel, type Tile } from "./tiles.js"; -import { endTurn, enterBuyPhase } from "./turn.js"; +import { afterPlacement, endTurn, enterBuyPhase } from "./turn.js"; +import { blockedThisTurn } from "./two-tiles.js"; import type { GameState, Move, Shares } from "./types.js"; export function currentSeat(state: GameState): number | undefined { @@ -45,7 +46,8 @@ export function sanitizeMove(state: GameState, raw: unknown, seat: number): Move throw new Error("A move must be an object"); } if (raw.type === "announce") { - // rules.md §4/§11.1: before placing or during the buy step, checked at this moment. + // rules.md §4/§11.1: before placing (with twoTiles, also before the second tile) or + // during the buy step, checked at this moment. if (state.phase !== "place" && state.phase !== "buy") { throw new Error("The end can only be announced before placing a tile or while buying"); } @@ -85,6 +87,9 @@ export function sanitizeMove(state: GameState, raw: unknown, seat: number): Move if (tileLegality(state.board, tile) !== "playable") { throw new Error(`Tile ${tileLabel(tile)} cannot be played now`); } + if (blockedThisTurn(state, tile)) { + throw new Error("A turn cannot found two chains, or both found a chain and cause a merger"); + } return { type: "place", tile: tileLabel(tile) }; } case "found": { @@ -251,7 +256,7 @@ function perform(state: GameState, move: Move, seat: number, auto: boolean): voi break; case "found": foundChain(state, move.chain); - enterBuyPhase(state); + afterPlacement(state); break; case "survivor": setSurvivor(state, move.chain); @@ -297,7 +302,7 @@ function placeTile(state: GameState, tile: Tile): void { } foundChain(state, available[0]!); } - enterBuyPhase(state); + afterPlacement(state); } /** rules.md §6. */ diff --git a/packages/engine/src/options.ts b/packages/engine/src/options.ts index b1f96ca..5ddb351 100644 --- a/packages/engine/src/options.ts +++ b/packages/engine/src/options.ts @@ -1,4 +1,4 @@ -import { END_SIZE } from "./config.js"; +import { END_SIZE, HAND_SIZE, TWO_TILE_HAND_SIZE } from "./config.js"; /** rules.md §15, after normalisation against the player count. */ export interface GameOptions { @@ -9,6 +9,8 @@ export interface GameOptions { endSize: number; /** Omitted when off, preserving the init log of games created before this variant. */ stockSwap?: 3 | 5 | 7 | "unlimited"; + /** Omitted when off, like stockSwap. */ + twoTiles?: true; } export interface IgnoredOption { @@ -80,9 +82,18 @@ export function normalizeOptions( } } + if (raw.twoTiles === true) { + options.twoTiles = true; + } + return { options, ignored }; } +/** rules.md §15 twoTiles: hands hold 8 tiles instead of 6. */ +export function handSize(options: GameOptions): number { + return options.twoTiles ? TWO_TILE_HAND_SIZE : HAND_SIZE; +} + /** Internal seats per real player: fourHands gives each of the 2 players two seats. */ export function seatOwners(players: number, options: GameOptions): number[] { if (players === 2 && options.twoPlayerMode === "fourHands") { diff --git a/packages/engine/src/position.ts b/packages/engine/src/position.ts index 7bdbde0..af69cce 100644 --- a/packages/engine/src/position.ts +++ b/packages/engine/src/position.ts @@ -1,5 +1,6 @@ import { connectedLoose, placementEffect } from "./board.js"; -import { CHAIN_IDS, HAND_SIZE, SHARES_PER_CHAIN, STARTING_CASH, type ChainId } from "./config.js"; +import { CHAIN_IDS, SHARES_PER_CHAIN, STARTING_CASH, type ChainId } from "./config.js"; +import { handSize } from "./options.js"; import { parseTile, type Tile } from "./tiles.js"; import type { Cell, GameState, Shares } from "./types.js"; @@ -24,6 +25,7 @@ export function positionAt(state: GameState, to: number): PublicPosition { const bank = sharesOf(SHARES_PER_CHAIN); let seats: PublicPosition["seats"] = []; let lastTile: Tile | undefined; + let hand = 0; let merger: { tile: Tile; chains: ChainId[]; survivor?: ChainId } | undefined; const place = (tile: Tile) => { @@ -41,6 +43,7 @@ export function positionAt(state: GameState, to: number): PublicPosition { switch (entry.type) { case "init": seats = Array.from({ length: entry.seats }, () => ({ cash: STARTING_CASH, shares: sharesOf(0), tiles: 0 })); + hand = handSize(entry.options); break; case "turnOrder": for (const tile of entry.tiles) { @@ -48,7 +51,7 @@ export function positionAt(state: GameState, to: number): PublicPosition { } // rules.md §3.4: every seat then draws a full hand, which is not logged as a draw. for (const seat of seats) { - seat.tiles = HAND_SIZE; + seat.tiles = hand; } break; case "move": diff --git a/packages/engine/src/setup.ts b/packages/engine/src/setup.ts index 2d5f682..49951ad 100644 --- a/packages/engine/src/setup.ts +++ b/packages/engine/src/setup.ts @@ -1,5 +1,5 @@ -import { CHAIN_IDS, HAND_SIZE, MAX_PLAYERS, MIN_PLAYERS, SHARES_PER_CHAIN, STARTING_CASH } from "./config.js"; -import { normalizeOptions, seatOwners } from "./options.js"; +import { CHAIN_IDS, MAX_PLAYERS, MIN_PLAYERS, SHARES_PER_CHAIN, STARTING_CASH } from "./config.js"; +import { handSize, normalizeOptions, seatOwners } from "./options.js"; import { shuffle } from "./prng.js"; import { allTiles } from "./tiles.js"; import { drawTile, startTurn } from "./turn.js"; @@ -60,7 +60,7 @@ export function initGame(players: number, rawOptions: Record, s state.log.push({ type: "turnOrder", tiles: orderTiles, first }); for (const seat of state.seats) { - for (let i = 0; i < HAND_SIZE; i++) { + for (let i = 0; i < handSize(options); i++) { seat.hand.push(drawTile(state)!); } } diff --git a/packages/engine/src/sim.spec.ts b/packages/engine/src/sim.spec.ts index 0e75ec2..488ffd1 100644 --- a/packages/engine/src/sim.spec.ts +++ b/packages/engine/src/sim.spec.ts @@ -22,17 +22,20 @@ function lcg(seed: number): () => number { function optionCombinations(players: number): Record[] { const combos: Record[] = []; for (const publicHoldings of [false, true]) { - for (const autoEnd of [false, true]) { - for (const endSize of ["41", "31"]) { - for (const twoPlayerMode of players === 2 ? ["bank", "fourHands"] : ["bank"]) { - for (const teamPlay of players === 4 ? [false, true] : [false]) { - combos.push({ - ...(publicHoldings ? { publicHoldings } : {}), - ...(autoEnd ? { autoEnd } : {}), - ...(teamPlay ? { teamPlay } : {}), - twoPlayerMode, - endSize, - }); + for (const twoTiles of [false, true]) { + for (const autoEnd of [false, true]) { + for (const endSize of ["41", "31"]) { + for (const twoPlayerMode of players === 2 ? ["bank", "fourHands"] : ["bank"]) { + for (const teamPlay of players === 4 ? [false, true] : [false]) { + combos.push({ + ...(publicHoldings ? { publicHoldings } : {}), + ...(autoEnd ? { autoEnd } : {}), + ...(twoTiles ? { twoTiles } : {}), + ...(teamPlay ? { teamPlay } : {}), + twoPlayerMode, + endSize, + }); + } } } } @@ -133,6 +136,6 @@ for (let players = 2; players <= 6; players++) { test("§15 the simulation covers every valid option combination", () => { assert.deepEqual( [2, 3, 4, 5, 6].map((players) => optionCombinations(players).length), - [16, 8, 16, 8, 8] + [32, 16, 32, 16, 16] ); }); diff --git a/packages/engine/src/simple.ts b/packages/engine/src/simple.ts index 8c24050..8345c07 100644 --- a/packages/engine/src/simple.ts +++ b/packages/engine/src/simple.ts @@ -52,7 +52,7 @@ export function simpleLineAt(state: GameState, index: number): string { /** * The turn that ended just before the "turn" entry at `index`, most important first: the - * placement (or the founding or merger it caused), then purchases, then an announcement. + * placements (or the foundings or mergers they caused), then purchases, then an announcement. * For example "Ann merged Luxor into Tower, bought 2 Tower". */ export function turnSummary(state: GameState, index: number): string { @@ -64,18 +64,20 @@ export function turnSummary(state: GameState, index: number): string { return "The game started"; } const seat = (state.log[start] as Extract).seat; - let placement = "placed no tile"; + // One line per placed tile (two with twoTiles), each replaced by what it caused. + const placements: string[] = []; let bankMerger = false; const parts: string[] = []; for (const entry of state.log.slice(start + 1, index)) { if (entry.type === "move" && entry.move.type === "place") { - placement = `placed ${entry.move.tile}`; + placements.push(`placed ${entry.move.tile}`); } else if (entry.type === "founded") { - placement = `founded ${chainName(entry.chain)}`; + placements[placements.length - 1] = `founded ${chainName(entry.chain)}`; } else if (entry.type === "merger") { bankMerger = entry.byBank === true; - } else if (entry.type === "mergerPlan" && !bankMerger && !placement.startsWith("merged")) { - placement = `merged ${list(entry.defunct.map(chainName))} into ${chainName(entry.survivor)}`; + } else if (entry.type === "mergerPlan" && !bankMerger && !placements.at(-1)!.startsWith("merged")) { + placements[placements.length - 1] = + `merged ${list(entry.defunct.map(chainName))} into ${chainName(entry.survivor)}`; } else if (entry.type === "swapped") { parts.push(`swapped ${entry.count} ${chainName(entry.from)} for ${entry.count} ${chainName(entry.to)}`); } else if (entry.type === "bought" && Object.keys(entry.shares).length > 0) { @@ -86,7 +88,7 @@ export function turnSummary(state: GameState, index: number): string { parts.push("left the game"); } } - return `${seatName(state, seat)} ${[placement, ...parts].join(", ")}`; + return `${seatName(state, seat)} ${[...(placements.length > 0 ? placements : ["placed no tile"]), ...parts].join(", ")}`; } export function simpleLine(state: GameState, entry: LogEntry): string { diff --git a/packages/engine/src/turn.ts b/packages/engine/src/turn.ts index 2c3c8b9..edb32e0 100644 --- a/packages/engine/src/turn.ts +++ b/packages/engine/src/turn.ts @@ -1,8 +1,10 @@ import { activeChains, chainSize, playableTiles, tileLegality } from "./board.js"; -import { AUTO_TURN_LIMIT, HAND_SIZE } from "./config.js"; +import { AUTO_TURN_LIMIT } from "./config.js"; +import { handSize } from "./options.js"; import { sharePrice } from "./prices.js"; import { canAnnounce, endConditionMet, endGame } from "./scoring.js"; import type { Tile } from "./tiles.js"; +import { secondPlacementDue } from "./two-tiles.js"; import type { GameState } from "./types.js"; export function drawTile(state: GameState): Tile | undefined { @@ -40,6 +42,10 @@ export function startTurn(state: GameState): void { state.turn += 1; state.placedThisTurn = false; state.log.push({ type: "turn", seat: state.activeSeat }); + if (state.options.twoTiles) { + // rules.md §15 twoTiles: dead tiles are replaced at the start of the turn instead. + replaceDeadTiles(state); + } if (playableTiles(state, state.activeSeat).length > 0 || canAnnounce(state)) { state.phase = "place"; return; @@ -55,7 +61,18 @@ export function startTurn(state: GameState): void { } } -/** Called once placement (and any founding or merger) is resolved. */ +/** + * Called once a placement and everything it caused (a founding, a merger, bank tiles) is + * resolved. With twoTiles the seat then places its second tile, if any is playable. + */ +export function afterPlacement(state: GameState): void { + if (secondPlacementDue(state) && playableTiles(state, state.activeSeat).length > 0) { + state.phase = "place"; + return; + } + enterBuyPhase(state); +} + export function enterBuyPhase(state: GameState): void { if (buyStepAvailable(state) || canAnnounce(state)) { state.phase = "buy"; @@ -70,12 +87,8 @@ export function endTurn(state: GameState): void { } } -/** - * rules.md §4.4-4.5: replace permanently unplayable tiles and refill the hand. Then either end - * the game (an announcement this turn, or the §11.1 stall rule) or pass the turn on. - * Returns whether the game ended. - */ -function finishTurn(state: GameState): boolean { +/** rules.md §5.3: discard the active seat's permanently unplayable tiles and refill the hand. */ +function replaceDeadTiles(state: GameState): void { const seat = state.seats[state.activeSeat]!; const dead = seat.hand.filter((tile) => tileLegality(state.board, tile) === "permanentlyUnplayable"); if (dead.length > 0) { @@ -83,8 +96,13 @@ function finishTurn(state: GameState): boolean { state.removed.push(...dead); state.log.push({ type: "discarded", seat: state.activeSeat, tiles: dead }); } + refillHand(state); +} + +function refillHand(state: GameState): void { + const seat = state.seats[state.activeSeat]!; let drawn = 0; - while (seat.hand.length < HAND_SIZE) { + while (seat.hand.length < handSize(state.options)) { const tile = drawTile(state); if (tile === undefined) { break; @@ -95,6 +113,19 @@ function finishTurn(state: GameState): boolean { if (drawn > 0) { state.log.push({ type: "drew", seat: state.activeSeat, count: drawn }); } +} + +/** + * rules.md §4.4-4.5: replace permanently unplayable tiles (with twoTiles, only refill) and + * draw. Then either end the game (an announcement this turn, or the §11.1 stall rule) or pass + * the turn on. Returns whether the game ended. + */ +function finishTurn(state: GameState): boolean { + if (state.options.twoTiles) { + refillHand(state); + } else { + replaceDeadTiles(state); + } state.turnsWithoutPlacement = state.placedThisTurn ? 0 : state.turnsWithoutPlacement + 1; if (state.endAnnounced !== null) { endGame(state, "announced"); diff --git a/packages/engine/src/two-tiles.spec.ts b/packages/engine/src/two-tiles.spec.ts new file mode 100644 index 0000000..ed8478b --- /dev/null +++ b/packages/engine/src/two-tiles.spec.ts @@ -0,0 +1,260 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import * as wrapper from "../wrapper.js"; +import { legalMoves } from "./legal.js"; +import { applyMove, currentSeat, dropPlayerSeats } from "./moves.js"; +import { normalizeOptions } from "./options.js"; +import { positionAt } from "./position.js"; +import { initGame } from "./setup.js"; +import { turnSummary } from "./simple.js"; +import { chainAt, checkInvariants, row, scenario, t } from "./test/helpers.js"; +import type { GameState, LogEntry, Move } from "./types.js"; + +const TWO_TILES = { twoTiles: true }; + +function play(state: GameState, seat: number, move: Move): GameState { + applyMove(state, move, seat); + checkInvariants(state); + return state; +} + +const place = (tile: string): Move => ({ type: "place", tile }); +const buy: Move = { type: "buy", shares: {} }; + +function placeable(state: GameState): string[] { + return legalMoves(state).flatMap((move) => (move.type === "place" ? [move.tile] : [])); +} + +function entries(state: GameState, type: T): Extract[] { + return state.log.filter((e): e is Extract => e.type === type); +} + +test("§15 twoTiles defaults off and leaves the init log of standard games unchanged", () => { + assert.equal("twoTiles" in normalizeOptions(3, {}).options, false); + for (const twoTiles of [false, "true", 1, null]) { + const { options, ignored } = normalizeOptions(3, { twoTiles }); + assert.equal("twoTiles" in options, false); + assert.deepEqual(ignored, []); + } + assert.equal(normalizeOptions(3, TWO_TILES).options.twoTiles, true); + const standard = initGame(3, {}, "standard"); + assert.equal("twoTiles" in (standard.log[0] as Extract).options, false); + assert.ok(standard.seats.every((seat) => seat.hand.length === 6)); +}); + +test("§15 twoTiles: hands hold 8 tiles, both tiles are placed before one buy step, then the hand refills", () => { + for (const players of [2, 3, 4, 5, 6]) { + const state = initGame(players, TWO_TILES, `eight-${players}`); + assert.ok(state.seats.every((seat) => seat.hand.length === 8)); + assert.ok(positionAt(state, state.log.length).seats.every((seat) => seat.tiles === 8)); + } + const state = scenario({ + board: chainAt("tower", ["1A", "2A"]), + hands: [["5E", "9I", "12A", "12C", "12E", "12G", "3I", "1I"], ["10C"], ["10E"]], + poolTop: ["7G", "7I"], + poolOnly: true, + options: TWO_TILES, + }); + play(state, 0, place("5E")); + assert.equal(state.phase, "place", "the second tile comes before buying"); + assert.deepEqual(placeable(state), ["9I", "12A", "12C", "12E", "12G", "3I", "1I"]); + play(state, 0, place("9I")); + assert.equal(state.phase, "buy"); + assert.equal(currentSeat(state), 0); + play(state, 0, { type: "buy", shares: { tower: 3 } }); + assert.deepEqual(state.seats[0]!.hand.slice(-2), [t("7G"), t("7I")]); + assert.equal(state.seats[0]!.hand.length, 8, "draws back up to 8"); + assert.equal(state.activeSeat, 1); + assert.equal(turnSummary(state, state.log.length - 1), "Player 1 placed 5E, placed 9I, bought 3 Tower"); +}); + +test("§15 twoTiles: the second tile is skipped when none is playable", () => { + // 2A founds a chain with 1A; 2C would found a second one, so it cannot follow. + const state = scenario({ + board: { "1A": "loose", "1C": "loose" }, + hands: [["2A", "2C"], ["12C"], ["12E"]], + poolOnly: true, + options: TWO_TILES, + }); + play(state, 0, place("2A")); + play(state, 0, { type: "found", chain: "tower" }); + assert.equal(state.phase, "buy"); + assert.deepEqual(placeable(state), []); +}); + +test("§15 twoTiles: after founding, a tile that would found or merge is blocked; growing is allowed", () => { + const state = scenario({ + board: { ...chainAt("tower", ["1E", "2E"]), ...chainAt("luxor", ["4E", "5E"]), "8A": "loose", "1A": "loose" }, + hands: [["9A", "3E", "6E", "2A"], ["12C"], ["12E"]], + poolOnly: true, + options: TWO_TILES, + }); + assert.deepEqual(placeable(state), ["9A", "3E", "6E", "2A"]); + play(state, 0, place("9A")); + play(state, 0, { type: "found", chain: "american" }); + assert.deepEqual(placeable(state), ["6E"]); + for (const tile of ["3E", "2A"]) { + assert.throws(() => applyMove(state, place(tile), 0), /cannot found two chains/); + } + play(state, 0, place("6E")); + assert.equal(state.phase, "buy"); +}); + +test("§15 twoTiles: a turn may cause two mergers, but not a merger and a founding", () => { + const state = scenario({ + board: { + ...chainAt("tower", ["1A", "2A"]), + ...chainAt("luxor", ["4A", "5A"]), + ...chainAt("american", ["1E", "2E"]), + ...chainAt("festival", ["4E", "5E"]), + "8G": "loose", + }, + hands: [["3A", "3E", "9G"], ["12C"], ["12E"]], + poolOnly: true, + options: TWO_TILES, + }); + play(state, 0, place("3A")); + play(state, 0, { type: "survivor", chain: "tower" }); + assert.equal(state.phase, "place"); + assert.deepEqual(placeable(state), ["3E"], "9G would found a chain after a merger"); + play(state, 0, place("3E")); + play(state, 0, { type: "survivor", chain: "american" }); + assert.equal(state.phase, "buy"); + play(state, 0, buy); + assert.equal( + turnSummary(state, state.log.length - 1), + "Player 1 merged Luxor into Tower, merged Festival into American" + ); +}); + +test("§15 twoTiles: a founding first rules out a merger with the second tile", () => { + const state = scenario({ + board: { ...chainAt("tower", ["1A", "2A"]), ...chainAt("luxor", ["4A", "5A"]), "8G": "loose" }, + hands: [["9G", "3A"], ["12C"], ["12E"]], + poolOnly: true, + options: TWO_TILES, + }); + play(state, 0, place("9G")); + play(state, 0, { type: "found", chain: "american" }); + assert.deepEqual(placeable(state), [], "3A would merge after a founding"); + assert.equal(state.phase, "buy"); +}); + +test("§15 twoTiles: a tile made dead by the first tile cannot be the second and is replaced next turn", () => { + // 11A makes Tower safe, so 2B would then merge two safe chains. + const state = scenario({ + board: { + ...chainAt("tower", row("A", 1, 10)), + ...chainAt("continental", row("C", 1, 11)), + ...chainAt("luxor", ["1G", "2G"]), + }, + hands: [["11A", "2B"], ["12E"], ["12G"]], + poolOnly: true, + options: TWO_TILES, + }); + assert.deepEqual(placeable(state), ["11A", "2B"]); + play(state, 0, place("11A")); + assert.equal(state.phase, "buy"); + play(state, 0, buy); + assert.ok(state.seats[0]!.hand.includes(t("2B")), "dead tiles stay in hand at the end of the turn"); + assert.equal(entries(state, "discarded").length, 0); + for (const seat of [1, 2]) { + play( + state, + seat, + legalMoves(state).find((move) => move.type === "place")! + ); + play(state, seat, buy); + } + assert.equal(state.activeSeat, 0); + const turn = state.log.map((entry) => entry.type).lastIndexOf("turn"); + assert.deepEqual(state.log.slice(turn, turn + 2), [ + { type: "turn", seat: 0 }, + { type: "discarded", seat: 0, tiles: [t("2B")] }, + ]); + assert.ok(!state.seats[0]!.hand.includes(t("2B"))); +}); + +test("§15 twoTiles: the end may be announced between the two tiles once a condition holds", () => { + const state = scenario({ + board: chainAt("tower", row("A", 1, 10)), + hands: [["11A", "5E"], ["12E"], ["12G"]], + poolOnly: true, + options: TWO_TILES, + }); + assert.equal( + legalMoves(state).some((move) => move.type === "announce"), + false + ); + play(state, 0, place("11A")); + assert.equal(state.phase, "place"); + play(state, 0, { type: "announce" }); + play(state, 0, place("5E")); + play(state, 0, buy); + assert.equal(state.ended, true); + assert.deepEqual(entries(state, "gameEnd"), [{ type: "gameEnd", reason: "announced" }]); +}); + +test("§15 twoTiles with §12: bank tiles are placed between the two tiles", () => { + const state = scenario({ + seats: 2, + board: { ...chainAt("tower", ["1A", "2A", "3A"]), ...chainAt("luxor", ["5A", "6A"]) }, + hands: [["4A", "9I", "9G", "12A", "12C", "12E", "12G", "12I"], ["1I"]], + shares: [{}, { luxor: 3 }], + poolTop: ["9C"], + poolOnly: true, + options: TWO_TILES, + }); + play(state, 0, place("4A")); + play(state, 1, { type: "dispose", sell: 3, trade: 0 }); + assert.equal(state.phase, "place"); + assert.equal(currentSeat(state), 0); + assert.deepEqual(state.log.at(-1), { type: "bankTile", tile: t("9C"), result: "loose" }); + assert.equal(state.board[t("9C")], "loose"); + play(state, 0, place("9I")); + assert.equal(state.phase, "buy"); +}); + +test("§15 twoTiles with §16: a dropped seat places its first two playable tiles", () => { + const state = scenario({ + hands: [["5E", "9I", "1A"], ["12C"], ["12E"]], + poolOnly: true, + options: TWO_TILES, + }); + dropPlayerSeats(state, 0); + const placed = entries(state, "move").filter((entry) => entry.seat === 0 && entry.move.type === "place"); + assert.deepEqual( + placed.map((entry) => entry.move), + [place("1A"), place("5E")] + ); + assert.ok(placed.every((entry) => entry.auto)); + assert.equal(currentSeat(state), 1); +}); + +test("§15 twoTiles: both placements can be drafted privately before committing", () => { + const state = scenario({ + board: chainAt("tower", ["1A", "2A"]), + hands: [["5E", "9I"], ["12C"], ["12E"]], + poolOnly: true, + options: TWO_TILES, + }); + const player = 0; + const draft = wrapper.move( + state, + { type: "turn", from: state.log.length, moves: [place("5E"), place("9I")] }, + player + ); + assert.equal(draft.phase, "buy"); + assert.deepEqual(draft.tentative, { player, from: state.log.length }); + assert.equal(wrapper.toSave(draft), undefined); +}); + +test("§14.7 twoTiles games replay exactly", () => { + let state = wrapper.init(4, [], TWO_TILES, "replay-two-tiles"); + for (let i = 0; i < 60 && !state.ended; i++) { + const player = wrapper.currentPlayer(state)!; + const moves = legalMoves(state).filter((move) => move.type !== "announce"); + state = wrapper.move(state, moves[i % moves.length]!, player); + } + assert.deepEqual(wrapper.replay(state), state); +}); diff --git a/packages/engine/src/two-tiles.ts b/packages/engine/src/two-tiles.ts new file mode 100644 index 0000000..fe90707 --- /dev/null +++ b/packages/engine/src/two-tiles.ts @@ -0,0 +1,41 @@ +import { placementEffect } from "./board.js"; +import type { Tile } from "./tiles.js"; +import type { GameState } from "./types.js"; + +/** What the active seat's placements have done so far this turn, read from the log. */ +export function turnPlacements(state: GameState): { placed: number; founded: boolean; merged: boolean } { + const result = { placed: 0, founded: false, merged: false }; + for (let i = state.log.length - 1; i >= 0; i--) { + const entry = state.log[i]!; + if (entry.type === "turn") { + break; + } + if (entry.type === "move" && entry.move.type === "place") { + result.placed++; + } else if (entry.type === "founded") { + result.founded = true; + } else if (entry.type === "merger" && !entry.byBank) { + // rules.md §15 twoTiles: mergers caused by bank tiles do not count. + result.merged = true; + } + } + return result; +} + +/** + * rules.md §15 twoTiles: a turn founds at most one chain, and never both founds a chain and + * causes a merger. Whether `tile` is ruled out by what this turn's placements already did. + */ +export function blockedThisTurn(state: GameState, tile: Tile): boolean { + if (!state.options.twoTiles) { + return false; + } + const { founded, merged } = turnPlacements(state); + const effect = placementEffect(state.board, tile).kind; + return (effect === "found" && (founded || merged)) || (effect === "merge" && founded); +} + +/** rules.md §15 twoTiles: whether the active seat places another tile before buying. */ +export function secondPlacementDue(state: GameState): boolean { + return state.options.twoTiles === true && turnPlacements(state).placed === 1; +} diff --git a/packages/viewer/src/dev.ts b/packages/viewer/src/dev.ts index 0382a02..a2b8960 100644 --- a/packages/viewer/src/dev.ts +++ b/packages/viewer/src/dev.ts @@ -29,7 +29,7 @@ async function loadBuiltViewer(): Promise { const players = Math.min(6, Math.max(2, Number(params.get("players") ?? 4))); const seed = params.get("seed") ?? "dev"; const options: Record = {}; -for (const key of ["publicHoldings", "teamPlay", "autoEnd"]) { +for (const key of ["publicHoldings", "teamPlay", "autoEnd", "twoTiles"]) { if (params.get(key) === "1") { options[key] = true; } diff --git a/packages/viewer/src/lib/Board.svelte b/packages/viewer/src/lib/Board.svelte index 1a9aacc..386f3f1 100644 --- a/packages/viewer/src/lib/Board.svelte +++ b/packages/viewer/src/lib/Board.svelte @@ -9,7 +9,7 @@ type GameState, type Tile, } from "acquire-engine"; - import { ofType } from "./decisions"; + import { ofType, turnLimit } from "./decisions"; import { chainName } from "./format"; import { CHAIN_STYLES } from "./palette"; import { safeBorders } from "./safeBorders"; @@ -36,6 +36,20 @@ const t = $derived(store.lang.t); const playable = $derived(new Set(ofType(store.moves, "place").map((move) => parseTile(move.tile)!))); const borders = $derived(safeBorders(state.board)); + + /** Why one of the viewer's tiles can't be placed now, or undefined. */ + function heldNote(tile: Tile): string | undefined { + const legality = tileLegality(state.board, tile); + if (legality === "permanentlyUnplayable") { + return t("hand.dead"); + } + if (legality === "temporarilyUnplayable") { + return t("hand.eighth"); + } + const seat = store.seats.find((s) => state.seats[s]?.hand.includes(tile)); + const limit = seat === undefined ? undefined : turnLimit(state, seat, tile); + return limit && t(`hand.${limit}`); + } const safeChains = $derived(new Set(borders.map(({ chain }) => chain))); const held = $derived( new Set( @@ -61,6 +75,7 @@ {@const cell = state.board[tile] ?? null} {@const label = tileLabel(tile)} {@const legality = held.has(tile) ? tileLegality(state.board, tile) : undefined} + {@const note = held.has(tile) ? heldNote(tile) : undefined} {#if playable.has(tile) && cell === null}