Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

ChessMarkup

Summary: Display chessboards in arbitrary positions
Version: 2009-08-22
Status: Stable
Prerequisites: 2.1.0
Maintainer: Pm
Categories: Markup
Votes:

Description

The chess.zipΔ recipe provides Portable Game Notation PGN markup into PmWiki, and the ability to graphically display the board position after selected moves in a game.

Notes

First, the (:chessboard:) markup can be used to display a simple chessboard:

(:chessboard align=center:)
rnbqkbnr
pppppppp
........
........
........
........
PPPPPPPP
RNBQKBNR

There are options available (described below) for changing the size and color of the resulting chessboard image.

Of course, displaying a chessboard in its starting position isn't all that useful by itself, so this recipe also recognizes Portable Game Notation as wiki markup that internally modifies the chessboard. Thus, an author can write something like

    1. e4 c5                            Sicilian opening
    2. Nf3 d6 3. d4 cxd4 4. Nxd4 Nf6
    5. Nc3 g6                           Dragon variation

and PmWiki treats each of the above PGN entries as moves in a virtual game. The (:chessboard:) markup then displays the state of the current game:

(:chessboard align=center:)

rnbqkb.r
pp..pp.p
...p.np.
........
...NP...
..N.....
PPP..PPP
R.BQKB.R

Following multiple analysis lines

The PGN markup can also be used to investigate alternate lines of play. For example, starting with the Dragon variation listed above, we can write

The move 2...e6 leads to more defensive variations of the Sicilian opening. The Taimanov variation goes 2...e6 3.d4 cxd4 4.Nxd4 Nc6 which leads to: (:chessboard align=center:)

r.bqkbnr
pp.p.ppp
..n.p...
........
...NP...
........
PPP..PPP
RNBQKB.R

Notice how the 2...e6 markup undid the previous moves of the Dragon variation and started a new line of play leading from White's 2.Nf3 move.

Of course, this means that starting from a completely new board position is as simple as writing a new first move:

    1. d4

It's also possible to display a chessboard in a specific position using Forsyth-Edwards Notation. For example, to display a chessboard after white's third move in the classic Ruy Lopez:

(:chessboard r1bqkbnr/pppp1ppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R
align=center:)
r.bqkbnr
pppp.ppp
..n.....
.B..p...
....P...
.....N..
PPPP.PPP
RNBQK..R

The {FEN} markup can be used to get the current game stage as a FEN record.

Installation

To install this recipe, simply download the chess.zipΔ file and unpack it into your base PmWiki directory. The zip archive contains the following files:

    cookbook/chess.php         - PGN and Chessboard: markups
    pub/chess/xx-60.gif        - individual piece images for table-based boards
    pub/chess/chessboard.php   - Script to generate chessboard images as gifs
    pub/chess/pieces-60.gif    - piece image file for chessboard.php script

Once installed, add the following line to a local customization file to enable the chess PGN markups:

    include_once("cookbook/chess.php");
  • This recipe was last tested on PmWiki version 2.1.beta2
  • This recipe requires at least PmWiki version 2.1.beta2

Other notes and comments

Although the PGN markups understand things such as castling (O-O and O-O-O), pawn promotion (14. cxe8=Q), and en-passant moves, it doesn't do a whole lot in the way of enforcing legal moves. So, if an author enters a move that isn't legally valid, the PGN markup goes ahead and processes it anyway.

The (:chessboard:) markup understands the following options:

    width=[width]       width of the chessboard in pixels (default 240)
    height=[height]     height of the chessboard (default same as width)
    class=[class]       table class (default "chesstable")
    align=[center]      table alignment ("left", "right", or "center")
    style=[style]       table CSS styles

In addition, there's also a Chessboard: PmWiki/InterMap shortcut for displaying images as a single gif instead of an HTML table. Normally one does Chessboard:{FEN}?t=.gif, where {FEN} will be replaced by the current board position:

%center% Chessboard:{FEN}?t=.gif

Since the Chessboard: shortcut is just like any other embedded image, it can be styled using things like %rframe% and %rfloat%:


Board after 1.d4

%rframe% Chessboard:{FEN}&light=ffffcc&dark=00bb00&w=160&t=.gif | Board after 1.d4

The board at right shows the initial move for a variety of queen's pawn openings, including the Queen's Gambit.


The tile images for chess pieces are stored in a simple image file, custom piece images can be easily created and configured into the chessboard.php script. The image files distributed with this recipe was built from tile images created by David Benbennick and made available through the Wikimedia commons through a freely-redistributable license.

