モジュール:サンドボックス/邪魔ー

出典: 謎の百科事典もどき『エンペディア(Enpedia)』
ナビゲーションに移動 検索に移動

ここに呼び出す説明文 『 モジュール:サンドボックス/邪魔ー/doc 』 が作成されていません。

local p = {}

function p.Amidakuji(frame)
    local width = tonumber(frame.args[1]);
    local height = tonumber(frame.args[2]);
    local density = tonumber(frame.args[3]);
    local rails = {}
    local retval = '<tt>\n'
    
    for i = 1, height do
        rails[i] = {}
        for j = 1, width - 1 do
            local n = density < math.random()
            if j == 1 then
                rails[i][j] = n
            else
                rails[i][j] = (rails[i][j - 1] == false) and n
            end
        end
        rails[i][width] = false
    end
    
    local loopend = false
    local needDestroyLine = false
    local count = 0
    local i = 0
    for j = 1, width - 1 do
        loopend = false
        needDestroyLine = false
        count = 0
        i = math.random(1, height)
        
        -- append line.
        repeat
            if rails[i][j] == true then
                break
            end
            
            count = count + 1
            if j == 1 then
                if rails[i][j] == false
                   and rails[i][j + 1] == false then
                    rails[i][j] = true
                    loopend = true
                end
            else
                if rails[i][j] == false
                   and rails[i][j + 1] == false
                   and rails[i][j - 1] == false then
                    rails[i][j] = true
                    loopend = true
                end
            end
            
            if i >= height then
                i = 0
            end
            i = i + 1
            if count > height then
                needDestroyLine = true
            end
        until loopend or needDestroyLine
        
        -- destroy same distance line.
        if needDestroyLine then
            rails[i][j] = true
            rails[i][j + 1] = false
            if j > 1 then
                rails[i][j - 1] = false
            end
        end
    end

    for i = 1, height do
        for j = 1, width do
            if j == 1 then
                if rails[i][j] == true then
                    retval = retval .. '├'
                else
                    retval = retval .. '│'
                end
            elseif j == width then
                if rails[i][j - 1] == true then
                    retval = retval .. '┤'
                else
                    retval = retval .. '│'
                end
            elseif rails[i][j] == true then
                retval = retval .. '├'
            elseif rails[i][j] == false and rails[i][j - 1] == true then
                retval = retval .. '┤'
            else
                retval = retval .. '│'
            end
        end
        retval = retval .. "<br/>\n"
    end
    
    return retval .. '</tt>'
end

return p

--[[
mw.log(p.Amidakuji({args={10,6,0.5,1234}}))
mw.log(p.Amidakuji({args={10,6,0.5,os.time()}}))
--]]