summaryrefslogtreecommitdiff
path: root/chess-polyglot.el
diff options
context:
space:
mode:
authorMario Lang <mlang@delysid.org>2014-03-26 10:52:52 +0100
committerMario Lang <mlang@delysid.org>2014-03-26 10:52:52 +0100
commitace6064ebed234ceaa36d9dd7dd12fdaa7f83a20 (patch)
treeec2af79cae498412ae1e41b707daa395570665fd /chess-polyglot.el
parent45011d157958aaf9a04047dcb1c05f7a3b43da10 (diff)
chess-uci: Choose a (random) move from the book.
Diffstat (limited to 'chess-polyglot.el')
-rw-r--r--chess-polyglot.el19
1 files changed, 19 insertions, 0 deletions
diff --git a/chess-polyglot.el b/chess-polyglot.el
index e41bc0f..da66fa9 100644
--- a/chess-polyglot.el
+++ b/chess-polyglot.el
@@ -465,6 +465,25 @@ Use `chess-ply-keyword' on elements of the returned list to retrieve them."
(when ply
(setq plies (nconc plies (list ply))))))))
+(defun chess-polyglot-book-ply (book position)
+ "If non-nil, a (randomly picked) ply from plies in BOOK for POSITION.
+Random Distribution is defined by the relative weights of the found plies."
+ (let* ((plies (chess-polyglot-book-plies book position))
+ (random-value (random (cl-reduce
+ #'+ (mapcar (lambda (ply)
+ (chess-ply-keyword
+ ply :polyglot-book-weight))
+ plies))))
+ (max 0) ply)
+ (while plies
+ (let ((min max))
+ (setq max (+ max (chess-ply-keyword (car plies) :polyglot-book-weight)))
+ (if (and (>= random-value min) (< random-value max))
+ (progn
+ (setq ply (car plies) plies nil))
+ (setq plies (cdr plies)))))
+ ply))
+
(defalias 'chess-polyglot-book-close 'kill-buffer
"Close a polyglot book.")