33 lines
1.2 KiB
TypeScript
33 lines
1.2 KiB
TypeScript
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;
|
|
}
|