-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
debug.ts
103 lines (94 loc) · 3.05 KB
/
debug.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Square, Piece, Role, ROLES } from './types';
import { opposite, squareRank, makeSquare, makeUci } from './util';
import { makePiece } from './fen';
import { SquareSet } from './squareSet';
import { Board } from './board';
import { Position } from './chess';
export function squareSet(squares: SquareSet): string {
const r = [];
for (let y = 7; y >= 0; y--) {
for (let x = 0; x < 8; x++) {
const square = x + y * 8;
r.push(squares.has(square) ? '1' : '.');
r.push(x < 7 ? ' ' : '\n');
}
}
return r.join('');
}
export function piece(piece: Piece): string {
return makePiece(piece);
}
export function board(board: Board): string {
const r = [];
for (let y = 7; y >= 0; y--) {
for (let x = 0; x < 8; x++) {
const square = x + y * 8;
const p = board.get(square);
const col = p ? piece(p) : '.';
r.push(col);
r.push(x < 7 ? (col.length < 2 ? ' ' : '') : '\n');
}
}
return r.join('');
}
export function square(sq: Square): string {
return makeSquare(sq);
}
export function dests(dests: Map<Square, SquareSet>): string {
const lines = [];
for (const [from, to] of dests) {
lines.push(`${makeSquare(from)}: ${Array.from(to, square).join(' ')}`);
}
return lines.join('\n');
}
export function perft(pos: Position, depth: number, log = false): number {
if (depth < 1) return 1;
const promotionRoles: Role[] = ['queen', 'knight', 'rook', 'bishop'];
if (pos.rules === 'antichess') promotionRoles.push('king');
const ctx = pos.ctx();
const dropDests = pos.dropDests(ctx);
if (!log && depth === 1 && dropDests.isEmpty()) {
// Optimization for leaf nodes.
let nodes = 0;
for (const [from, to] of pos.allDests(ctx)) {
nodes += to.size();
if (pos.board.pawn.has(from)) {
const backrank = SquareSet.backrank(opposite(pos.turn));
nodes += to.intersect(backrank).size() * (promotionRoles.length - 1);
}
}
return nodes;
} else {
let nodes = 0;
for (const [from, dests] of pos.allDests(ctx)) {
const promotions: Array<Role | undefined> =
(squareRank(from) === (pos.turn === 'white' ? 6 : 1) && pos.board.pawn.has(from)) ?
promotionRoles : [undefined];
for (const to of dests) {
for (const promotion of promotions) {
const child = pos.clone();
const move = { from, to, promotion };
child.play(move);
const children = perft(child, depth - 1, false);
if (log) console.log(makeUci(move), children);
nodes += children;
}
}
}
if (pos.pockets) {
for (const role of ROLES) {
if (pos.pockets[pos.turn][role] > 0) {
for (const to of (role === 'pawn' ? dropDests.diff(SquareSet.backranks()) : dropDests)) {
const child = pos.clone();
const move = { role, to };
child.play(move);
const children = perft(child, depth - 1, false);
if (log) console.log(makeUci(move), children);
nodes += children;
}
}
}
}
return nodes;
}
}