fertig-classic-games/src/games/othello/OthelloBook.js

86 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// OthelloBook.js: a small book of named 8×8 openings, written from Black's
// usual first move f5. The four legal first moves (f5, e6, d3, c4) are
// mirror images of one another, so a game is "normalised" onto f5 before it
// is looked up, and book replies are mapped back.
//
// openingName(moves) longest named line the game has followed
// bookReplies(moves) next moves (in the game's own orientation)
// openingCard(rnd) a line for the Master's opening card, mirrored
// at random: { name, moves: ['f5', …] }
import { parseSq, notate } from './OthelloLogic.js';
export const LINES = [
{ name: 'Diagonal Opening', moves: 'f5 f6' },
{ name: 'Perpendicular Opening', moves: 'f5 d6' },
{ name: 'Parallel Opening', moves: 'f5 f4' },
{ name: 'Tiger', moves: 'f5 d6 c3 d3 c4' },
{ name: 'Brightwell', moves: 'f5 d6 c3 d3 c4 f4 f6' },
{ name: 'Buffalo', moves: 'f5 d6 c3 d3 c4 f4 c5 b3 c2' },
{ name: 'Cow', moves: 'f5 d6 c5 f4 e3' },
{ name: 'Heath', moves: 'f5 f6 e6 f4 g5' },
{ name: 'Rabbit', moves: 'f5 f6 e6 f4 e3' },
].map((l) => ({ ...l, list: l.moves.split(' ') }));
// Lines worth starting a game from (the two-move "openings" are just families).
export const CARD_LINES = LINES.filter((l) => l.list.length >= 5);
const SIZE = 8;
// The symmetries of the starting position, each its own inverse.
const SYMS = [
(r, c) => [r, c],
(r, c) => [7 - r, 7 - c],
(r, c) => [c, r],
(r, c) => [7 - c, 7 - r],
];
function mapMove(n, sym) {
const sq = parseSq(n, SIZE);
if (sq < 0) return null;
const [r, c] = SYMS[sym](Math.floor(sq / SIZE), sq % SIZE);
return notate(r * SIZE + c, SIZE);
}
/** The symmetry that turns the game's first move into f5, or -1. */
function symFor(first) {
for (let i = 0; i < SYMS.length; i++) if (mapMove(first, i) === 'f5') return i;
return -1;
}
function normalise(moves) {
if (!moves.length) return { sym: 0, list: [] };
const sym = symFor(moves[0]);
if (sym < 0) return null;
return { sym, list: moves.map((m) => mapMove(m, sym)) };
}
const startsWith = (a, b) => b.length <= a.length && b.every((m, i) => a[i] === m);
/** Name of the longest book line the moves have followed in full. */
export function openingName(moves) {
const n = normalise(moves);
if (!n || !n.list.length) return null;
let best = null;
for (const l of LINES) if (startsWith(n.list, l.list) && (!best || l.list.length > best.list.length)) best = l;
return best?.name ?? null;
}
/** Book continuations from here (distinct, in the game's orientation). */
export function bookReplies(moves) {
if (!moves.length) return ['f5', 'e6', 'd3', 'c4'];
const n = normalise(moves);
if (!n) return [];
const out = new Set();
for (const l of LINES) {
if (l.list.length > n.list.length && startsWith(l.list, n.list)) out.add(mapMove(l.list[n.list.length], n.sym));
}
return [...out];
}
export function openingCard(rnd = Math.random, avoid = []) {
const pool = CARD_LINES.filter((l) => !avoid.includes(l.name));
const l = (pool.length ? pool : CARD_LINES)[Math.floor(rnd() * (pool.length || CARD_LINES.length))];
const sym = Math.floor(rnd() * SYMS.length);
return { name: l.name, moves: l.list.map((m) => mapMove(m, sym)) };
}