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 gametab = {{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(numtab)
- 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, numtab)
- 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, numtab, virtual)
- virtual = virtual or false
- local points = 0
- if true then
- local changed = false
- if events == 1 then --up
- 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
- points = points + 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 then --right
- 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
- points = points + 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 == 3 then --down
- 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
- points = points + 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 == 4 then --left
- 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
- points = points + 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
- if not virtual then
- local c = math.random(1, 10)
- if c == 10 then spawnTile(4, numtab) else spawnTile(2, numtab) end
- else
- --choose worst possible spot
- end
- end
- end
- if not virtual then score = score + points end
- return points
- end
- local function checkState(numtab)
- 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(numtab, dir)
- dir = dir or 0
- 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)
- if dir > 0 then
- term.setCursorPos(2, 10)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- if dir == 1 then write("up")
- elseif dir == 2 then write("right")
- elseif dir == 3 then write("down")
- elseif dir == 4 then write("left") end
- end
- end
- local function copyTab(a, b)
- for i = 1, #a do
- for v = 1, #a[1] do
- b[i][v] = a[i][v]
- end
- end
- end
- local function checkEqual(a, b)
- for i = 1, #a do
- for v = 1, #a[1] do
- if b[i][v] ~= a[i][v] then return false end
- end
- end
- return true
- end
- function countScore(numtab)
- --local weight = {{1, .95, .9, .85}, {.65, .7, .75, .8}, {.6, .55, .5, .45}, {.25, .3, .35, .4}}
- local weight = {{1, .95, .9, .85}, {.65, .7, .75, .8}, {.6, .55, .5, .45}, {.25, .3, .35, .4}}
- local total = 0
- for i = 1, #numtab do
- for v = 1, #numtab[1] do
- total = total + numtab[i][v]*weight[i][v]
- end
- end
- return total
- end
- function countEmptySpaces(numtab)
- local total = 0
- for i = 1, #numtab do
- for v = 1, #numtab[1] do
- if numtab[i][v] == 0 then total = total + 1 end
- end
- end
- return total
- end
- function penalty(numtab)
- total = 0
- order = {{1, 1}, {1, 2}, {1, 3}, {1, 4}, {2, 4}, {2, 3}, {2, 2}, {2, 1}, {3, 1}, {3, 2}, {3, 3}, {3, 4}, {4, 4}, {4, 3}, {4, 2}, {4, 1}}
- for i = 2, #order do
- if numtab[order[i][1]][order[i][2]] > numtab[order[i-1][1]][order[i-1][2]] then
- total = total + numtab[order[i][1]][order[i][2]]*.5
- end
- end
- return total
- end
- function bonus(numtab)
- local total = 0
- for i = 1, #numtab do
- for v = 1, #numtab[1] do
- if i > 1 then
- if numtab[i-1][v] == 2*numtab[i-1][v] then
- total = total + numtab[i][v]*.1
- end
- end
- end
- end
- return total
- end
- function heuristicScore(numtab, layer)
- if checkEqual(numtab, gametab) then return 0 end
- if layer == 1 then
- return countScore(numtab) - penalty(numtab)
- else
- return computeChoice(numtab, layer-1)
- end
- end
- function computeChoice(numtab, layer)
- layer = layer or 1
- local choices = {}
- for i = 1, 4 do
- local potential = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
- copyTab(numtab, potential)
- extrascore = eventHandler(i, potential, true)
- choices[i] = heuristicScore(potential, layer)
- end
- local priority = {1, 4, 2, 3}
- local decision = priority[1]
- for i = 2, 4 do
- if choices[priority[i]] > choices[decision] then decision = priority[i] end
- end
- return decision
- end
- initialTiles(gametab)
- local gameState = true
- local prevDir = 0
- while gameState do
- if won and showWin then
- printTiles(gametab)
- 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(gametab, prevDir)
- coroutine.yield("key")
- --local events = {os.pullEvent()}
- local compChoice = computeChoice(gametab)
- prevDir = compChoice
- eventHandler(compChoice, gametab)
- gameState = checkState(gametab)
- end
- printTiles(gametab)
- 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)
Add Comment
Please, Sign In to add comment