switching paths: two routes to the same square

Builds docs/plans/switching-paths.md. A passing loop can offer two legal
routes between the same two squares, coupling different cars — the engine
only ever found one, an artifact of search order (Reported by Jesse, undo
379). exploreMoves now enumerates every simple route (per-path visited set,
capped at 4000 frontier nodes) and dedupes on outcome — destination, entry
side, and origin-tagged cars — rather than on reaching the square at all.

switch.move gains an optional `via: GridCoord` naming one intermediate
square on the chosen route; absent, it resolves exactly as before, so
every existing save and bot decision replays identically (575/575, then
579/579 with the new tests). Threaded through the label, the action-list
dedupe, the hover highlight (data-route), and the history (trayMoved.via).

Ruling recorded as Gap 14 in docs/rules/open-questions.md: the player may
choose the path; §A.4's "may not go around" a car does not reach a
different track the player declined to enter.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SAt2YCXgd5qCjBcF2x34aK
This commit is contained in:
Jesse
2026-08-19 16:52:10 -04:00
co-authored by Claude Sonnet 5
parent 7932bb6ccf
commit fbaa3d4147
14 changed files with 440 additions and 36 deletions
+69 -9
View File
@@ -383,6 +383,28 @@ export type MoveBlock = { coord: GridCoord; kind: MoveBlockKind; why: string };
* beyond a reachable one whose rails do not meet. Squares the walk never came near are not listed:
* "there is no track between here and there" is not news.
*/
/**
* How many frontier nodes a single `exploreMoves` walk may enqueue before it stops finding new
* routes. A per-path visited set (below) guarantees every individual route terminates, but the
* NUMBER of simple paths through a dense district is worst-case exponential — a real board never
* gets close, but nothing stops a pathological one from being built. BFS order means the queue
* fills shortest-route-first, so hitting the cap loses only the longest, least-likely-to-matter
* routes; it never loses a shorter one in favour of a longer one.
*/
const MAX_ENUMERATED_FRONTIER = 4000;
/**
* The identity of a route's OUTCOME, for dedupe: which square it ends at, which side it entered
* by, and exactly which cars it couples, each tagged with the card it came off. Two routes that
* couple the same car TYPES from DIFFERENT cards are not the same outcome — sweeping the wrong
* card clean is exactly the bug this key exists to avoid — so the origin card rides along with
* every car, not just its type.
*/
function routeOutcomeKey(coord: GridCoord, entry: Port, couples: RollingStock[], origins: string[]): string {
const cars = couples.map((c, idx) => `${origins[idx]}:${c.type}:${c.loaded ? 1 : 0}`).join(',');
return `${coordKey(coord)}|${entry}|${cars}`;
}
export function exploreMoves(
ctx: MoveContext,
start: GridCoord,
@@ -403,9 +425,20 @@ export function exploreMoves(
if (!startCard) return { destinations: results, blocked };
if (!hasPort(startCard, initialExit)) return { destinations: results, blocked };
const seen = new Set<string>();
const resultKeys = new Set<string>();
type Frontier = { coord: GridCoord; entry: Port; path: MoveStep[]; couples: RollingStock[] };
type Frontier = {
coord: GridCoord;
entry: Port;
path: MoveStep[];
couples: RollingStock[];
/** Coord key each entry in `couples` came off, aligned by index — see `routeOutcomeKey`. */
origins: string[];
/** Cards visited on THIS route, start included. A per-path set, not a global one — see the
* module doc comment on `MAX_ENUMERATED_FRONTIER` for why a global one would forbid the very
* routes this walk exists to find. */
visited: Set<string>;
};
// The very first hop is checked here because `start`'s card is not itself enqueued; every later
// hop is checked at the push site below, where both sides of the edge are in hand.
@@ -427,9 +460,18 @@ export function exploreMoves(
* Ordered nearest-first like every other card's, so it simply seeds the accumulator.
*/
const ownCut = cutTowards({ standingWest: ctx.standingWest }, carsOn(startCard), initialExit);
const startKey = coordKey(start);
const queue: Frontier[] = [
{ coord: first, entry: opposite(initialExit), path: [], couples: ownCut },
{
coord: first,
entry: opposite(initialExit),
path: [],
couples: ownCut,
origins: ownCut.map(() => startKey),
visited: new Set([startKey]),
},
];
let enumerated = 1;
while (queue.length > 0) {
const node = queue.shift()!;
@@ -476,6 +518,8 @@ export function exploreMoves(
*/
const met = node.entry === 'e' ? [...carsOn(card)].reverse() : carsOn(card);
const couples = [...node.couples, ...met];
const nodeKey = coordKey(node.coord);
const origins = [...node.origins, ...met.map(() => nodeKey)];
if (ctx.consistSize + couples.length > MAX_CONSIST) {
const own =
ownCut.length > 0
@@ -489,18 +533,24 @@ export function exploreMoves(
continue;
}
const key = `${coordKey(node.coord)}|${node.entry}`;
if (seen.has(key)) continue;
seen.add(key);
if (isOperationalRail(card) && !sameCoord(node.coord, start)) {
results.push({ coord: node.coord, entry: node.entry, path: node.path, couples });
// Dedupe on OUTCOME, not on reaching the square: two routes that end here having coupled
// different cars (or the same cars off different cards) are a real choice, and both are
// offered. Two routes that end here having coupled identically are not a choice — one is
// noise doubling the button list and the bot's branching factor for nothing.
const rkey = routeOutcomeKey(node.coord, node.entry, couples, origins);
if (!resultKeys.has(rkey)) {
resultKeys.add(rkey);
results.push({ coord: node.coord, entry: node.entry, path: node.path, couples });
}
} else if (!sameCoord(node.coord, start)) {
// Reached, crossable, but not somewhere a train may STOP — a turnout carries no wheel icon
// (§A.1). Trains run through one all day; they just cannot finish a Move on it.
block(node.coord, 'noStopping', 'a train may run through here but not stop — this is not Operational Rail (§A.1), so it cannot be the end of a Move');
}
if (enumerated >= MAX_ENUMERATED_FRONTIER) continue;
for (const exit of exitsFrom(card, node.entry)) {
// Slope is a property of the EDGE, not of either card, so it can only be tested with both in
// hand. Enqueueing on `hasPort` alone routed trains across a 45° leg that bent back on itself.
@@ -510,8 +560,18 @@ export function exploreMoves(
if (next) block(to, 'noJoin', 'the rails do not meet — two cards touching is not a join, and on a north or south edge both 45° legs must lie on the same diagonal');
continue;
}
// A card twice on ONE route is a loop, not a longer route (§2.4 travels without changing
// direction; it never says without repeating ground, but a train cannot occupy the same
// track twice at once either). Per-path, not global — a DIFFERENT route may legitimately
// pass through a card this one already used.
const toKey = coordKey(to);
if (node.visited.has(toKey)) continue;
if (enumerated >= MAX_ENUMERATED_FRONTIER) break;
enumerated++;
const step: MoveStep = { coord: node.coord, entry: node.entry, exit };
queue.push({ coord: to, entry: opposite(exit), path: [...node.path, step], couples });
const visited = new Set(node.visited);
visited.add(toKey);
queue.push({ coord: to, entry: opposite(exit), path: [...node.path, step], couples, origins, visited });
}
}