summaryrefslogtreecommitdiff
path: root/chess-ply.el
diff options
context:
space:
mode:
authorErik Peldan <epe@trifork.com>2017-01-22 16:53:08 +0100
committerErik Peldan <epe@trifork.com>2017-01-22 16:53:08 +0100
commit03f961a1d8bb9178ebe9a399eacfed3ade36a80c (patch)
treefc219360d631b43f81705c1f508a9126fd219409 /chess-ply.el
parent4b7f4e525d07e435e3aa1590f0286f30d2ce65a8 (diff)
Ask user for promotion piece when applicable
Diffstat (limited to 'chess-ply.el')
-rw-r--r--chess-ply.el28
1 files changed, 27 insertions, 1 deletions
diff --git a/chess-ply.el b/chess-ply.el
index 26a832c..dfae21f 100644
--- a/chess-ply.el
+++ b/chess-ply.el
@@ -176,6 +176,28 @@
(cl-assert (vectorp position))
(list position))
+
+(defvar promotion-options
+ '(queen rook bishop knight))
+
+(defun ask-promotion (white)
+ (let ((p (ido-completing-read "Symbol? " (mapcar #'symbol-name promotion-options))))
+ (cond ((string= "queen" p)
+ (if white ?Q ?q))
+
+ ((string= "rook" p)
+ (if white ?R ?r))
+
+ ((string= "bishop" p)
+ (if white ?B ?b))
+
+ ((string= "knight" p)
+ (if white ?N ?n))
+
+ ;; Promote to queen as fallback
+ (t (if white ?Q ?q)))))
+
+
(defun chess-ply-create (position &optional valid-p &rest changes)
"Create a ply from the given POSITION by applying the supplied CHANGES.
This function will guarantee the resulting ply is legal, and will also
@@ -223,7 +245,11 @@ maneuver."
(when (and (not (memq :promote changes))
(= (if color 0 7)
(chess-index-rank (cadr changes))))
- (chess-error 'ambiguous-promotion))
+ (let ((promo (ask-promotion color)))
+ (setq changes
+ (append changes (list :promote promo)))
+
+ (setq ply (cons position changes))))
;; is this an en-passant capture?
(when (let ((ep (chess-pos-en-passant position)))