]> code.delx.au - gnu-emacs-elpa/blob - chess-algebraic.el
Coded engines as a separate library. Still work to be done here.
[gnu-emacs-elpa] / chess-algebraic.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Convert a ply to/from standard chess algebraic notation
4 ;;
5 ;; A thing to deal with in chess is algebraic move notation, such as
6 ;; Nxf3+. (I leave description of this notation to better manuals
7 ;; than this). This notation is a shorthand way of representing where
8 ;; a piece is moving from and to, by specifying the piece is involved,
9 ;; where it's going, and whether or not a capture or check is
10 ;; involved.
11 ;;
12 ;; You can convert from algebraic notation to a ply (one pair in most
13 ;; cases, but two for a castle) using the following function (NOTE:
14 ;; POSITION determines which side is on move (by calling
15 ;; `chess-pos-side-to-move')):
16 ;;
17 ;; (chess-algebraic-to-ply POSITION STRING)
18 ;;
19 ;; The function also checks if a move is legal, and will raise an
20 ;; error if not.
21 ;;
22 ;; To convert from a ply to algebraic notation, use:
23 ;;
24 ;; (chess-ply-to-algebraic PLY)
25 ;;
26 ;; Castling is determined by the movement of both a king and a rook.
27 ;;
28 ;; Lastly, there is a regexp for quickly checking if a string is in
29 ;; algebraic notation or not, or searching out algebraic strings in a
30 ;; buffer:
31 ;;
32 ;; chess-algebraic-regexp
33
34 ;; $Revision$
35
36 (require 'chess-ply)
37
38 (defconst chess-algebraic-pieces-regexp "[RNBKQ]")
39
40 (defconst chess-algebraic-regexp
41 (format (concat "\\("
42 "O-O\\(-O\\)?\\|"
43 "\\(%s?\\(\\([a-h]\\|[1-8]\\)?\\|[a-h][1-8]\\)\\)?"
44 "\\([x-]\\)?"
45 "\\([a-h][1-8]\\)"
46 "\\(=\\(%s\\)\\)?"
47 "\\)"
48 "\\([#+]\\)?")
49 chess-algebraic-pieces-regexp
50 chess-algebraic-pieces-regexp)
51 "A regular expression that matches all possible algebraic moves.
52 This regexp handles both long and short form.")
53
54 (defun chess-algebraic-to-ply (position move &optional search-func)
55 "Convert the algebraic notation MOVE for POSITION to a ply."
56 (when (string-match chess-algebraic-regexp move)
57 (let* ((color (chess-pos-side-to-move position))
58 (mate (match-string 10 move))
59 (piece (aref move 0))
60 (changes
61 (if (eq piece ?O)
62 (let ((rank (if color 7 0))
63 (long (= (length (match-string 1 move)) 5)))
64 (list (chess-rf-to-index rank 4)
65 (chess-rf-to-index rank (if long 2 6))
66 (chess-rf-to-index rank (if long 0 7))
67 (chess-rf-to-index rank (if long 3 5))))
68 (let ((source (match-string 4 move))
69 (target (chess-coord-to-index (match-string 7 move))))
70 (if (and source (= (length source) 2))
71 (list (chess-coord-to-index source) target)
72 (let (candidates which)
73 (unless (< piece ?a)
74 (setq piece ?P))
75 ;; we must use our knowledge of how pieces can
76 ;; move, to determine which piece is meant by the
77 ;; piece indicator
78 (when (setq candidates
79 (funcall (or search-func
80 'chess-standard-search-position)
81 position target (if color piece
82 (downcase piece))))
83 (if (= (length candidates) 1)
84 (list (car candidates) target)
85 (if (null source)
86 (error "Clarify piece to move by rank or file")
87 (while candidates
88 (if (if (>= source ?a)
89 (eq (cdar candidates) (- source ?a))
90 (eq (caar candidates) (- 7 (- source ?1))))
91 (setq which (car candidates) candidates nil)
92 (setq candidates (cdr candidates))))
93 (if (null which)
94 (error "Could not determine which piece to use")
95 (list which target)))))))))))
96 (if mate
97 (nconc changes
98 (list (if (equal mate "#")
99 ':checkmate
100 ':check))))
101 (assert changes)
102 (apply 'chess-ply-create position changes))))
103
104 (defun chess-ply-to-algebraic (ply &optional long search-func)
105 "Convert the given PLY to algebraic notation.
106 If LONG is non-nil, render the move into long notation."
107 (if (null (car (chess-ply-changes ply)))
108 ""
109 (let* ((pos (chess-ply-pos ply))
110 (changes (chess-ply-changes ply))
111 (from (car changes))
112 (to (cadr changes))
113 (from-piece (chess-pos-piece pos from))
114 (color (chess-pos-side-to-move pos)) str
115 (notation
116 (if (setq str
117 (and (= (upcase from-piece) ?K)
118 (= from (chess-rf-to-index (if color 7 0) 4))
119 (if (= to (chess-rf-to-index (if color 7 0) 6))
120 "O-O"
121 (if (= to (chess-rf-to-index (if color 7 0) 2))
122 "O-O-O"))))
123 str
124 (let ((candidates (funcall (or search-func
125 'chess-standard-search-position)
126 pos to from-piece))
127 (rank 0) (file 0)
128 (from-rank (/ from 8))
129 (from-file (mod from 8))
130 differentiator notation)
131 (when (> (length candidates) 1)
132 (dolist (candidate candidates)
133 (if (= (/ candidate 8) from-rank)
134 (setq rank (1+ rank)))
135 (if (= (mod candidate 8) from-file)
136 (setq file (1+ file))))
137 (cond
138 ((= file 1)
139 (setq differentiator (+ from-file ?a)))
140 ((= rank 1)
141 (setq differentiator (+ (- 7 from-rank) ?1)))
142 (t (error "Could not differentiate piece"))))
143 (concat
144 (unless (= (upcase from-piece) ?P)
145 (char-to-string (upcase from-piece)))
146 (if long
147 (chess-index-to-coord from)
148 (if differentiator
149 (char-to-string differentiator)
150 (if (and (not long) (= (upcase from-piece) ?P)
151 (/= (chess-index-file from)
152 (chess-index-file to)))
153 (char-to-string (+ (chess-index-file from) ?a)))))
154 (if (/= ? (chess-pos-piece pos to))
155 "x" (if long "-"))
156 (chess-index-to-coord to)
157 (let ((promote (memq ':promote changes)))
158 (if promote
159 (concat "=" (char-to-string (cadr promote))))))))))
160 (concat notation
161 (if (memq ':check changes) "+"
162 (if (memq ':checkmate changes) "#"))))))
163
164 (provide 'chess-algebraic)
165
166 ;;; chess-algebraic.el ends here