我正在学习Lua,所以我决定尝试实现一个从数组获取坐标并输出ASCII图片的函数。
只要我没有在一行中放置几个点(纵坐标),一切都会很好:
它输出此
XXXX
.X..
..X.
...X
代替这个
create_massive()
经过一番调查,我得出的结论是,除create_massive()
外,所有本地功能都运行良好。当它放置一个点时,先前添加在同一y轴上的点将被删除。
为了澄清,这是我得出的结论:
我只是删除了函数drawing
并自己定义了庞大的local n=nil
drawing={
{1,1,1,1},
{n,1,n,n},
{n,n,1,n},
{n,n,n,1},
}
:
create_massive()
然后程序输出我想要的东西。
我可以只保留函数function draw(coords,sym,spc)
local sym = sym or "X" -- dot
local spc = spc or "-" -- empty slot
local max={} -- massive containing max co-ordinates
local min={} -- massive containing min co-ordinates
local drawing={} -- massive containing drawing
local function find_min_max()
-- finds min/max co-ordinates
for i=1,#coords do
local c=coords[i]
if i%2~=0 then
max.x=max.x and ((c>max.x) and c or max.x) or c
min.x=min.x and ((c<min.x) and c or min.x) or c
else
max.y=min.y and ((c>max.y) and c or max.y) or c
min.y=min.y and ((c<min.y) and c or min.y) or c
end
end
end
local function create_massive()
-- creates massive containing drawing
for i=2,#coords,2 do
local y=coords[i]
local x=coords[i-1]
drawing[y]={[x]=1} -- the thing is, it overwrites previous dots' position, so line can contain only the last called dot
end
end
local function print_drawing()
local n=1
local line={}
for i=min.y,max.y do
if drawing[i] then
for k=min.x,max.x do
if drawing[i][k] then
line[n]=line[n] and line[n]..sym or sym
else
line[n]=line[n] and line[n]..spc or spc
end
if k==max.x then
n=n+1
end
end
else
for p=min.x,max.x do
line[n]=line[n] and line[n]..spc or spc
end
n=n+1
end
end
for i=1,#line do
print(line[i])
end
end
find_min_max()
create_massive() -- probably this function works incorrect
print_drawing()
end
m={1,1,2,1,3,1,4,1,2,2,3,3,4,4} -- co-ordinates: even indexes of the array are y, others are x
draw(m) -- main function is called
,因为只需要对其进行修复,但我认为完整的代码会更有用:
{{1}}
答案 0 :(得分:1)
Codable
使用一个条目创建一个新行。
尝试Data
。