|
|
|
@@ -1,4 +1,5 @@
|
|
|
|
|
import type { BoardView, Color, PieceType, Square } from '@blind-chess/shared';
|
|
|
|
|
import { Game as EngineGame } from 'js-chess-engine';
|
|
|
|
|
import type { BoardView, Color, PieceType, PromotionType, Square } from '@blind-chess/shared';
|
|
|
|
|
import type {
|
|
|
|
|
Brain,
|
|
|
|
|
BrainAction,
|
|
|
|
@@ -9,6 +10,13 @@ import type {
|
|
|
|
|
|
|
|
|
|
interface CasualOpts {
|
|
|
|
|
seed?: number;
|
|
|
|
|
/**
|
|
|
|
|
* Engine difficulty for vanilla mode (1-5; 1 is weakest).
|
|
|
|
|
* `js-chess-engine` level 1 plays at roughly beginner strength —
|
|
|
|
|
* crushes random moves but loses to a careful human. Higher levels
|
|
|
|
|
* raise both strength and per-move latency.
|
|
|
|
|
*/
|
|
|
|
|
level?: 1 | 2 | 3 | 4 | 5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const PIECE_VALUE: Record<PieceType, number> = {
|
|
|
|
@@ -18,10 +26,12 @@ const PIECE_VALUE: Record<PieceType, number> = {
|
|
|
|
|
export class CasualBrain implements Brain {
|
|
|
|
|
private color: Color = 'w';
|
|
|
|
|
private mode: 'blind' | 'vanilla' = 'blind';
|
|
|
|
|
private level: 1 | 2 | 3 | 4 | 5;
|
|
|
|
|
private rng: () => number;
|
|
|
|
|
|
|
|
|
|
constructor(opts: CasualOpts = {}) {
|
|
|
|
|
this.rng = mulberry32(opts.seed ?? Math.floor(Math.random() * 0xffffffff));
|
|
|
|
|
this.level = opts.level ?? 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async init(args: BrainInitArgs): Promise<void> {
|
|
|
|
@@ -39,22 +49,24 @@ export class CasualBrain implements Brain {
|
|
|
|
|
throw new Error('CasualBrain: zero candidates after exclusion');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const scored = filtered.map((c) => {
|
|
|
|
|
let score = this.scoreMove(c, input.view, input.ply);
|
|
|
|
|
// Promotion bias: prefer queen >> rook >> bishop >> knight
|
|
|
|
|
// Add before random tiebreak to ensure queen wins when tied.
|
|
|
|
|
if (c.promotion === 'q') score += 1000;
|
|
|
|
|
else if (c.promotion === 'r') score += 500;
|
|
|
|
|
else if (c.promotion === 'b') score += 100;
|
|
|
|
|
else if (c.promotion === 'n') score += 50;
|
|
|
|
|
return {
|
|
|
|
|
move: c,
|
|
|
|
|
score: score + this.rng() * 0.01,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
scored.sort((a, b) => b.score - a.score);
|
|
|
|
|
const choice = scored[0]!.move;
|
|
|
|
|
// Vanilla mode: delegate to a real chess engine. The driver supplies
|
|
|
|
|
// a FEN only in vanilla mode, so this branch is naturally gated.
|
|
|
|
|
if (this.mode === 'vanilla' && input.fen) {
|
|
|
|
|
const engineMove = this.engineMove(input.fen, filtered);
|
|
|
|
|
if (engineMove) {
|
|
|
|
|
return {
|
|
|
|
|
type: 'commit',
|
|
|
|
|
from: engineMove.from,
|
|
|
|
|
to: engineMove.to,
|
|
|
|
|
promotion: engineMove.promotion,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
// Fall through to heuristic if the engine produced something we
|
|
|
|
|
// can't validate against the candidate list.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Blind mode (or vanilla fallback): score-based heuristic.
|
|
|
|
|
const choice = this.heuristicPick(filtered, input.view, input.ply);
|
|
|
|
|
return {
|
|
|
|
|
type: 'commit',
|
|
|
|
|
from: choice.from,
|
|
|
|
@@ -63,6 +75,57 @@ export class CasualBrain implements Brain {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run js-chess-engine on the given FEN and return a candidate matching
|
|
|
|
|
* its choice, or null if no match was found.
|
|
|
|
|
*/
|
|
|
|
|
private engineMove(fen: string, candidates: CandidateMove[]): CandidateMove | null {
|
|
|
|
|
let result: { move: Record<string, string> };
|
|
|
|
|
try {
|
|
|
|
|
const g = new EngineGame(fen);
|
|
|
|
|
// randomness=30 picks among moves within 30 centipawns of best; this
|
|
|
|
|
// breaks threefold-repetition draws when the bot is clearly winning
|
|
|
|
|
// but doesn't see the conversion path.
|
|
|
|
|
result = g.ai({ level: this.level, play: false, randomness: 30 }) as { move: Record<string, string> };
|
|
|
|
|
} catch {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
const entry = Object.entries(result.move ?? {})[0];
|
|
|
|
|
if (!entry) return null;
|
|
|
|
|
const [fromUC, toUC] = entry;
|
|
|
|
|
const from = (fromUC as string).toLowerCase() as Square;
|
|
|
|
|
const to = (toUC as string).toLowerCase() as Square;
|
|
|
|
|
// Find a candidate matching this from-to. If the move is a promotion,
|
|
|
|
|
// js-chess-engine emits the destination square (e.g., {E7: 'E8'}) but
|
|
|
|
|
// doesn't separately surface the promotion piece — default to queen.
|
|
|
|
|
const matches = candidates.filter((c) => c.from === from && c.to === to);
|
|
|
|
|
if (matches.length === 0) return null;
|
|
|
|
|
const queen = matches.find((c) => c.promotion === 'q');
|
|
|
|
|
if (queen) return queen;
|
|
|
|
|
return matches[0]!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Score-based fallback used for blind mode and any vanilla case where
|
|
|
|
|
* the engine's pick wasn't in the candidate list. Plays badly on purpose.
|
|
|
|
|
*/
|
|
|
|
|
private heuristicPick(
|
|
|
|
|
candidates: CandidateMove[],
|
|
|
|
|
view: BoardView,
|
|
|
|
|
ply: number,
|
|
|
|
|
): CandidateMove {
|
|
|
|
|
const scored = candidates.map((c) => {
|
|
|
|
|
let score = this.scoreMove(c, view, ply);
|
|
|
|
|
if (c.promotion === 'q') score += 1000;
|
|
|
|
|
else if (c.promotion === 'r') score += 500;
|
|
|
|
|
else if (c.promotion === 'b') score += 100;
|
|
|
|
|
else if (c.promotion === 'n') score += 50;
|
|
|
|
|
return { move: c, score: score + this.rng() * 0.01 };
|
|
|
|
|
});
|
|
|
|
|
scored.sort((a, b) => b.score - a.score);
|
|
|
|
|
return scored[0]!.move;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private excludeRejected(
|
|
|
|
|
candidates: CandidateMove[],
|
|
|
|
|
history: BrainInput['attemptHistory'],
|
|
|
|
@@ -74,43 +137,31 @@ export class CasualBrain implements Brain {
|
|
|
|
|
|
|
|
|
|
private scoreMove(move: CandidateMove, view: BoardView, ply: number): number {
|
|
|
|
|
let score = 0;
|
|
|
|
|
|
|
|
|
|
// Capture proxy: destination not own-occupied. (In view, we only see own
|
|
|
|
|
// pieces in blind mode; if dest has a piece it's ours -> not a capture.
|
|
|
|
|
// If empty in view, may be empty or opponent — guess.)
|
|
|
|
|
const destPiece = view.pieces[move.to];
|
|
|
|
|
if (!destPiece) score += 50;
|
|
|
|
|
|
|
|
|
|
const piece = view.pieces[move.from];
|
|
|
|
|
// Reachable when scoring tests pass minimal fixture views. In real play
|
|
|
|
|
// the candidate's `from` is always one of our pieces (since candidates
|
|
|
|
|
// came from `legalCandidates` over our own squares), but the early
|
|
|
|
|
// return keeps unit tests from needing full board fixtures.
|
|
|
|
|
if (!piece) return score;
|
|
|
|
|
|
|
|
|
|
const ownStartingRank = this.color === 'w' ? '1' : '8';
|
|
|
|
|
const ownPawnStartingRank = this.color === 'w' ? '2' : '7';
|
|
|
|
|
|
|
|
|
|
// Development bonus for first 16 plies (8 moves per side).
|
|
|
|
|
if (ply < 16 && (piece.type === 'n' || piece.type === 'b')
|
|
|
|
|
&& move.from[1] === ownStartingRank) {
|
|
|
|
|
score += 30;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Center pawn bonus.
|
|
|
|
|
if (piece.type === 'p' && move.from[1] === ownPawnStartingRank) {
|
|
|
|
|
const file = move.from[0];
|
|
|
|
|
if (file === 'd' || file === 'e') score += 25;
|
|
|
|
|
else if (file === 'c' || file === 'f') score += 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rank-advance bonus toward opponent.
|
|
|
|
|
const fromRank = parseInt(move.from[1]!, 10);
|
|
|
|
|
const toRank = parseInt(move.to[1]!, 10);
|
|
|
|
|
const advance = this.color === 'w' ? toRank - fromRank : fromRank - toRank;
|
|
|
|
|
if (advance > 0) score += 15 * advance;
|
|
|
|
|
|
|
|
|
|
// Anti-shuffling: penalize moving major pieces from start before knights/bishops.
|
|
|
|
|
if (move.from[1] === ownStartingRank && (piece.type === 'q' || piece.type === 'r')) {
|
|
|
|
|
score -= 40;
|
|
|
|
|
}
|
|
|
|
@@ -119,8 +170,6 @@ export class CasualBrain implements Brain {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private acceptDraw(view: BoardView): boolean {
|
|
|
|
|
// Crude material count from own view only. Accept if "low material"
|
|
|
|
|
// (assume opponent symmetric). Decline if "high material".
|
|
|
|
|
let own = 0;
|
|
|
|
|
for (const sq of Object.keys(view.pieces) as Square[]) {
|
|
|
|
|
const p = view.pieces[sq];
|
|
|
|
@@ -134,7 +183,6 @@ function moveKey(m: CandidateMove): string {
|
|
|
|
|
return `${m.from}-${m.to}${m.promotion ?? ''}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mulberry32 PRNG: seedable, fast, good enough for tiebreaks.
|
|
|
|
|
function mulberry32(seed: number): () => number {
|
|
|
|
|
let a = seed >>> 0;
|
|
|
|
|
return function () {
|
|
|
|
|