summaryrefslogtreecommitdiff
path: root/chess-var.el
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2002-04-08 03:37:26 +0000
committerJohn Wiegley <johnw@newartisans.com>2002-04-08 03:37:26 +0000
commitb60fd83dd950c2c5ef04f23f25bf0d25ac9c11db (patch)
treefb1c6e3aced45c08bcd1a3ee173a1c21afeff704 /chess-var.el
parent48b77c46e15e664ffeab0c612418f4505c48f7b8 (diff)
Simplified the code, removed the 'search-function' nonsense. Even the
wackiest chess variants use standard move notation. `chess-ply-create' now fully validates and annotates the plies that it creates, based on the initial piece move (such as the king, in the case of castling).
Diffstat (limited to 'chess-var.el')
-rw-r--r--chess-var.el73
1 files changed, 73 insertions, 0 deletions
diff --git a/chess-var.el b/chess-var.el
new file mode 100644
index 0000000..a3972af
--- /dev/null
+++ b/chess-var.el
@@ -0,0 +1,73 @@
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;; Manipulate variations (which are really just lists of plies)
+;;
+;; $Revision$
+
+;;; Commentary:
+
+;; A chess variations is a simple list of plies. This module provides
+;; an abstraction layer for applications.
+
+(require 'chess-ply)
+
+(defsubst chess-var-plies (var)
+ "Return the tags alist associated with VAR."
+ var)
+
+(defsubst chess-var-pos (var &optional index)
+ "Return the position related to VAR's INDEX position."
+ (chess-ply-pos (chess-var-ply var index)))
+
+(defsubst chess-var-index (var)
+ "Return the VAR's current position index."
+ (1- (length (chess-var-plies var))))
+
+(defsubst chess-var-seq (var)
+ "Return the current VAR sequence."
+ (1+ (/ (chess-var-index var) 2)))
+
+(defsubst chess-var-side-to-move (var)
+ (chess-pos-side-to-move (chess-var-pos var)))
+
+(defun chess-var-ply (var &optional index)
+ "Return the position related to VAR's INDEX position."
+ (if index
+ (nth index (chess-var-plies var))
+ (car (last (chess-var-plies var)))))
+
+(defun chess-var-add-ply (var ply)
+ "Return the position related to VAR's INDEX position."
+ (let ((plies (chess-var-plies var)))
+ (assert plies)
+ (nconc plies (list ply))))
+
+(defsubst chess-var-create (&optional position)
+ "Create a new chess variation object.
+Optionally use the given starting POSITION.
+SEARCH-FUNC specifies the function used to test the legality of moves.
+TAGS is the starting set of var tags (which can always be changed
+later using the various tag-related methods)."
+ (list (chess-ply-create (or position (chess-pos-create)))))
+
+(defun chess-var-move (var ply)
+ "Make a move in the current VAR, from FROM to TO.
+This creates a new position and adds it to the main variation.
+The 'changes' of the last ply reflect whether the var is currently in
+progress (nil), if it is drawn, resigned, mate, etc."
+ (let ((current-ply (chess-var-ply var))
+ (changes (chess-ply-changes ply))
+ (position (chess-ply-pos ply)))
+ (unless (equal position (chess-ply-pos current-ply))
+ (error "Positions do not match"))
+ (unless (chess-search-position
+ position (cadr (chess-ply-changes ply))
+ (chess-pos-piece position (car (chess-ply-changes ply))))
+ (signal 'chess-illegal "Illegal move"))
+ (chess-ply-set-changes current-ply changes)
+ (chess-var-add-ply var (chess-ply-create
+ (chess-ply-next-pos current-ply)))))
+
+(provide 'chess-var)
+
+;;; chess-var.el ends here