See Also

Contributors

  • Pm, original recipe, 2005-11-23
  • David Benbennick created the chess piece images and made them available through the Wikimedia commons [1].

Discussion

(Solved) Bug with queenside castling

The recipe prior to version 2006-12-12 didn't recognize the queenside castling (O-O-O) properly. The bug was reported by Bartolin December 12, 2006, at 03:41 AM.

Now fixed, thanks! Pm December 12, 2006, at 07:42 AM

Problem with (:markup:) and (:chessboard:) directives combined

There may be problems if you use both (:markup:) and (:chessboard:) directives -- the chessboards may not display what you expect to see. I guess, that's because everything within (:markup:) environment is interpreted before the "normal" text of wiki pages (which contains your chess moves in PGN format). So: don't use (:chessboard:) directives within (:markup:). For details compare PITS entry 01064.

Bartolin July 08, 2009, at 04:29 PM

Using (:chessboard:) with captions (titles)

I thought it'd be useful to have an option to give a caption (like "white to move") to a position. As noted above that's easy if one uses the Chessboard:{FEN}?t=.gif syntax: It is possible to style the generated image with a style like %rframe% Chessboard:{FEN}&t=.gif | Board after move n .

Using the (:chessboard:) directive I don't see a simple way to provide a caption which remains stuck to the chessboard however this is aligned or styled. Therefore I hacked the script chess.php on my local machine in the following way:

  1. Add the following line to the definition of the function ChessTable($pagename, $args):
    # line 202:
    $caption = $args['caption'];
  2. Add a <caption> tag to the table which defines the chessboard
    # line 230:
    return "<table $tableargs><caption align=bottom>$caption</caption>".Keep(implode('', $out))."</table>";

That makes it possible to use the following syntax:

(:chessboard caption="Board after move x":)

Bartolin


Category:

Sandbox

The remainder of this page is available as a sandbox for experimenting with the chess markups.

After 1. e4 e5 2. Nf3 Nc6, white can then do 3. Nc3, leading to this position.

r.bqkbnr
pppp.ppp
..n.....
....p...
....P...
..N..N..
PPPP.PPP
R.BQKB.R

3... Nf6 4. Nxe5

r.bqkb.r
pppp.ppp
..n..n..
....N...
....P...
..N.....
PPPP.PPP
R.BQKB.R

However, the classic Ruy Lopez opening is 3. Bb5, which leads to

r.bqkbnr
pppp.ppp
..n.....
.B..p...
....P...
.....N..
PPPP.PPP
RNBQK..R

3... a6

r.bqkbnr
.ppp.ppp
p.n.....
.B..p...
....P...
.....N..
PPPP.PPP
RNBQK..R

1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 4. Ba4 Nf6 5. O-O b5 6. Bb3 Be7 7. Re1 O-O 8. d3 d6 9. h3 Na5 10. Nbd2 Nxb3 11. axb3 Ne8 12. Nf1 f5 13. exf5 Bxf5 14. Bd2 Bc8

r.bqnrk.
..p.b.pp
p..p....
.p..p...
........
.P.P.N.P
.PPB.PP.
R..QRNK.

15. N1h2

r.bqnrk.
..p.b.pp
p..p....
.p..p...
........
.P.P.N.P
.PPB.PPN
R..QR.K.

15. ... Bb7 16. Qc1 c5 17. Ng5 Nc7 18. c4 h6 19. Ne4 Qe8 20. f4 exf4 21. Bxf4 Qg6 22. Qd2 Bf6 23. Nxf6+ Rxf6 24. Ng4 Rf7 25. Bxh6 gxh6 26. Nxh6+ Kh7 27. Nxf7 Qxf7 28. Qg5 Ne6 29. Qh4+ Kg8 30. Rxe6 Qxe6 31. Qg3+ Kh7 32. Re1 Qf6 33. Qg4 Rg8 34. Qh5+ Qh6 35. Re7+ Rg7 36. Qf5+ Qg6 37. Rxg7+ Kxg7 38. Qd7+ Qf7 39. Qxd6 1/2-1/2

User notes? : If you use, used or reviewed this recipe, you can add your name. The following format is recognized:
* (+) Optional positive comment. Name, date
* (-) Optional negative comment. Name, date

These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.

Edit - History - Print - Recent Changes - Search
Page last modified on August 21, 2009, at 06:42 PM