]> code.delx.au - gnu-emacs-elpa/blob - packages/chess/chess-var.el
Initial import of chess.el.
[gnu-emacs-elpa] / packages / chess / chess-var.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Manipulate variations (which are really just lists of plies)
4 ;;
5
6 ;;; Commentary:
7
8 ;; A chess variations is a simple list of plies. This module provides
9 ;; an abstraction layer for applications.
10
11 (require 'chess-ply)
12 (eval-when-compile (require 'cl))
13
14 (defsubst chess-var-plies (var)
15 "Return the plies of VAR."
16 (assert var)
17 var)
18
19 (defsubst chess-var-pos (var &optional index)
20 "Return the position related to VAR's INDEX ply."
21 (assert var)
22 (chess-ply-pos (chess-var-ply var index)))
23
24 (defsubst chess-var-index (var)
25 "Return the VAR's current position index."
26 (assert var)
27 (1- (length (chess-var-plies var))))
28
29 (defsubst chess-var-seq (var)
30 "Return the current VAR sequence."
31 (assert var)
32 (1+ (/ (chess-var-index var) 2)))
33
34 (defsubst chess-var-side-to-move (var &optional index)
35 "Return the color whose move it is in VAR at INDEX (or at the last position
36 of the variation if INDEX is nil)."
37 (assert var)
38 (chess-pos-side-to-move (chess-var-pos var index)))
39
40 (defun chess-var-ply (var &optional index)
41 "Return VAR's INDEXth ply."
42 (assert var)
43 (if index
44 (nth index (chess-var-plies var))
45 (car (last (chess-var-plies var)))))
46
47 (defun chess-var-add-ply (var ply)
48 "Return the position related to VAR's INDEX position."
49 (assert var)
50 (assert (listp ply))
51 (let ((plies (chess-var-plies var)))
52 (assert plies)
53 (nconc plies (list ply))))
54
55 (defsubst chess-var-create (&optional position)
56 "Create a new chess variation object.
57 Optionally use the given starting POSITION."
58 (list (chess-ply-create* (or position chess-starting-position))))
59
60 (defun chess-var-move (var ply)
61 "Make a move in the current VAR by applying the changes of PLY.
62 This creates a new position and adds it to the main variation.
63 The 'changes' of the last ply reflect whether the var is currently in
64 progress (nil), if it is drawn, resigned, mate, etc."
65 (assert var)
66 (assert (listp ply))
67 (let ((current-ply (chess-var-ply var))
68 (changes (chess-ply-changes ply))
69 (position (chess-ply-pos ply)))
70 (if (chess-ply-final-p current-ply)
71 (chess-error 'add-to-completed))
72 (assert (eq position (chess-ply-pos current-ply)))
73 (chess-ply-set-changes current-ply changes)
74 (chess-var-add-ply var (chess-ply-create*
75 (chess-ply-next-pos current-ply)))))
76
77 (defun chess-var-to-algebraic (var &optional long)
78 "Reveal the plies of VAR by converting them to algebraic
79 notation."
80 (mapconcat (lambda (ply)
81 (chess-ply-to-algebraic ply long))
82 (if (chess-ply-final-p (chess-var-ply var))
83 (chess-var-plies var)
84 (reverse (cdr (reverse (chess-var-plies var)))))
85 " "))
86
87 (provide 'chess-var)
88
89 ;;; chess-var.el ends here