summaryrefslogtreecommitdiff
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
parent45011d157958aaf9a04047dcb1c05f7a3b43da10 (diff)
chess-uci: Choose a (random) move from the book.
-rw-r--r--chess-polyglot.el19
-rw-r--r--chess-uci.el12
2 files changed, 25 insertions, 6 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.")
diff --git a/chess-uci.el b/chess-uci.el
index 2413cd8..ad6852c 100644
--- a/chess-uci.el
+++ b/chess-uci.el
@@ -119,13 +119,13 @@ If conversion fails, this function fired an 'illegal event."
(chess-game-set-tag game "White" chess-full-name)
(chess-game-set-tag game "Black" chess-engine-opponent-name))
- (let ((book-plies (and chess-uci-book (bufferp chess-uci-book)
- (buffer-live-p chess-uci-book)
- (chess-polyglot-book-plies chess-uci-book
- (chess-game-pos game)))))
- (if book-plies
+ (let ((book-ply (and chess-uci-book (bufferp chess-uci-book)
+ (buffer-live-p chess-uci-book)
+ (chess-polyglot-book-ply chess-uci-book
+ (chess-game-pos game)))))
+ (if book-ply
(let ((chess-display-handling-event nil))
- (funcall chess-engine-response-handler 'move (car book-plies)))
+ (funcall chess-engine-response-handler 'move book-ply))
(chess-engine-send nil (concat (chess-uci-position game) "go\n"))))
(if (chess-game-over-p game)