]> code.delx.au - gnu-emacs-elpa/blob - chess-perft.el
Proper file header.
[gnu-emacs-elpa] / chess-perft.el
1 ;;; chess-perft.el --- Perft tests for emacs-chess -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2014 Mario Lang
4
5 ;; Author: Mario Lang <mlang@delysid.org>
6 ;; Keywords: games
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;; The classic perft function counts all leaf nodes at a certain depth.
24 ;; To make it easier to identify specific problems we also count properties
25 ;; of the (final) plies. We count capturing plies, en passant plies,
26 ;; castling plies, plies that promote to a piece,
27 ;; plies which bring the opponent king in check and plies which result in
28 ;; checkmate.
29
30 ;; For more details about perft in general, see
31 ;; <URL:https://chessprogramming.wikispaces.com/Perft>.
32
33 ;; Typically, depths greater than 4 will result in very long runtimes.
34 ;; We only define tests which don't take a lot of execution time
35 ;; (less than a million nodes).
36
37 ;; To make it easier to selectively run tests, most tests provide tags
38 ;; to indentify which types of plies they are covering.
39 ;; The available ERT tags are:
40 ;; `:capture', `:en-passant', `:castle', `:promotion',
41 ;; `:check' and `:checkmate'.
42 ;;
43 ;; For instance, to make sure castling plies work as expected, you might run
44 ;; M-: (ert '(tag :castle)) RET
45
46 ;;; Code:
47
48 (require 'chess-fen)
49 (require 'chess-ply)
50 (require 'chess-pos)
51 (require 'cl-lib)
52 (require 'ert)
53
54 (defun chess-perft (position depth)
55 "Count all leaf nodes of the tree starting at POSITION pruned at DEPTH.
56 If not called interactively the result is a list of the form
57 \(LEAFS CAPTURES EN-PASSANTS CASTLES PROMOTIONS CHECKS CHECKMATES)."
58 (interactive (list (or (ignore-errors (chess-display-position nil))
59 (chess-fen-to-pos (read-string "FEN: " nil nil
60 (chess-pos-to-fen
61 (chess-pos-create)))))
62 (read-number "Depth: " 1)))
63 (if (zerop depth)
64 (cl-values 1 0 0 0 0 0 0)
65 (let ((plies (chess-legal-plies position
66 :color (chess-pos-side-to-move position)))
67 (nodes 0) (captures 0) (en-passants 0) (castles 0) (promotions 0)
68 (checks 0) (checkmates 0))
69 (if (= depth 1)
70 (dolist (ply plies)
71 (cl-incf nodes)
72 (when (let ((position (chess-ply-pos ply)))
73 (chess-pos-piece-p position (chess-ply-target ply)
74 (not (chess-pos-side-to-move position))))
75 (cl-incf captures))
76 (when (chess-ply-keyword ply :en-passant)
77 (cl-incf captures)
78 (cl-incf en-passants))
79 (if (chess-ply-any-keyword ply :castle :long-castle)
80 (cl-incf castles)
81 (when (chess-ply-keyword ply :promote)
82 (cl-incf promotions)))
83 (when (chess-ply-any-keyword ply :check :checkmate)
84 (cl-incf checks))
85 (when (chess-ply-any-keyword ply :checkmate)
86 (cl-incf checkmates)) )
87 (let ((progress (when (called-interactively-p 'any)
88 (make-progress-reporter "Perft... " 0 (length plies))))
89 (index 0))
90 (when (and (not noninteractive) (= depth 2)) (accept-process-output))
91 (dolist (ply plies)
92 (unless (chess-ply-final-p ply)
93 (cl-multiple-value-bind (n c e ca p ch cm)
94 (chess-perft (chess-ply-next-pos ply) (1- depth))
95 (cl-incf nodes n)
96 (cl-incf captures c)
97 (cl-incf en-passants e)
98 (cl-incf castles ca)
99 (cl-incf promotions p)
100 (cl-incf checks ch)
101 (cl-incf checkmates cm)))
102
103 (when progress
104 (cl-incf index)
105 (progress-reporter-force-update
106 progress index (format "Perft... (%d nodes) " nodes))))))
107
108 (if (called-interactively-p 'any)
109 (message "%d nodes (%d captures (%d ep), %d castles, %d promotions and %d checks (%d mate))"
110 nodes captures en-passants castles promotions checks checkmates)
111 (cl-values nodes
112 captures en-passants castles promotions checks checkmates)))))
113
114 (ert-deftest chess-perft-startpos-depth1 ()
115 (should (equal (chess-perft (chess-pos-create) 1) '(20 0 0 0 0 0 0))))
116
117 (ert-deftest chess-perft-startpos-depth2 ()
118 (should (equal (chess-perft (chess-pos-create) 2) '(400 0 0 0 0 0 0))))
119
120 (ert-deftest chess-perft-startpos-depth3 ()
121 :tags '(:capture :check)
122 (should (equal (chess-perft (chess-pos-create) 3) '(8902 34 0 0 0 12 0))))
123
124 (ert-deftest chess-perft-startpos-depth4 ()
125 :tags '(:capture :check :checkmate)
126 (should (equal (chess-perft (chess-pos-create) 4)
127 '(197281 1576 0 0 0 469 8))))
128
129 (ert-deftest chess-perft-startpos-depth5 ()
130 :tags '(:capture :en-passant :check :checkmate)
131 (should (equal (chess-perft (chess-pos-create) 5) '(4865609 82719 258 0 0 27351 347))))
132
133 (ert-deftest chess-perft-kiwipete-depth1 ()
134 :tags '(:capture :castle)
135 (let ((position
136 (chess-fen-to-pos
137 "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -")))
138 (should (equal (chess-perft position 1) '(48 8 0 2 0 0 0)))))
139
140 (ert-deftest chess-perft-kiwipete-depth2 ()
141 :tags '(:capture :en-passant :castle :check)
142 (let ((position
143 (chess-fen-to-pos
144 "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -")))
145 (should (equal (chess-perft position 2) '(2039 351 1 91 0 3 0)))))
146
147 (ert-deftest chess-perft-kiwipete-depth3 ()
148 :tags '(:capture :en-passant :castle :check :checkmate)
149 (let ((position
150 (chess-fen-to-pos
151 "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -")))
152 (should (equal (chess-perft position 3) '(97862 17102 45 3162 0 993 1)))))
153
154 (ert-deftest chess-perft-kiwipete-depth4 ()
155 :tags '(:capture :en-passant :castle :promote :check :checkmate)
156 (let ((position
157 (chess-fen-to-pos
158 "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -")))
159 (should (equal (chess-perft position 4)
160 '(4085603 757163 1929 128013 15172 25523 43)))))
161
162 (ert-deftest chess-perft-pos3-depth1 ()
163 :tags '(:capture :check)
164 (let ((position (chess-fen-to-pos "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -")))
165 (should (equal (chess-perft position 1) '(14 1 0 0 0 2 0)))))
166
167 (ert-deftest chess-perft-pos3-depth2 ()
168 :tags '(:capture :check)
169 (let ((position (chess-fen-to-pos "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -")))
170 (should (equal (chess-perft position 2) '(191 14 0 0 0 10 0)))))
171
172 (ert-deftest chess-perft-pos3-depth3 ()
173 :tags '(:capture :en-passant :check)
174 (let ((position (chess-fen-to-pos "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -")))
175 (should (equal (chess-perft position 3) '(2812 209 2 0 0 267 0)))))
176
177 (ert-deftest chess-perft-pos3-depth4 ()
178 :tags '(:capture :en-passant :check :checkmate)
179 (let ((position (chess-fen-to-pos "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -")))
180 (should (equal (chess-perft position 4) '(43238 3348 123 0 0 1680 17)))))
181
182 (ert-deftest chess-perft-pos3-depth5 ()
183 :tags '(:capture :en-passant :check)
184 (let ((position (chess-fen-to-pos "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -")))
185 (should (equal (chess-perft position 5) '(674624 52051 1165 0 0 52950 0)))))
186
187 (ert-deftest chess-perft-pos4-depth1 ()
188 (let ((chess-ply-allow-interactive-query nil)
189 (position
190 (chess-fen-to-pos
191 "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq -")))
192 (should (equal (chess-perft position 1) '(6 0 0 0 0 0 0)))))
193
194 (ert-deftest chess-perft-pos4-depth2 ()
195 :tags '(:capture :castle :promotion :check)
196 (let ((chess-ply-allow-interactive-query nil)
197 (position
198 (chess-fen-to-pos
199 "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq -")))
200 (should (equal (chess-perft position 2) '(264 87 0 6 48 10 0)))))
201
202 (ert-deftest chess-perft-pos4-depth3 ()
203 :tags '(:capture :en-passant :promotion :check :checkmate)
204 (let ((chess-ply-allow-interactive-query nil)
205 (position
206 (chess-fen-to-pos
207 "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq -")))
208 (should (equal (chess-perft position 3) '(9467 1021 4 0 120 38 22)))))
209
210 (ert-deftest chess-perft-pos4-depth4 ()
211 :tags '(:capture :castle :promotion :check :checkmate)
212 (let ((chess-ply-allow-interactive-query nil)
213 (position
214 (chess-fen-to-pos
215 "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq -")))
216 (should (equal (chess-perft position 4) '(422333 131393 0 7795 60032 15492 5)))))
217
218 (provide 'chess-perft)
219 ;;; chess-perft.el ends here