forked from rooklift/nibbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
42_perft.js
62 lines (52 loc) · 1.79 KB
/
42_perft.js
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
"use strict";
/* Perft notes:
The correct perft value for a position and depth is the number of leaf nodes at
that depth (or equivalently, the number of legal move sequences of that length).
Some important points:
- Rules about "Triple Repetition" and "Insufficient Material" are ignored.
- Terminal nodes (mates) at a shallower depth are not counted.
- But they are counted if they are at the correct depth.
*/
function Perft(fen, depth) {
if (!fen || !depth) throw "Need FEN and depth";
let starttime = performance.now();
let board = LoadFEN(fen);
let val = perft(board, depth, true);
console.log(`Total.......... ${val} (${((performance.now() - starttime) / 1000).toFixed(1)} seconds)`);
if (perft_known_values[fen] && perft_known_values[fen][depth]) {
if (perft_known_values[fen][depth] === val) {
console.log("Known good result");
} else {
console.log(`Known BAD result -- expected ${perft_known_values[fen][depth]}`);
}
}
return val;
}
function perft(pos, depth, print_moves) {
let moves = pos.movegen();
if (depth === 1) {
return moves.length;
} else {
let count = 0;
for (let mv of moves) {
let val = perft(pos.move(mv), depth - 1, false);
if (print_moves) {
perft_print_move(pos, mv, val);
}
count += val;
}
return count;
}
}
function perft_print_move(pos, mv, val) {
let nice = pos.nice_string(mv);
console.log(`${mv + (mv.length === 4 ? " " : "")} ${nice + " ".repeat(7 - nice.length)}`, val);
}
// In Stockfish:
// setoption name UCI_Chess960 value true
// position fen <whatever>
// go perft 4
let perft_known_values = {
"Qr3knr/P1bp1p1p/2pn1q2/4p3/2PP2pB/1p1N1bP1/BP2PP1P/1R3KNR w BHbh - 0 1": [0, 31, 1122, 34613, 1253934, 40393041],
"1nr1nk1r/1b5B/p1p1qp2/b2pp1pP/3P2P1/P3P2N/1Pp2P2/BNR2KQR w CHch g6 0 1": [0, 28, 964, 27838, 992438, 30218648],
};