]> code.delx.au - gnu-emacs-elpa/blobdiff - chess-algebraic.el
added castling keywords
[gnu-emacs-elpa] / chess-algebraic.el
index cd67c6ace6ecf42a0459865e7af9ec48bd2456dc..737a4d0d3606f3a0d7fbbfb67f09306530606266 100644 (file)
@@ -51,7 +51,7 @@
   "A regular expression that matches all possible algebraic moves.
 This regexp handles both long and short form.")
 
-(defun chess-algebraic-to-ply (position move &optional search-func)
+(defun chess-algebraic-to-ply (position move)
   "Convert the algebraic notation MOVE for POSITION to a ply."
   (unless (string-match chess-algebraic-regexp move)
     (error "Cannot parse non-algebraic move notation: %s" move))
@@ -63,9 +63,7 @@ This regexp handles both long and short form.")
              (let ((rank (if color 7 0))
                    (long (= (length (match-string 1 move)) 5)))
                (list (chess-rf-to-index rank 4)
-                     (chess-rf-to-index rank (if long 2 6))
-                     (chess-rf-to-index rank (if long 0 7))
-                     (chess-rf-to-index rank (if long 3 5))))
+                     (chess-rf-to-index rank (if long 2 6))))
            (let ((source (match-string 4 move))
                  (target (chess-coord-to-index (match-string 7 move))))
              (if (and source (= (length source) 2))
@@ -80,10 +78,9 @@ This regexp handles both long and short form.")
                  ;; move, to determine which piece is meant by the
                  ;; piece indicator
                  (when (setq candidates
-                             (funcall (or search-func
-                                          'chess-standard-search-position)
-                                      position target (if color piece
-                                                        (downcase piece))))
+                             (chess-search-position position target
+                                                    (if color piece
+                                                      (downcase piece))))
                    (if (= (length candidates) 1)
                        (list (car candidates) target)
                      (if (null source)
@@ -102,12 +99,11 @@ This regexp handles both long and short form.")
     (if mate
        (nconc changes
               (list (if (equal mate "#")
-                        ':checkmate
-                      ':check))))
-    (assert changes)
-    (apply 'chess-ply-create position changes)))
+                        :checkmate
+                      :check))))
+    (and changes (apply 'chess-ply-create position changes))))
 
-(defun chess-ply-to-algebraic (ply &optional long search-func)
+(defun chess-ply-to-algebraic (ply &optional long)
   "Convert the given PLY to algebraic notation.
 If LONG is non-nil, render the move into long notation."
   (if (null (car (chess-ply-changes ply)))
@@ -120,16 +116,10 @@ If LONG is non-nil, render the move into long notation."
           (color (chess-pos-side-to-move pos)) str
           (notation
            (if (setq str
-                     (and (= (upcase from-piece) ?K)
-                          (= from (chess-rf-to-index (if color 7 0) 4))
-                          (if (= to (chess-rf-to-index (if color 7 0) 6))
-                              "O-O"
-                            (if (= to (chess-rf-to-index (if color 7 0) 2))
-                                "O-O-O"))))
+                     (if (memq :castle changes) "O-O"
+                       (if (memq :long-castle changes) "O-O-O")))
                str
-             (let ((candidates (funcall (or search-func
-                                            'chess-standard-search-position)
-                                        pos to from-piece))
+             (let ((candidates (chess-search-position pos to from-piece))
                    (rank 0) (file 0)
                    (from-rank (/ from 8))
                    (from-file (mod from 8))
@@ -160,12 +150,12 @@ If LONG is non-nil, render the move into long notation."
                 (if (/= ?  (chess-pos-piece pos to))
                     "x" (if long "-"))
                 (chess-index-to-coord to)
-                (let ((promote (memq ':promote changes)))
+                (let ((promote (memq :promote changes)))
                   (if promote
                       (concat "=" (char-to-string (cadr promote))))))))))
       (concat notation
-             (if (memq ':check changes) "+"
-               (if (memq ':checkmate changes) "#"))))))
+             (if (memq :check changes) "+"
+               (if (memq :checkmate changes) "#"))))))
 
 (provide 'chess-algebraic)