重复一组代码,但增加价值

时间:2018-04-16 06:14:06

标签: lua

美好的一天。

我正在尝试编写相当长的剧本,我想知道我的生活是否可以变得更容易。

这是我正在写的一个例子:

-- Static Variable
chance = 49.5
nextbet = 0.00000010
base = 0.00000010
base2 = 0.00000010
a1 = 0
stage = 0
bethigh = false
a1b = 0
bigbalance = 0
bigloss = 0
bigbet = 0
counter = 0

function dobet()

counter += 1

--Increase this variable by 1 each time a number is rolled
a1 += 1

-- Check if last rolled number is less than, if true reset variable to 0
if (lastBet.roll < 1) then
    a1 = 0
end


--------------STAGES--------------
if stage == 0 then
    if win then
        stage = 0
        nextbet = base
        chance = 49.5
    else
        nextbet = base
    end
end

if stage == 1 then
    if win then
        stage = 0
        a1 = 0
        nextbet = base2
        chance = 49.5
    else
        nextbet = previousbet*1.02
    end
end

------------STAGE SETTINGS---------

if stage == 0 then
    if a1 >= (99/1*5) then
        stage = 1
        chance = 1
        nextbet = base2
    end
end
end

我希望实现的目标。是复制这一部分:

if (lastBet.roll < 1) then
    a1 = 0
end

并重复它,但将数字增加1,如下所示:

if (lastBet.roll < 1) then
    a1 = 0
end

if (lastBet.roll < 2) then
    a2 = 0
end

if (lastBet.roll < 3) then
    a3 = 0
end

同样适用于下一部分:

if a1 >= (99/1*5) then
    stage = 1
    chance = 1
    nextbet = base2
end

我希望复制/粘贴此内容,同时将数字增加1:

if a1 >= (99/1*5) then
    stage = 1
    chance = 1
    nextbet = base2
end

if a2 >= (99/2*5) then
    stage = 2
    chance = 2
    nextbet = base2
end

if a3 >= (99/3*5) then
    stage = 3
    chance = 3
    nextbet = base2
end

这不是关于我的脚本功能的问题,它按预期工作。但我需要让它比这更长,我问是否有办法重复代码部分,值略有变化,所以我不必手动输入全部。

2 个答案:

答案 0 :(得分:0)

如果将aN变量设为a数组,则可以使用第一个函数和第二个函数循环。

function CheckLast(last, roll)
    if last.roll < roll
        a[roll] = 0
    end
end

for i=1,10 do
    if a[i] >= (99/i*5) then
        stage = i
        chance = i
        nextbet = base2
    end
end

答案 1 :(得分:0)

在我和朋友之间,我们想出来了。

这里的问题是如何复制代码,因为我不知道数组或添加函数。

对于偶然发现这个问题的其他人,我们就是这样做的。

--defines static variables
chance = 49.5
nextbet = 0.00000010
base = 0.00000010
base2 = 0.00000100
stage = 0
maxChance = 99
previousBetWasFail = false
counter = 0
betHigh = false
losspercent = 0
turnaround = 0
turn = 0
first = 0
lossstreak = 0
bb = 0

targets = {}
for i = 0, 3300, 1 do
    targets[i] = 0
end


function resetAll ()
    nextbet = base
    stage = 0
    chance = 49.5
    losspercent = 0
    turnaround = 0
    turn = 0
    first = 0
    lossstreak = 0
end

function dobet()

    counter += 1

    if win then
        lossstreak = 0
    end
    for i = 0, 3300, 1 do
        targets[i] = targets[i] + 1
    end

    for i = 0, 3300, 1 do 
        if (lastBet.roll < (i/100)) then
            targets[i] = 0
        end
        if turnaround == 0 then
            if (targets[i] >= ((99/(i/100))*10)) then
                turnaround = 1
                nextbet = base2
                if turn == 0 then
                    chance = (i/100)
                    turn = 1
                end
            end
        else
            nextbet = (previousbet*((chance/50)+1))
            if win then
                resetAll()
            end
        end
    end
    if turnaround == 1 and first == 0 then
        nextbet = base2
        first = 1
    else
        if first >= 1 and turnaround == 1 then
            if !win then
                nextbet = (previousbet*((chance/50)+1))
            else
                resetAll()
            end
        end
    end
    if balance >= bb then
        bb = balance
    end
    print(balance)
    print(bb)
end