TheKojent icon

2048 Dummy

TheKojent | PRO | 03/21/14 09:12:53 PM UTC | 0 ⭐ | 289 👁️ | Never ⏰ | []
Lua |

2.46 KB

|

None

|

0 👍

/

0 👎

--[[
 
11  12  13  14
 
21  22  23  24
 
31  32  33  34
 
41  42  43  44
 
]]--
 
local win = false
 
a = {}
for i=1,4 do
    a[i] = {}
    for j=1,4 do
        a[i][j] = 0
    end
end
 
function determinant()
    a[2][1] = a[1][2]
    a[3][1] = a[1][3]
    a[4][1] = a[1][4]
    a[3][2] = a[2][3]
    a[4][2] = a[2][4]
    a[4][3] = a[3][4]
end
 
function update()
    --insert code
end
 
function reset()
    for i = 1,4 do
        for j = 1,4 do
            a[i][j] = 0
        end
    end
    update()
end
 
function newNum()
    local blank = 0
    for i = 1,4 do
        for j = 1,4 do
            if a[i][j] == 0 then
                blank = blank + 1
            end
        end
    end
    if blank == 0 then
        return false
    else
        while true do
            --insert random selection of x,y
            if a[x][y] == 0 then
                a[x][y] = 2
                break
            end
        end
    end
end
 
function score(val)
    --insert code for adding score
    for i = 1,4 do
        for j = 1,4 do
            if a[i][j] = 2048 then
                win = true
            end
        end
    end
end
 
function endScreen()
    --insert code here
end
 
 
 
--main body
 
while true do
    update()
    while true do
        event, key = os.pullEvent("key")
        if (key == "w" or key == "a" or key == "s" or key == "d" or key == "r") then
            break
        end
    end
    if key == "r" then
        reset()
    elseif key == "w" or key == "s" then
        determinant()
    end
    if key == "w" or key == "a" then
        for i = 1,4 do
            for j = 1,3 do
                if a[i][5-j] == a[i][4-j] then
                    a[i][4-j] = a[i][5-j] + a[i][4-j]
                    score(a[i][4-j])
                elseif a[i][4-j] == 0 then
                    a[i][4-j] = a[i][5-j]
                end
                a[i][5-j] = 0
            end
            if a[i][2] == 0 and a[i][3] > 0 then
                a[i][2] = a[i][3]
                a[i][3] = 0
            end
        end
    elseif key == "s" or key == "d" then
        for i = 1,4 do
            for j = 1,3 do
                if a[i][j] == a[i][j+1] then
                    a[i][j+1] = a[i][j] + a[i][j+1]
                    score(a[i][j])
                elseif a[i][j+1] == 0 then
                    a[i][j+1] = a[i][j]
                end 
                a[i][j] = 0
            end
            if a[i][3] == 0 and a[i][2] != 0 then
                a[i][3] = a[i][2]
                a[i][2] = 0
            end
        end
    end
    if key == "w" or key == "s" then
        determinant()
    end
    if not newNum() then
        local comb = false
        for b = 1,2 do
            for i = 1,4 do
                for j = 1,3 do
                    if a[i][j] == a[i][j+1] then
                        comb = true
                        break
                    end
                    if comb then break end
                end
                if comb then break end
            end
            if comb then break end
            determinant()
        end
        endScreen()
        sleep(1000)
        reset()
    end
    if win == true then
        update()
        sleep(250)
        endScreen()
    end
end

Comments