diff --git a/docs/admin/gameinfo.json b/docs/admin/gameinfo.json index 9aefc63..5e743c4 100644 --- a/docs/admin/gameinfo.json +++ b/docs/admin/gameinfo.json @@ -143,10 +143,34 @@ }, { "name": "twoTiles", - "label": "Two tiles per turn (hands of 8)", + "label": "Two tiles per turn", "type": "checkbox", "default": false }, + { + "name": "handSize", + "label": "Tiles in hand", + "type": "select", + "default": "6", + "items": [ + { + "name": "6", + "label": "6 (standard)" + }, + { + "name": "8", + "label": "8" + }, + { + "name": "10", + "label": "10" + }, + { + "name": "12", + "label": "12" + } + ] + }, { "name": "limitedLog", "label": "Limited game log (only last round)", diff --git a/docs/admin/rules.md b/docs/admin/rules.md index b826a54..ce4b51b 100644 --- a/docs/admin/rules.md +++ b/docs/admin/rules.md @@ -14,7 +14,7 @@ There are 7 hotel chains, in three price tiers: - **Medium:** Worldwide, American, Festival - **Major:** Imperial, Continental -Each chain has 25 shares. Everyone starts with $6,000 and a hand of 6 tiles. Before the game, each player places one tile from the pool; the lowest tile (by number, then letter) goes first, and play continues in seat order. +Each chain has 25 shares. Everyone starts with $6,000 and a hand of 6 tiles (or 8, 10 or 12 with the Tiles in hand option). Before the game, each player places one tile from the pool; the lowest tile (by number, then letter) goes first, and play continues in seat order. A chain of 11 or more tiles is **safe**: it can never be taken over. @@ -22,7 +22,7 @@ A chain of 11 or more tiles is **safe**: it can never be taken over. 1. **Place one tile** from your hand. 2. **Buy up to 3 shares** by default, in any active chains (optional; see Shares per turn below). -3. **Draw** back up to 6 tiles. +3. **Draw** back up to 6 tiles (or the game’s hand size). ### What a tile does @@ -39,9 +39,9 @@ A chain of 11 or more tiles is **safe**: it can never be taken over. ### 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. +If the game uses **Two tiles per turn**, each player places two tiles every turn. The number of tiles in hand is a separate option, so you can combine two tiles per turn with any hand size. (Two-tile games started before that option existed use hands of 8.) -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 shares up to the game’s per-turn limit (3 by default) at the prices on the board, then draw back up to 8. +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 shares up to the game’s per-turn limit (3 by default) at the prices on the board, then draw back up to your hand size. You may found only one chain per turn, and your second tile can't cause a merger involving the chain your first tile founded. Otherwise each tile follows the usual rules: you can merge and then found a chain, found a chain and then merge two other chains, or cause two mergers, one with each tile. Example: your first tile founds Worldwide, so your second tile can merge Luxor into Tower or grow Worldwide, but it can't found another chain or cause a merger involving Worldwide. @@ -147,7 +147,8 @@ Because replaying the game would show the full history, replay is only available ## Game options - **Limited game log:** the log shows only what happened since your last turn, until the game ends (see above). -- **Two tiles per turn:** hands of 8 tiles and two tiles placed every turn (see above). +- **Two tiles per turn:** two tiles placed every turn (see above). +- **Tiles in hand:** 6 (standard), 8, 10 or 12 tiles in each hand, with or without two tiles per turn. - **Shares per turn:** buy up to 3 (standard), 4 or 5 shares in total per turn, independently of stock swaps. - **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. diff --git a/packages/engine/src/hand-size.spec.ts b/packages/engine/src/hand-size.spec.ts new file mode 100644 index 0000000..8128b11 --- /dev/null +++ b/packages/engine/src/hand-size.spec.ts @@ -0,0 +1,106 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { legalMoves } from "./legal.js"; +import { applyMove } from "./moves.js"; +import { positionAt } from "./position.js"; +import { initGame } from "./setup.js"; +import { chainAt, checkInvariants, row, scenario, t } from "./test/helpers.js"; +import type { GameState, Move } from "./types.js"; +import * as wrapper from "../wrapper.js"; + +const place = (tile: string): Move => ({ type: "place", tile }); +const buy: Move = { type: "buy", shares: {} }; + +function play(state: GameState, seat: number, move: Move): GameState { + applyMove(state, move, seat); + checkInvariants(state); + return state; +} + +test("§15 handSize: every seat is dealt the chosen hand in every mode and player count", () => { + for (const handSize of ["6", "8", "10", "12"]) { + for (const twoTiles of [false, true]) { + for (let players = 2; players <= 6; players++) { + const modes = players === 2 ? ["bank", "fourHands"] : ["bank"]; + for (const twoPlayerMode of modes) { + const state = initGame( + players, + { ...(twoTiles && { twoTiles }), ...(players === 4 && { teamPlay: true }), twoPlayerMode, handSize }, + `deal-${handSize}-${players}-${twoPlayerMode}-${twoTiles}` + ); + checkInvariants(state); + const size = Number(handSize); + assert.ok(state.seats.every((seat) => seat.hand.length === size)); + assert.ok(positionAt(state, state.log.length).seats.every((seat) => seat.tiles === size)); + const view = wrapper.stripSecret(state, 0); + assert.ok( + view.seats.every((seat) => seat.hand.length === size), + "masked hands keep their size" + ); + } + } + } + } + // Six hands of 12 plus six turn-order tiles still leave 30 of the 108 tiles to draw. + assert.equal(initGame(6, { handSize: "12" }, "deal-max").pool.length, 30); +}); + +test("§15 handSize: the hand refills to the chosen size after a turn", () => { + const hand = ["5E", "9G", "11G", "3I", "6I", "9I", "1G", "1I", "12A", "12C"]; + const state = scenario({ hands: [hand, ["12E"]], poolTop: ["12G", "12I"], options: { handSize: "10" } }); + play(state, 0, place("5E")); + assert.equal(state.seats[0]!.hand.length, 10); + assert.ok(state.seats[0]!.hand.includes(t("12G"))); + assert.ok(!state.seats[0]!.hand.includes(t("12I"))); +}); + +test("§15 handSize: dead tiles are replaced up to the chosen size", () => { + const state = scenario({ + board: { ...chainAt("tower", row("A", 1, 11)), ...chainAt("continental", row("C", 1, 11)) }, + hands: [["1B", "10G", "12E", "12G", "12I", "10I", "8I", "6I", "4I", "2I", "8G", "6G"], ["5E"]], + options: { handSize: "12" }, + }); + play(state, 0, place("10G")); + play(state, 0, buy); + assert.ok(state.removed.includes(t("1B"))); + assert.equal(state.seats[0]!.hand.length, 12); + assert.deepEqual(state.log.filter((entry) => entry.type === "drew").at(-1), { type: "drew", seat: 0, count: 2 }); +}); + +test("§15 handSize with twoTiles: both tiles are placed, then the hand refills to the chosen size", () => { + const state = scenario({ + board: chainAt("tower", ["1A", "2A"]), + hands: [["5E", "9I", "12A", "12C", "12E", "12G"], ["10C"], ["10E"]], + poolTop: ["7G", "7I"], + options: { twoTiles: true, handSize: "6" }, + }); + assert.equal(state.options.handSize, 6); + play(state, 0, place("5E")); + play(state, 0, place("9I")); + play(state, 0, buy); + assert.equal(state.seats[0]!.hand.length, 6); + assert.deepEqual(state.seats[0]!.hand.slice(-2), [t("7G"), t("7I")]); +}); + +/** Plays a random game to the end, checking invariants after every move, and replays it. */ +function playToEnd(players: number, options: Record, seed: string): GameState { + const state = wrapper.init(players, [], options, seed); + for (let step = 0; !state.ended; step++) { + assert.ok(step < 5000, `${seed} did not finish`); + const moves = legalMoves(state); + const others = moves.filter((move) => move.type !== "announce"); + const choices = others.length > 0 && step % 23 !== 0 ? others : moves; + wrapper.move(state, choices[(step * 7919) % choices.length]!, wrapper.currentPlayer(state)!); + checkInvariants(state); + } + assert.deepEqual(wrapper.replay(structuredClone(state)), state, `§14.7 ${seed} replays exactly`); + return state; +} + +test("§14.8 handSize 12: full games finish and replay with 6 players, two-player bank mode and four hands", () => { + playToEnd(6, { handSize: "12" }, "hand-12-six"); + playToEnd(6, { handSize: "12", twoTiles: true }, "hand-12-six-two-tiles"); + playToEnd(2, { handSize: "12", twoPlayerMode: "bank" }, "hand-12-bank"); + playToEnd(2, { handSize: "12", twoPlayerMode: "fourHands", twoTiles: true }, "hand-12-four-hands"); + playToEnd(4, { handSize: "10", teamPlay: true, publicHoldings: true }, "hand-10-teams"); +}); diff --git a/packages/engine/src/options.spec.ts b/packages/engine/src/options.spec.ts index 8a6100e..afef512 100644 --- a/packages/engine/src/options.spec.ts +++ b/packages/engine/src/options.spec.ts @@ -2,7 +2,7 @@ import assert from "node:assert/strict"; import { test } from "node:test"; import { endConditionHolds } from "./board.js"; import { applyMove } from "./moves.js"; -import { DEFAULT_OPTIONS, normalizeOptions } from "./options.js"; +import { DEFAULT_OPTIONS, handSize, normalizeOptions } from "./options.js"; import { sharePrice } from "./prices.js"; import { initGame } from "./setup.js"; import { chainAt, checkInvariants, row, scenario } from "./test/helpers.js"; @@ -93,6 +93,45 @@ test("§3.5 options are never rejected: unknown values are ignored and logged", assert.doesNotThrow(() => wrapper.init(2, [], { teamPlay: true, twoPlayerMode: "fourHands", endSize: 7 }, "x")); }); +test("§15 handSize: missing keeps the legacy hand of 6, or 8 with twoTiles, and leaves the init log unchanged", () => { + assert.equal("handSize" in normalizeOptions(3, {}).options, false); + assert.equal(handSize(normalizeOptions(3, {}).options), 6); + assert.equal("handSize" in normalizeOptions(3, { twoTiles: true }).options, false); + assert.equal(handSize(normalizeOptions(3, { twoTiles: true }).options), 8); + const init = initGame(3, {}, "no-hand-size").log[0] as Extract; + assert.deepEqual(init.options, DEFAULT_OPTIONS); +}); + +test("§15 handSize: every size is read as BGS sends it, alone or with twoTiles; the legacy size is omitted", () => { + for (const twoTiles of [false, true]) { + const legacy = twoTiles ? 8 : 6; + for (const size of [6, 8, 10, 12]) { + for (const sent of [String(size), size]) { + const { options, ignored } = normalizeOptions(3, { ...(twoTiles && { twoTiles }), handSize: sent }); + assert.deepEqual(ignored, []); + assert.equal(handSize(options), size); + assert.equal(options.handSize, size === legacy ? undefined : size); + assert.equal("handSize" in options, size !== legacy, `handSize ${sent} with twoTiles ${twoTiles}`); + } + } + } + assert.deepEqual(normalizeOptions(3, { handSize: "10" }).options, { ...DEFAULT_OPTIONS, handSize: 10 }); +}); + +test("§15 handSize: unknown values are ignored and logged, keeping the legacy size", () => { + for (const value of ["7", 7, "", "eight", "08", " 8", 6.5, 14, true, null, ["8"]]) { + for (const twoTiles of [false, true]) { + const state = initGame(3, { ...(twoTiles && { twoTiles }), handSize: value }, "bad-hand-size"); + assert.equal("handSize" in state.options, false); + assert.deepEqual( + ignoredLog(state).map((e) => [e.option, e.value, e.reason]), + [["handSize", value, "unknown value"]] + ); + assert.ok(state.seats.every((seat) => seat.hand.length === (twoTiles ? 8 : 6))); + } + } +}); + test("§15 endSize 31: a 31-tile chain meets the end condition; safe size and prices are unchanged", () => { const tiles = [...row("A", 1, 12), ...row("B", 1, 12), ...row("C", 1, 7)]; const board = scenario({ board: chainAt("tower", tiles), hands: [["9I"]] }).board; diff --git a/packages/engine/src/options.ts b/packages/engine/src/options.ts index 315bc10..4e371b5 100644 --- a/packages/engine/src/options.ts +++ b/packages/engine/src/options.ts @@ -13,6 +13,8 @@ export interface GameOptions { sharesPerTurn?: 4 | 5; /** Omitted when off, like stockSwap. */ twoTiles?: true; + /** Omitted at the legacy size (6, or 8 with twoTiles), so games created before it replay. */ + handSize?: 6 | 8 | 10 | 12; /** * Set with twoTiles in games created under the relaxed founding/merger limits (rules.md * §4.1). Two-tile games logged before them lack it and keep the original limits. @@ -37,6 +39,7 @@ export const DEFAULT_OPTIONS: GameOptions = { }; const END_SIZES = [41, 31]; +const HAND_SIZES = [6, 8, 10, 12, "6", "8", "10", "12"]; /** * Reads the creator's options defensively. BGS omits unticked checkboxes and always sends @@ -107,6 +110,16 @@ export function normalizeOptions( options.twoTilesRelaxed = true; } } + if (raw.handSize !== undefined) { + if (HAND_SIZES.includes(raw.handSize as number | string)) { + const size = Number(raw.handSize) as 6 | 8 | 10 | 12; + if (size !== legacyHandSize(options)) { + options.handSize = size; + } + } else { + ignored.push({ option: "handSize", value: raw.handSize, reason: "unknown value" }); + } + } if (raw.limitedLog === true) { options.limitedLog = true; } @@ -119,11 +132,16 @@ export function sharePurchaseLimit(options: GameOptions): number { return options.sharesPerTurn ?? MAX_BUY; } -/** rules.md §15 twoTiles: hands hold 8 tiles instead of 6. */ -export function handSize(options: GameOptions): number { +/** Games created before the handSize option hold 6 tiles, or 8 with twoTiles. */ +function legacyHandSize(options: GameOptions): number { return options.twoTiles ? TWO_TILE_HAND_SIZE : HAND_SIZE; } +/** rules.md §15 handSize: the number of tiles each seat draws up to. */ +export function handSize(options: GameOptions): number { + return options.handSize ?? legacyHandSize(options); +} + /** 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/viewer/src/dev.ts b/packages/viewer/src/dev.ts index 480a4f2..a527073 100644 --- a/packages/viewer/src/dev.ts +++ b/packages/viewer/src/dev.ts @@ -1,7 +1,7 @@ // Local dev harness (not part of the BGS bundle): runs the real engine in the page, plays // random moves on request, and lets you view the game as any player or as a spectator. // URL parameters: players=2..6, seed=..., view=N, hotseat=0, theme=light|dark, locale=de, and any game option, e.g. publicHoldings=1, -// twoPlayerMode=fourHands, teamPlay=1, autoEnd=1, endSize=31. For reproducible screenshots: moves=N plays N random moves, +// twoPlayerMode=fourHands, teamPlay=1, autoEnd=1, endSize=31, twoTiles=1, handSize=12. For reproducible screenshots: moves=N plays N random moves, // merger=1 then plays on to the next merger decision, bar=0 hides this toolbar. import * as rules from "acquire-engine"; import { currentSeat, legalMoves, type GameState } from "acquire-engine"; @@ -34,7 +34,7 @@ for (const key of ["publicHoldings", "teamPlay", "autoEnd", "twoTiles", "limited options[key] = true; } } -for (const key of ["twoPlayerMode", "endSize", "stockSwap", "sharesPerTurn"]) { +for (const key of ["twoPlayerMode", "endSize", "stockSwap", "sharesPerTurn", "handSize"]) { const value = params.get(key); if (value) { options[key] = value; diff --git a/packages/viewer/src/lib/Hand.svelte b/packages/viewer/src/lib/Hand.svelte index 00d28c6..7d7fed5 100644 --- a/packages/viewer/src/lib/Hand.svelte +++ b/packages/viewer/src/lib/Hand.svelte @@ -1,5 +1,5 @@