Function Lua:返回彩虹文本而不重复相邻的内容
我很难理解为什么下面的函数不能产生我需要的结果。第一个函数生成结果,但不是以我期望的方式。第二个函数生成一个结果,但它的工作方式与第一个函数不同 我想做的是返回一个带有随机颜色代码的字符串,以生成文本的“彩虹”。在过去,我可以用xterm实现这一点,所以我想修改我的原始函数以满足我当前的需要。以下是我尝试过的函数及其结果Function Lua:返回彩虹文本而不重复相邻的内容,function,lua,Function,Lua,我很难理解为什么下面的函数不能产生我需要的结果。第一个函数生成结果,但不是以我期望的方式。第二个函数生成一个结果,但它的工作方式与第一个函数不同 我想做的是返回一个带有随机颜色代码的字符串,以生成文本的“彩虹”。在过去,我可以用xterm实现这一点,所以我想修改我的原始函数以满足我当前的需要。以下是我尝试过的函数及其结果 function rbow(text) local colorTable = {"@w", "@b", "@r", "@g", "@y", "@c", "@m", "@
function rbow(text)
local colorTable = {"@w", "@b", "@r", "@g", "@y", "@c", "@m", "@W", "@B", "@R", "@G", "@Y", "@C", "@M", "@D"}
local rcolor, sbyte, lastColor, rtext = 0, 1, 0, ""
for i = 1, #text do
local tempTable = {}
for _,v in ipairs(colorTable) do
table.insert(tempTable, v)
end
if i > 1 then
table.remove(tempTable, rcolor)
end
math.randomseed(os.time() * string.byte(rcolor) * sbyte)
sbyte = string.byte(i)
rcolor = math.random(1, #tempTable)
rtext = rtext .. tempTable[rcolor] .. text:sub(i,i)
end
return rtext
end
function rbow2(text)
local colorTable = {"@w", "@b", "@r", "@g", "@y", "@c", "@m", "@W", "@B", "@R", "@G", "@Y", "@C", "@M", "@D"}
local rcolor, lastColor, sbyte, rtext = 0, 0, 1, ""
for i = 1, #text do
math.randomseed(os.time() * string.byte(rcolor) * sbyte)
sbyte = string.byte(i)
rcolor = math.random(1, #colorTable)
local colorCheck = function()
if rcolor ~= lastColor then
rtext = rtext .. tempTable[rcolor] .. text:sub(i,i)
else
rcolor = math.random(1, #colorTable)
colorCheck()
end
end
end
return rtext
end
print(rbow("This is a long string to see if there are any repeated colors of letters adjacent to each other."))
> @bT@Gh@Gi@ys@Y @gi@Cs@D @ya@C @wl@bo@bn@bg@b @bs@bt@br@bi@bn@Gg@G @Gt@Go@G @Gs@Ge@Ge@G @Gi@yf@B @ct@Mh@ye@Br@ce@M @ya@Br@Be@B @Ba@Bn@By@B @Br@Be@Bp@Be@Ga@gt@Ye@gd@Y @gc@Yo@gl@Yo@gr@Cs@b @co@gf@C @bl@ce@gt@Ct@be@Rr@Ys@D @Da@Dd@Dj@Da@Dc@De@Dn@yt@G @yt@Go@y @Ge@ya@Gc@yh@G @yo@Ct@yh@Ce@yr@C.
print(rbow2("This is a long string to see if there are any repeated colors of letters adjacent to each other."))
> 0
在第一个示例中,相邻字母由相同的颜色代码表示,例如:
@bo@bn@bg@b @bs@bt@br@bi@bn -- all @b
@G @Gt@Go@G @Gs@Ge@Ge@G @Gi -- all @G
等等。奇怪的是,相同的图案(带有不同的颜色代码)在随后的返回中几乎重复出现。它并不完全匹配,但在三张照片中,“要看”和“相邻”在整个单词中都带有相同的颜色代码
我不确定如何继续进行这项工作。我不想让它在相邻的字母中重复颜色,我本以为第一个函数会解决这个问题,将最近使用的颜色代码从诱惑中删除。我缺少什么?
math.randomseed
应该从循环中移出sbyte=string.byte(I)
给出tostring(I)
本地颜色检查=函数()的第一个字符的ASCII码
是定义递归的错误方法function@EgorSkriptunoff我在循环中使用了math.randomseed
,以完全最大化随机性(因此它会乘以其他两个因子)。然后不确定如何执行递归函数并保留值,嗯。math.randomseed
应该从循环中移出sbyte=string.byte(i)
给出tostring(i)
localcolorcheck=function()的第一个字符的ASCII码
是定义递归的错误方法function@EgorSkriptunoff我在循环中使用了math.randomseed
,以完全最大化随机性(因此它会乘以其他两个因子)。然后不确定如何做递归函数并保留值,嗯。