Module:Chessboard mxn/Sandbox: Difference between revisions

Content deleted Content added
Rewrite FEN converter to allow multi digit numbers
OK, string.match isn’t flexible enough. We have to match only the head of the string, so we have to work with string.find() and string.sub() instead
Line 133:
-- Loop over rows, which are delimited by /
for srow in string.gmatch("/" .. fen, "/%w+") do
srow = srow:gsub("/","")
srow = srow:gsub("%s*","")
print(srow) -- DEBUG
-- Loop over all letters and numbers in the row
-- Since Lua regexes do not have the | operator, we have
Line 138 ⟶ 141:
local index = 1
local piece = "" -- Piece can also be empty squares
local place = 0
local pstart = 0
local pend = 0
local length = srow:len()
while index < length do
-- Look for a number. Can have multiple digits
piece pstart, pend = srow:matchfind("%d+", index)
if piece ~= nil andif piece:len()pstart >== 0index then
index = index + piece = srow:lensub(pstart, pend)
for k index =1,tonumber(piece) dopend + 1
table.insert(res,' ' for k=1,tonumber(piece) do
end table.insert(res,' ')
else end
else
-- If number not found, look for a letter (piece on board)
piece pstart = srow:matchfind("%a",index)
if piece ~= nil and piece:len() > 0 if pstart == index then
index = index + piece = srow:lensub(pstart, pstart)
-- l: White (light); d: Black (dark)
local color = piece:match( '%u' ) and 'l' or 'd' index = pstart + 1
local color = piece:match( '%u' ) and 'l' or 'd'
piece = piece:lower()
table.insert(res, piece .. color)
else
index = length + 1 -- Break loop
end
end
end
end
 
return res
end
 
 
 
function p.board(frame)