summaryrefslogtreecommitdiff
path: root/chess-polyglot.el
diff options
context:
space:
mode:
Diffstat (limited to 'chess-polyglot.el')
-rw-r--r--chess-polyglot.el36
1 files changed, 20 insertions, 16 deletions
diff --git a/chess-polyglot.el b/chess-polyglot.el
index a58ae8d..a9a0551 100644
--- a/chess-polyglot.el
+++ b/chess-polyglot.el
@@ -462,23 +462,27 @@ 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))
+(defun chess-polyglot-book-ply (book position &optional strength)
+ "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.
+If non-nil, STRENGTH defines the bias towards better moves.
+A value below 1.0 will penalize known good moves while a value
+above 1.0 will prefer known good moves. The default is 1.0.
+A strength value of 0.0 will completely ignore move weights and evenly
+distribute the probability that a move gets picked."
+ (unless strength (setq strength 1.0))
+ (cl-assert (and (>= strength 0) (< strength 4)))
+ (cl-flet ((ply-weight (ply)
+ (round (expt (chess-ply-keyword ply :polyglot-book-weight)
+ strength))))
+ (let* ((plies (chess-polyglot-book-plies book position))
+ (random-value (random (cl-reduce #'+ (mapcar #'ply-weight plies))))
+ (max 0) ply)
+ (while plies
+ (if (< random-value (setq max (+ max (ply-weight (car plies)))))
(setq ply (car plies) plies nil)
- (setq plies (cdr plies)))))
- ply))
+ (setq plies (cdr plies))))
+ ply)))
(defalias 'chess-polyglot-book-close 'kill-buffer
"Close a polyglot book.")