summaryrefslogtreecommitdiff
path: root/chess-fen.el
diff options
context:
space:
mode:
Diffstat (limited to 'chess-fen.el')
-rw-r--r--chess-fen.el21
1 files changed, 13 insertions, 8 deletions
diff --git a/chess-fen.el b/chess-fen.el
index 8dca4f6..b104f06 100644
--- a/chess-fen.el
+++ b/chess-fen.el
@@ -46,21 +46,24 @@
(let ((i 0) (l (length fen))
(rank 0) (file 0) (c ?0)
(position (chess-pos-create t))
- number)
- (while (and (/= c ? ) (< i l))
+ error number)
+ (while (and (null error) (/= c ? ) (< i l))
(setq c (aref fen i))
(cond
((= c ?/)
(setq file 0 rank (1+ rank)))
((and (>= c ?1) (<= c ?9))
(setq file (+ file (- c ?0))))
- ((/= c ? )
+ ((memq (upcase c) '(?K ?Q ?B ?N ?R ?P))
(chess-pos-set-piece position (chess-rf-to-index rank file) c)
- (setq file (1+ file))))
+ (setq file (1+ file)))
+ (t (setq error t)))
(setq i (1+ i)))
- (chess-pos-set-side-to-move position (= (aref fen i) ?w))
+ (if (memq (aref fen i) '(?b ?w))
+ (chess-pos-set-side-to-move position (= (aref fen i) ?w))
+ (setq error t))
(setq i (+ i 2) number nil)
- (while (< i l)
+ (while (and (null error) (< i l))
(setq c (aref fen i))
(cond
((= c ?K) (chess-pos-set-can-castle position ?K t))
@@ -70,9 +73,11 @@
((and (>= c ?a) (<= c ?h))
(chess-pos-set-en-passant position (chess-coord-to-index
(substring fen i (+ i 2))))
- (setq i (1+ i))))
+ (setq i (1+ i)))
+ (t (setq error t)))
(setq i (1+ i)))
- position))
+ (unless error
+ position)))
(defun chess-pos-to-fen (position &optional full)
"Convert a chess POSITION to FEN-like notation.