![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module uses TemplateStyles: |
Implements {{Chess diagram}}.
Usage
{{#invoke:Chessboard|function_name}}
function chessboard( fen, size, reverse )
local piecenames = { p = 'Pawn', r = 'Rook', n = 'Knight', b = 'Bishop', q = 'Queen', k = 'King' }
local colornames = { l = 'White', d = 'Black' }
function rowchar( row ) return 9 - row end
function filechar( file ) return ( "abcdefgh" ):sub( file, file ) end
function coord( ind ) return ( reverse and ( 8 - ind ) * size ) or ( ind - 1 ) * size end
function piecediv( piece, row, file, res )
local color = piece:match( '%u' ) and 'l' or 'd'
piece = piece:lower()
local alt = string.format("%s%s %s %s", filechar( file ), rowchar( row ), colornames[color], piecenames[piece] or piece)
local img = string.format('[[File:Chess %s%st45.svg|%dx%dpx|alt=%s|%s]]', piece, color, size, size, alt, alt)
table.insert( res, string.format('<div style="position:absolute;z-index:3;top:%dpx;left:%dpx;">%s</div>', coord( row ), coord( file ), img) )
end
function oneRow( s, row, res )
local file = 1
for piece in s:gmatch( "%w" ) do -- if a digit, increment "file" by the digit. else, add the piece _and_ increment file by 1
file = file + ( piece:match("%d") or piecediv( piece, row, file, res ) or 1 )
end
end
local result = {}
table.insert(result, string.format([=[
<div class="chess-fen" style="position:relative;">
[[File:Chessboard480.png|%dx%dpx|link=]]
]=], size * 8, size * 8))
local row = 0
for srow in string.gmatch("/" .. fen, "/%w+") do
row = row + 1
oneRow(srow, row, result)
end
table.insert(result, '</div>')
return result
end
return {
f = function( frame )
local args = frame.args
local t = chessboard( args.fen, args.size or 30, ( args.reverse or '' ):lower() == "true" )
return table.concat( t, "\n" )
end
}