Chess: Validating moves and commentary using XSLT.
After reading Michael Kay's Knights Tour (XSLT 2nd
Edition), Max Froumentin's An XSLT stylesheet for
and attempting The Times Winning Move over a couple of pints with a chess playing friend.
I wanted to validate chess moves using XSLT and create an interactive chess board to input
moves using SVG. Here is the work in progress, there is plenty of room for improvement some of which would be focused towards increasing the speed of execution.
Requires MSIE (6.0), the Adobe SVG plug-in
Chess.htm: Allows user to move both White and Black pieces in accordance to the rules of
chess. By default, when a piece is selected those squares to which the piece can
move are highlighted blue, if a capture is possible then the square is
highlighted in green.
Problem.htm: An interface to a set of chess problems. Allows the
user to solve selected chess problem with undo move functionality and move commentary.
What it does
Allows user to move both White and Black pieces in accordance to the rules of chess. By default, when
a piece is selected those squares to which the piece can move are highlighted blue, if a capture is possible
then the square is highlighted in green.
By selecting the radio button All possible moves and
pressing button Set Parameters, all the squares that it is possible for the active player to move to are highlighted as above.
Checking checkbox Include covering own pieces and setting parameter causes the pieces covered by
the selected piece or any of focus players pieces are highlighted red.
If prior to making first move the checkbox Show Commentary is checked, on completion
of move the commentary of move is shown (this slows the process down).
- makechess.svg: the SVG defination of empty board and pieces (these are hidden).
- game.xml: a one element definition of a chess board. This will be removed later, used
- commentary.xsl: produces commentary of moves in html
- chess.xsl: the application engine. Validates moves, produces commentary and instructions
to update SVG Chess board.
How it does it
Onload a reference to the embedded makechess.svg is made and the files game.xml, commentary.xsl and chess.xsl and loaded in to DOMDocument objects.
Initialisation of parameters in chess.xsl are set, game.xml and chess.xsl are then transformed, the
result is loaded into the existing game.xml DOMDocument. The resulting XML has three elements representing
When the active player clicks on one of his pieces the game.xml and chess.xsl
DOMDocuments are again transformed and cached in the game.xml DOMDocument (this process is repeated throughout).
The resulting XML has the same three elements
- A Moves element (at this time this element has no child elements)
- A Squares element, a representation of the current state of the board
- A Validation element, this element has attribute go indicating whose go it is and has
childnodes that represent instructions needed to update the SVG chess board (at this point making
the pieces visible on the correct square)
The active player then selects a destination square by either clicking on a square or piece, the transformation
process is repeated. If the move was illegal then the Validation element will have an
error attribute and instructions to clean board. The Moves and Squares elements remain
- the Moves element is unchanged
- the Squares element is unchanged
- the Validation element has a nodelist of instrucions used to update the board (colouring squares
as described above) and a nodelist representing all valid moves allowed by the active piece.
If the move is a valid one then
The game.xml and commentary.xsl are transformed to output the commentary in HTML.
- a move node is appended to the Moves element
- the Squares element childnodes are updated to represent the new board view
- the Validation element go attribute represents the new active player and has
instruction childnodes that clean the board and update pieces.
Thanks to Max Froumentin for allowing me to use the SVG chess font
(Chess Sophia), without
which I would never have started
and for his advice and encouraging words.
Information on this Site may contain inaccuracies - start positions, solutions, commentary etc.
This site has not reviewed any or all of the web sites linked to this Site and
is not responsible for the content of any off-site pages or any other web sites
linked to this Site. It is the user's
responsibility to take precautions to ensure that whatever is selected is free
of such items as viruses, worms, Trojan horses and other items of a destructive
The ChessGML/SVG stylesheets are released under the W3C
Software Notice and License.
Bugs and comments welcome at email@example.com