Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local maxNum = 2
- local score = 0
- local won, showWin = false, true
- local w, h = term.getSize()
- local midW, midH = math.floor(w/2), math.floor(h/2)
- local numtab = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
- local colTab = {
- [0] = colors.black,
- [2] = colors.white,
- [4] = colors.white,
- [8] = colors.orange,
- [16] = colors.orange,
- [32] = colors.red,
- [64] = colors.red,
- [128] = colors.yellow,
- [256] = colors.yellow,
- [512] = colors.yellow,
- [1024] = colors.yellow,
- [2048] = colors.yellow,
- [4096] = colors.green,
- [8192] = colors.purple
- }
- local glitchWorkaround = {
- [0] = " ",
- [2] = "2",
- [4] = "4",
- [8] = "8",
- [16] = "16",
- [32] = "32",
- [64] = "64",
- [128] = "128",
- [256] = "256",
- [512] = "512",
- [1024] = "1024",
- [2048] = "2048",
- [4096] = "4096",
- [8192] = "8192"
- }
- local function initialTiles()
- local y, x
- for i = 1, 2 do
- repeat
- x = math.random(1, 4)
- y = math.random(1, 4)
- until numtab[y][x] == 0
- numtab[y][x] = 2
- end
- end
- local function spawnTile(tileNum)
- local availableSpots = {}
- for y = 1, 4 do
- for x = 1, 4 do
- if numtab[y][x] == 0 then
- availableSpots[#availableSpots + 1] = {y, x}
- end
- end
- end
- local choice = availableSpots[math.random(1, #availableSpots)]
- numtab[choice[1]][choice[2]] = tileNum
- end
- local function eventHandler(events)
- if events[1] == "key" then
- local changed = false
- if events[2] == keys.up then
- for x = 1, 4 do
- local moveMode = false
- for y = 2, 4 do
- for i = 0, y - 2 do
- if numtab[y - i][x] == 0 then
- elseif numtab[y - 1 - i][x] == 0 then
- numtab[y - 1 - i][x] = numtab[y - i][x]
- numtab[y - i][x] = 0
- changed = true
- elseif not moveMode and numtab[y - 1 - i][x] == numtab[y - i][x] then
- numtab[y - 1 - i][x] = numtab[y - i][x] * 2
- score = score + numtab[y - i][x] * 2
- numtab[y - i][x] = 0
- changed = true
- moveMode = true
- break
- else moveMode = false; break end
- end
- end
- end
- elseif events[2] == keys.right then
- for y = 1, 4 do
- local moveMode = false
- for x = 3, 1, -1 do
- for i = 0, 3 - x do
- if numtab[y][x + i] == 0 then
- elseif numtab[y][x + 1 + i] == 0 then
- numtab[y][x + 1 + i] = numtab[y][x + i]
- numtab[y][x + i] = 0
- changed = true
- elseif not moveMode and numtab[y][x + 1 + i] == numtab[y][x + i] then
- numtab[y][x + 1 + i] = numtab[y][x + i] * 2
- score = score + numtab[y][x + i] * 2
- numtab[y][x + i] = 0
- changed = true
- moveMode = true
- break
- else moveMode = false; break end
- end
- end
- end
- elseif events[2] == keys.down then
- for x = 1, 4 do
- local moveMode = false
- for y = 3, 1, -1 do
- for i = 0, 3 - y do
- if numtab[y + i][x] == 0 then
- elseif numtab[y + 1 + i][x] == 0 then
- numtab[y + 1 + i][x] = numtab[y + i][x]
- numtab[y + i][x] = 0
- changed = true
- elseif not moveMode and numtab[y + 1 + i][x] == numtab[y + i][x] then
- numtab[y + 1 + i][x] = numtab[y + i][x] * 2
- score = score + numtab[y + i][x] * 2
- numtab[y + i][x] = 0
- changed = true
- moveMode = true
- break
- else moveMode = false; break end
- end
- end
- end
- elseif events[2] == keys.left then
- for y = 1, 4 do
- local moveMode = false
- for x = 2, 4 do
- for i = 0, x - 2 do
- if numtab[y][x - i] == 0 then
- elseif numtab[y][x - 1 - i] == 0 then
- numtab[y][x - 1 - i] = numtab[y][x - i]
- numtab[y][x - i] = 0
- changed = true
- elseif not moveMode and numtab[y][x - 1 - i] == numtab[y][x - i] then
- numtab[y][x - 1 - i] = numtab[y][x - i] * 2
- score = score + numtab[y][x - i] * 2
- numtab[y][x - i] = 0
- changed = true
- moveMode = true
- break
- else moveMode = false; break end
- end
- end
- end
- end
- if changed then
- local c = math.random(1, 10)
- if c == 10 then spawnTile(4) else spawnTile(2) end
- end
- end
- end
- local function checkState()
- for y = 1, 4 do
- for x = 1, 4 do
- if numtab[y][x] > maxNum then maxNum = numtab[y][x] end
- if numtab[y][x] == 2048 then won = true end
- end
- end
- local flag = false
- for y = 1, 4 do
- for x = 1, 4 do
- if numtab[y][x] == 0 then
- flag = true
- elseif y > 1 and numtab[y - 1][x] == numtab[y][x] then
- flag = true
- elseif y < 4 and numtab[y + 1][x] == numtab[y][x] then
- flag = true
- elseif x > 1 and numtab[y][x - 1] == numtab[y][x] then
- flag = true
- elseif x < 4 and numtab[y][x + 1] == numtab[y][x] then
- flag = true
- end
- if flag then break end
- end
- end
- if not flag then return false end
- return true
- end
- local function printTiles()
- if maxNum == 16384 then
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- error("ERROR: User is too f*cking good at this... failure to parse awesomeness level...")
- end
- term.setBackgroundColor(colTab[maxNum])
- term.clear()
- paintutils.drawFilledBox(midW - 7, midH - 8, midW + 10, midH + 14, colors.gray)
- for y = 1, 4 do
- for x = 1, 4 do
- paintutils.drawFilledBox(midW - 10 + x*4, midH - 11 + y*4, midW - 11 + (x+1)*4, midH - 12 + (y+1)*4, colTab[numtab[y][x]])
- term.setCursorPos(midW - 10 + x*4, midH - 10 + y*4)
- if numtab[y][x] == 2 or numtab[y][x] == 4 then
- term.setTextColor(colors.black)
- else term.setTextColor(colors.white) end
- term.write(glitchWorkaround[numtab[y][x]])
- end
- end
- term.setBackgroundColor(colTab[maxNum])
- term.setTextColor(colTab[maxNum] == colors.white and colors.black or colors.white)
- term.setCursorPos(w - 4, 1)
- term.write("Score")
- term.setCursorPos(w - #tostring(score) + 1, 2)
- term.write(score)
- end
- initialTiles()
- local gameState = true
- while gameState do
- if won and showWin then
- printTiles()
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.white)
- for i = 0, 1 do
- term.setCursorPos(1, midH + i)
- term.clearLine()
- end
- term.setCursorPos(midW, midH)
- term.write("2048")
- term.setCursorPos(midW - 2, midH + 1)
- term.write("Victory!")
- sleep(.5)
- coroutine.yield("key")
- showWin = false
- end
- printTiles()
- local events = {os.pullEvent()}
- eventHandler(events)
- gameState = checkState()
- end
- printTiles()
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- for i = 0, 1 do
- term.setCursorPos(1, midH + i)
- term.clearLine()
- end
- term.setCursorPos(midW - #tostring(score)/2 + 2, midH)
- term.write(score)
- term.setCursorPos(midW - 2, midH + 1)
- term.write("You Lose")
- sleep(.5)
- coroutine.yield("key")
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement