Support up to 25 queued premoves with projected board state

This commit is contained in:
tyuXX 2026-11-22 20:14:40 +03:00
parent 3ccab53b41
commit 14263d3c32
6 changed files with 304 additions and 82 deletions

View file

@ -1,7 +1,17 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { Chess, algToSq } from '../src/shared/chess';
import { computePremoveTargets, resolveQueuedPremove } from '../src/shared/premove';
import {
MAX_PREMOVES,
type QueuedPremove,
applyPremoveQueueToBoard,
computePremoveTargets,
resolveQueuedPremove
} from '../src/shared/premove';
test('MAX_PREMOVES is 25', () => {
assert.equal(MAX_PREMOVES, 25);
});
test('Pawn premove targets include 1-step, 2-step from start, diagonal captures, and promotions', () => {
const c = new Chess('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1');
@ -19,11 +29,30 @@ test('Pawn premove targets include 1-step, 2-step from start, diagonal captures,
assert.deepEqual(c7Targets.get(algToSq('d8')), ['q', 'r', 'b', 'n']);
});
test('Chained multi-premove targets project board state across consecutive moves', () => {
const c = new Chess('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1');
const queue: QueuedPremove[] = [
{ from: algToSq('d2'), to: algToSq('d4') },
{ from: algToSq('d4'), to: algToSq('d5') }
];
// After queueing d2->d4 and d4->d5, pawn is projected at d5 and d2 is empty
const projected = applyPremoveQueueToBoard(c.board(), queue);
assert.equal(projected[algToSq('d2')], null);
assert.deepEqual(projected[algToSq('d5')], { color: 'w', type: 'p' });
// From d5, pawn can premove d6 (1-step), c6, e6 — NOT d7 (2-step only from start rank)
const d5Targets = computePremoveTargets(c, algToSq('d5'), queue);
assert.equal(d5Targets.has(algToSq('d6')), true);
assert.equal(d5Targets.has(algToSq('c6')), true);
assert.equal(d5Targets.has(algToSq('e6')), true);
assert.equal(d5Targets.has(algToSq('d7')), false);
});
test('Slider premove targets ignore blockers and resolve recapture when turn returns', () => {
// Black to move: Black bishop on g4 can take White knight on f3; White queen on d1 premoves Qxf3
const c = new Chess('r2qkbnr/ppp2ppp/2np4/4p3/4P1b1/3P1N2/PPP2PPP/RNBQKB1R b KQkq - 0 4');
const qTargets = computePremoveTargets(c, algToSq('d1'));
// Even though f3 is currently occupied by own knight and e2 is occupied by own pawn, queen can premove d1->f3 and d1->h5
assert.equal(qTargets.has(algToSq('f3')), true);
assert.equal(qTargets.has(algToSq('h5')), true);
@ -39,12 +68,31 @@ test('Slider premove targets ignore blockers and resolve recapture when turn ret
assert.equal(dropped, null);
});
test('King castling premove targets respect FEN castling rights and check resolution', () => {
test('King castling in premove queue moves rook on projected board and disables repeat castling', () => {
const withRights = new Chess('r3k2r/8/8/8/8/8/8/R3K2R b KQkq - 0 1');
const kTargets = computePremoveTargets(withRights, algToSq('e1'));
assert.equal(kTargets.has(algToSq('g1')), true);
assert.equal(kTargets.has(algToSq('c1')), true);
// Queue O-O (e1->g1): projected board places White king on g1 and White rook on f1
const queueAfterCastle: QueuedPremove[] = [{ from: algToSq('e1'), to: algToSq('g1') }];
const projected = applyPremoveQueueToBoard(withRights.board(), queueAfterCastle);
assert.deepEqual(projected[algToSq('g1')], { color: 'w', type: 'k' });
assert.deepEqual(projected[algToSq('f1')], { color: 'w', type: 'r' });
assert.equal(projected[algToSq('h1')], null);
// Rook on f1 can now premove along the f-file
const f1Targets = computePremoveTargets(withRights, algToSq('f1'), queueAfterCastle);
assert.equal(f1Targets.has(algToSq('f8')), true);
// Once king moves in queue, e1->c1 castling is no longer offered even if projected back
const movedBackQueue: QueuedPremove[] = [
{ from: algToSq('e1'), to: algToSq('f1') },
{ from: algToSq('f1'), to: algToSq('e1') }
];
const e1AfterMove = computePremoveTargets(withRights, algToSq('e1'), movedBackQueue);
assert.equal(e1AfterMove.has(algToSq('g1')), false);
const noRights = new Chess('r3k2r/8/8/8/8/8/8/R3K2R b - - 0 1');
const kNoTargets = computePremoveTargets(noRights, algToSq('e1'));
assert.equal(kNoTargets.has(algToSq('g1')), false);
@ -54,3 +102,23 @@ test('King castling premove targets respect FEN castling rights and check resolu
const checkGame = new Chess('4k3/8/8/8/8/8/4P3/4K2r w - - 0 1');
assert.equal(resolveQueuedPremove(checkGame, { from: algToSq('e2'), to: algToSq('e4') }), null);
});
test('Multi-premove queue can queue 25 moves and executes sequentially until completion or illegality', () => {
const c = new Chess('4k3/8/8/8/8/8/R7/4K3 b - - 0 1');
const queue: QueuedPremove[] = [];
for (let i = 0; i < MAX_PREMOVES; i++) {
queue.push(i % 2 === 0 ? { from: algToSq('a2'), to: algToSq('a3') } : { from: algToSq('a3'), to: algToSq('a2') });
}
assert.equal(queue.length, 25);
// Black plays Ke7 -> White executes premove 0 (a2->a3) -> Black plays Kd7 -> White executes premove 1 (a3->a2)
assert.ok(c.moveSan('Ke7'));
const uci0 = resolveQueuedPremove(c, queue[0]!);
assert.equal(uci0, 'a2a3');
assert.ok(c.moveUci(uci0!));
assert.ok(c.moveSan('Kd7'));
const uci1 = resolveQueuedPremove(c, queue[1]!);
assert.equal(uci1, 'a3a2');
assert.ok(c.moveUci(uci1!));
});