Module:Chessboard: Difference between revisions

Content deleted Content added
No edit summary
more lua-like
Line 1:
function chessboard( fen, size, reverse )
local p = {
local piecenames = { p = 'Pawn', r = 'Rook', n = 'Knight', b = 'Bishop', q = 'Queen', k = 'King' },
local colornames = { l = 'White', d = 'Black' }
 
p.f = function(frame)
local fen = frame.args['fen']
local size = frame.args['size'] or 30
local reverse = string.lower(frame.args['reverse'] or '') == "true"
 
function rownumFromRowpiecediv( piece, row, file, res )
function rowchar() return 8 - row end
function filechar() return string.sub( "abcdefgh", ):sub( file + 1, file + 1 ) end
end
function coord( ind ) return ( reverse and ( 7 - ind ) * size ) or ind * size end
function filecharFromFile(file)
local pieceColorcolor = string.piece:match(piece, '%u' ) and 'l' or 'd'
return string.sub("abcdefgh", file + 1, file + 1)
piece = string.piece:lower(piece)
end
local alt = string.format("%s%s %s %s", filecharFromFilefilechar( file ), rownumFromRowrowchar( row ), p.colornames[pieceColorcolor], p.piecenames[piece] or piece)
local img = string.format('[[File:Chess %s%st45.svg|%dx%dpx|alt=%s|%s]]', piece, pieceColorcolor, size, size, alt, alt)
function coord(ind)
return table.insert( res, string.format('<div style="position:absolute;z-index:3;top:%dpx;left:%dpx;">%s</div>\n', coord(row), coord(file), img) )
return (reverse and (7 - ind) * size) or ind * size
return result1
end
end
 
function piecedivoneRow(piece s, row, fileres )
local file = 0
local pieceColor = string.match(piece, '%u') and 'l' or 'd'
for piece in s:gmatch( "%w" ) do -- if a digit, increment "file" by the digit. else, add the piece _and_ increment file by 1
piece = string.lower(piece)
file = file + ( piece:match("%d") or piecediv( piece, row, file, res ) )
local alt = string.format("%s%s %s %s", filecharFromFile(file), rownumFromRow(row), p.colornames[pieceColor], p.piecenames[piece] or piece)
end
local img = string.format('[[File:Chess %s%st45.svg|%dx%dpx|alt=%s|%s]]', piece, pieceColor, size, size, alt, alt)
end
return string.format('<div style="position:absolute;z-index:3;top:%dpx;left:%dpx;">%s</div>\n', coord(row), coord(file), img)
end
 
local presult = {}
function oneRow(s, row)
local files8 = 0size * 8
table.insert(result, string.format([=[
local result = ''
<div class="chess-fen" style="position:relative;">
for piece in string.gmatch(s, "%\w") do
[[File:Chessboard480.png|%dx%dpx|link=]]
if string.match(piece, "%\d") then
]=], s8, s8))
file = file + piece
else
result = result .. piecediv(piece, row, file)
file = file + 1
end
end
return result
end
 
local row = 0
for piecesrow in string.gmatch(s"/" .. fen, "/%\w+") do
local result = string.format('<div class="chess-fen" style="position:relative;min-width:%dpx;min-height:%dpx;">', size*8,size*8)
result = result .. oneRow(srow, row, result)
result = result .. string.format('<div style="position:absolute;z-index:0; top:0px; left:0px;">[[File:Chessboard480.png|%dx%dpx|link=]]</div>', size * 8, size * 8)
row = row + 1
for srow in string.gmatch("/" .. fen, "/%w+") do
end
result = result .. oneRow(srow, row)
table.insert(result, '</div>')
row = row + 1
return result
end
result = result .. '</div>'
return result
end
 
return p{
p. f = function( frame )
local fenargs = frame.args['fen']
local t = chessboard( args.fen, args.size or 30, ( args.reverse or '' ):lower() == "reverse" )
return table.concat( t, "\n" )
end