You are browsing a read-only backup copy of Wikitech. The primary site can be found at wikitech.wikimedia.org

Module:Chess-from-fen

From Wikitech-static
Jump to navigation Jump to search

Documentation for this module may be created at Module:Chess-from-fen/doc (Test casesTest results)

local p = {}

p.f = function(frame) 
    local fen,size,reverse
    
	function coord(ind) 
		return (reverse and (7 - ind) * size) or ind * size
	end
	
	function colBool(bool)
		return (bool and 'l') or 'd'
	end
	
	function piecepict(file,row,piece)
		local squareColor = colBool( (file + row) % 2 == 0 )
		local pieceColor = colBool(piece == string.upper(piece))
		return string.format('[[File:Chess %s%st45.svg|%dx%dpx|link=]]', string.lower(piece), pieceColor, size, size) 
	end
	
	function oneRow(s, row) 
		local file = 0
		local result = ''
		for piece in string.gmatch(s, "%\w") do
			if string.match(piece, "%\d") then 
				file = file + piece
			else 
				result = result .. string.format('<div style="top:%dpx;left:%dpx;">%s</div>\n', coord(row), coord(file), piecepict(file,row,piece))
				file = file + 1
			end
		end
		return result
	end
	
	local row = 0;
	fen = frame.args['fen']
	size = frame.args['size'] or 30
	reverse = string.lower(frame.args['reverse'] or '') == "true"
	local result = string.format('<div class="chess-fen" style="min-width:%dpx;min-height:%dpx;">', size*8,size*8)
	result = result .. string.format('<div style="z-index:0; top:0px; left:0px;">[[File:Chessboard480.png|%dx%dpx]]</div>', size * 8, size * 8)
	for srow in string.gmatch("/" .. fen, "/%w+") do
		result = result .. oneRow(srow, row)
		row = row + 1
	end
	result = result .. '</div>'
	return result
end

return p