1 ;;; chess-var.el --- Manipulate variations
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
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
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
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/>.
20 ;; A chess variations is a simple list of plies. This module provides
21 ;; an abstraction layer for applications.
25 (require 'chess-algebraic)
27 (eval-when-compile (require 'cl-lib))
29 (defsubst chess-var-plies (var)
30 "Return the plies of VAR."
34 (defsubst chess-var-pos (var &optional index)
35 "Return the position related to VAR's INDEX ply."
37 (chess-ply-pos (chess-var-ply var index)))
39 (defsubst chess-var-index (var)
40 "Return the VAR's current position index."
42 (1- (length (chess-var-plies var))))
44 (defsubst chess-var-seq (var)
45 "Return the current VAR sequence."
47 (1+ (/ (chess-var-index var) 2)))
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)."
53 (chess-pos-side-to-move (chess-var-pos var index)))
55 (defun chess-var-ply (var &optional index)
56 "Return VAR's INDEXth ply."
59 (nth index (chess-var-plies var))
60 (car (last (chess-var-plies var)))))
62 (defun chess-var-add-ply (var ply)
63 "Add to VAR the given PLY."
65 (cl-assert (listp ply))
66 (let ((plies (chess-var-plies var)))
68 (nconc plies (list ply))))
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))))
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."
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)))))
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))
99 (butlast (chess-var-plies var)))
104 ;;; chess-var.el ends here