Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

ChessMarkup

Summary: Display chessboards in arbitrary positions
Version: 20160214
Status: Stable
Prerequisites: 2.2.58
Maintainer: Pm
Discussion: ChessMarkup-Talk
Categories: Markup, PHP55
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..
pp..pp..
p.p..np.
........
..NP....
..N.....
PPP..PPP
R..BQKB.

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..bqkbn
pp..p..p
ppn..p..
........
..NP....
........
PPP..PPP
RNBQKB..

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.2.83
  • This recipe requires at least PmWiki version 2.2.58

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].
  • Petko, update for PHP 5.5

Comments

See discussion at ChessMarkup-Talk

User notes? : If you use, used or reviewed this recipe, you can add your name. 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 February 13, 2016, at 05:56 PM