Placing the Knight


Placing the Knight

This is a simple function:

  <xsl:function name="tour:place-knight" as="xs:integer*">   <!-- This function places a knight on the board at a given square.   The returned value is the supplied board, modified to indicate   that the knight reached a given square at a given move -->   <xsl:param name="move" as="xs:integer"/>   <xsl:param name="board" as="xs:integer*"/>   <xsl:param name="square" as="xs:integer" /><!-- integer in range 0..63 -->   <xsl:sequence select="   for $i in 1 to 64 return   if ($i = $square + 1) then $move else $board[$i]" />   </xsl:function>  

This function takes three parameters: the number of this move, the current state of the chessboard, and the square on which the knight is to be placed. When it's called from the root template, the move number is always one, and the board is always empty, but I will use the same function again later with different arguments.

What the function does is to copy the whole supplied chessboard before and after the square where the knight is to be placed. This square itself is replaced by the move number. For example, if the tour starts at square a8 (which translates to square zero), then the first call on place-knight () will return a sequence containing a one followed by 63 zeroes.

I can't, of course, modify the supplied chessboard in situ. All variables in XSLT are immutable. Instead I create a new board as a modified copy of the original. The result of the function is a sequence representing the new state of the chessboard after placing the knight.

There are various ways the actual calculation of the new board could have been written here. Another possibility would be:

  <xsl:sequence select="$board[position() = 1 to $square],   $move,   $board[position() = $square+2 to 64]"/>   and a third option would be:<xsl:sequence select="insert-before(   remove($board, $square+1),   $square+1,   $move) "/>  



XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net