summaryrefslogtreecommitdiff
path: root/chess-perft.el
diff options
context:
space:
mode:
authorMario Lang <mlang@delysid.org>2014-04-17 13:13:46 +0200
committerMario Lang <mlang@delysid.org>2014-04-17 13:13:46 +0200
commit351eeb1d040cd1b919845e3161ba1558fe2d126e (patch)
tree40b6c8b9a4c1a3a34ed67d0cfe571dfac0b136f8 /chess-perft.el
parent6f61ce560a5c6fc1e8845640e7b073000d181e9e (diff)
chess-perft.el: move generator testing.
Diffstat (limited to 'chess-perft.el')
-rw-r--r--chess-perft.el74
1 files changed, 74 insertions, 0 deletions
diff --git a/chess-perft.el b/chess-perft.el
new file mode 100644
index 0000000..d249b2d
--- /dev/null
+++ b/chess-perft.el
@@ -0,0 +1,74 @@
+;;; chess-perft.el --- Perft tests for emacs-chess -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2014 Mario Lang
+
+;; Author: Mario Lang <mlang@delysid.org>
+;; Keywords: games
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Counts all leave nodes to a certain depth.
+
+;;; Code:
+
+(require 'chess-fen)
+(require 'chess-ply)
+(require 'chess-pos)
+(require 'ert)
+
+(defun chess-perft (position depth)
+ (if (= depth 1)
+ (length (chess-legal-plies position :color (chess-pos-side-to-move position)))
+ (let ((nodes 0))
+ (dolist (ply
+ (chess-legal-plies position
+ :color (chess-pos-side-to-move position))
+ nodes)
+ (setq nodes (+ nodes (chess-perft (chess-ply-next-pos ply)
+ (1- depth))))))))
+
+(ert-deftest chess-perft-1 ()
+ (should (= (chess-perft (chess-pos-create) 1) 20)))
+
+(ert-deftest chess-perft-2 ()
+ (should (= (chess-perft (chess-pos-create) 2) 400)))
+
+(ert-deftest chess-perft-3 ()
+ (should (= (chess-perft (chess-pos-create) 3) 8902)))
+
+(ert-deftest chess-perft-4 ()
+ (should (= (chess-perft (chess-pos-create) 4) 197281)))
+
+(ert-deftest chess-perft-5 ()
+ (should (= (chess-perft (chess-pos-create) 5) 4865609)))
+
+(ert-deftest chess-perft-kiwipete-1 ()
+ (let ((position (chess-fen-to-pos
+ "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -")))
+ (should (= (chess-perft position 1) 48))))
+
+(ert-deftest chess-perft-kiwipete-2 ()
+ (let ((position (chess-fen-to-pos
+ "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -")))
+ (should (= (chess-perft position 2) 2039))))
+
+(ert-deftest chess-perft-kiwipete-3 ()
+ (let ((position (chess-fen-to-pos
+ "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -")))
+ (should (= (chess-perft position 3) 97862))))
+
+(provide 'chess-perft)
+;;; chess-perft.el ends here