From ace6064ebed234ceaa36d9dd7dd12fdaa7f83a20 Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Wed, 26 Mar 2014 10:52:52 +0100 Subject: chess-uci: Choose a (random) move from the book. --- chess-polyglot.el | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'chess-polyglot.el') 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.") -- cgit v1.2.3