]> code.delx.au - gnu-emacs-elpa/blob - chess-var.el
Release 2.0.4
[gnu-emacs-elpa] / chess-var.el
1 ;;; chess-var.el --- Manipulate variations
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; This is free software; you can redistribute it and/or modify it under
6 ;; the terms of the GNU General Public License as published by the Free
7 ;; Software Foundation; either version 3, or (at your option) any later
8 ;; version.
9 ;;
10 ;; This is distributed in the hope that it will be useful, but WITHOUT
11 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 ;; for more details.
14 ;;
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
17
18 ;;; Commentary:
19
20 ;; A chess variations is a simple list of plies. This module provides
21 ;; an abstraction layer for applications.
22
23 ;;; Code:
24
25 (require 'chess-algebraic)
26 (require 'chess-ply)
27 (eval-when-compile (require 'cl-lib))
28
29 (defsubst chess-var-plies (var)
30 "Return the plies of VAR."
31 (cl-assert var)
32 var)
33
34 (defsubst chess-var-pos (var &optional index)
35 "Return the position related to VAR's INDEX ply."
36 (cl-assert var)
37 (chess-ply-pos (chess-var-ply var index)))
38
39 (defsubst chess-var-index (var)
40 "Return the VAR's current position index."
41 (cl-assert var)
42 (1- (length (chess-var-plies var))))
43
44 (defsubst chess-var-seq (var)
45 "Return the current VAR sequence."
46 (cl-assert var)
47 (1+ (/ (chess-var-index var) 2)))
48
49 (defsubst chess-var-side-to-move (var &optional index)
50 "Return the color whose move it is in VAR at INDEX (or at the last position
51 of the variation if INDEX is nil)."
52 (cl-assert var)
53 (chess-pos-side-to-move (chess-var-pos var index)))
54
55 (defun chess-var-ply (var &optional index)
56 "Return VAR's INDEXth ply."
57 (cl-assert var)
58 (if index
59 (nth index (chess-var-plies var))
60 (car (last (chess-var-plies var)))))
61
62 (defun chess-var-add-ply (var ply)
63 "Add to VAR the given PLY."
64 (cl-assert var)
65 (cl-assert (listp ply))
66 (let ((plies (chess-var-plies var)))
67 (cl-assert plies)
68 (nconc plies (list ply))))
69
70 (defsubst chess-var-create (&optional position)
71 "Create a new chess variation object.
72 Optionally use the given starting POSITION."
73 (list (chess-ply-create* (or position chess-starting-position))))
74
75 (defun chess-var-move (var ply)
76 "Make a move in the current VAR by applying the changes of PLY.
77 This creates a new position and adds it to the main variation.
78 The 'changes' of the last ply reflect whether the var is currently in
79 progress (nil), if it is drawn, resigned, mate, etc."
80 (cl-assert var)
81 (cl-assert (listp ply))
82 (let ((current-ply (chess-var-ply var))
83 (changes (chess-ply-changes ply)))
84 (if (chess-ply-final-p current-ply)
85 (chess-error 'add-to-completed))
86 (cl-assert (eq (chess-ply-pos ply) (chess-ply-pos current-ply)))
87 (chess-ply-set-changes current-ply changes)
88 (chess-var-add-ply var (chess-ply-create*
89 (chess-ply-next-pos current-ply)))))
90
91 (defun chess-var-to-algebraic (var &optional type)
92 "Reveal the plies of VAR by converting them to algebraic notation.
93 Optional argument TYPE defines the type of algebraic notation to use
94 (`:san', `:lan' or `:fan'."
95 (mapconcat (lambda (ply)
96 (chess-ply-to-algebraic ply type))
97 (if (chess-ply-final-p (chess-var-ply var))
98 (chess-var-plies var)
99 (butlast (chess-var-plies var)))
100 " "))
101
102 (provide 'chess-var)
103
104 ;;; chess-var.el ends here