Zum Inhalt springen

Modul:Schachbrett

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 3. März 2013 um 05:43 Uhr durch קיפודנחש (Diskussion | Beiträge) (more lua-like). Sie kann sich erheblich von der aktuellen Version unterscheiden.

Die Dokumentation für dieses Modul kann unter Modul:Schachbrett/Doku erstellt werden

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 piecediv( piece, row, file, res )
    	function rowchar() return 8 - row end
		function filechar() return ( "abcdefgh" ):sub( file + 1, file + 1 ) end
		function coord( ind ) return ( reverse and ( 7 - ind ) * size ) or ind * size end
		
        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) )
		return 1
    end

    function oneRow( s, row, res )
        local file = 0
        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 ) )
        end
    end

    local result = {}
	local s8 = size * 8
	table.insert(result, string.format([=[
<div class="chess-fen" style="position:relative;">
[[File:Chessboard480.png|%dx%dpx|link=]]
	]=], s8, s8))

    local row = 0
    for srow in string.gmatch("/" .. fen, "/%w+") do
        oneRow(srow, row, result)
        row = row + 1
    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() == "reverse" )
		return table.concat( t, "\n" )
	end
}