feat(engine): ghost derivation from cross-board piece comparison

This commit is contained in:
claude (duplicate_chess)
2026-05-19 00:53:51 -04:00
parent 7473cc69b3
commit 4e876b3197
2 changed files with 63 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
import type { DuplicateGame } from './game';
import type { GhostMarker, BoardId, Color, Square } from './types';
import { PLAYERS, PLAYER_BOARDS, PLAYER_COLOR } from './boards';
/** Squares occupied by a piece of `color` on `board`. */
function colorSquares(game: DuplicateGame, board: BoardId, color: Color): Set<Square> {
const set = new Set<Square>();
for (const row of game.boards[board].board()) {
for (const cell of row) {
if (cell && cell.color === color) set.add(cell.square);
}
}
return set;
}
/**
* Ghosts across all four players. A player's non-ghost pieces always occupy
* identical squares on both their boards (they move in lockstep), so a piece is
* a ghost iff the player's other board has no same-colour piece on that square.
*/
export function ghosts(game: DuplicateGame): GhostMarker[] {
const markers: GhostMarker[] = [];
for (const player of PLAYERS) {
const [a, b] = PLAYER_BOARDS[player];
const color = PLAYER_COLOR[player];
const sqA = colorSquares(game, a, color);
const sqB = colorSquares(game, b, color);
for (const sq of sqA) if (!sqB.has(sq)) markers.push({ board: a, square: sq });
for (const sq of sqB) if (!sqA.has(sq)) markers.push({ board: b, square: sq });
}
return markers;
}