יחידה:Chess from fen

מתוך ויקיספר, אוסף הספרים והמדריכים החופשי
קפיצה לניווט קפיצה לחיפוש

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:Chess from fen/תיעוד

function chessboard( fen, size, reverse )
	local piecenames = { p = 'רגלי', r = 'צריח', n = 'פרש', b = 'רץ', q = 'מלכה', k = 'מלך' }
	local colornames = { l = 'לבן', d = 'שחור' }

	function rowchar( row ) return 9 - row end
	function filechar( file ) return mw.ustring.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 ), piecenames[piece] or piece, colornames[color])
		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
}