SHOW:
|
|
- or go back to the newest paste.
| 1 | -- 2048 | |
| 2 | -- Clone by Richard (Rich73) | |
| 3 | -- Hacked together 10/04/2014 | |
| 4 | ||
| 5 | g = { }
| |
| 6 | tiles = { }
| |
| 7 | tiles[1] = {" ", 1}
| |
| 8 | tiles[2] = {" 2 ", 2}
| |
| 9 | tiles[3] = {" 4 ", 4}
| |
| 10 | tiles[4] = {" 8 ", 8}
| |
| 11 | tiles[5] = {" 16 ", 16}
| |
| 12 | tiles[6] = {" 32 ", 32}
| |
| 13 | tiles[7] = {" 64 ", 64}
| |
| 14 | tiles[8] = {"128 ", 128}
| |
| 15 | tiles[9] = {"256 ", 256}
| |
| 16 | tiles[10] = {"512 ", 512}
| |
| 17 | tiles[11] = {"1024", 1024}
| |
| 18 | tiles[12] = {"2048", 2048}
| |
| 19 | bg = "icon" | |
| 20 | size = 4 | |
| 21 | score = 0 | |
| 22 | hiscore = 0 | |
| 23 | ||
| 24 | function createBoard() | |
| 25 | for i=1, size do | |
| 26 | g[i] = { }
| |
| 27 | for j=1, size do | |
| 28 | g[i][j] = 0 | |
| 29 | end | |
| 30 | end | |
| 31 | for j=1, 2 do | |
| 32 | local x, y = findEmptyPos() | |
| 33 | g[y][x] = 1 | |
| 34 | end | |
| 35 | end | |
| 36 | ||
| 37 | function getRandomPos() | |
| 38 | return math.random(size), math.random(size) | |
| 39 | end | |
| 40 | ||
| 41 | function findEmptyPos() | |
| 42 | while true do | |
| 43 | x, y = getRandomPos() | |
| 44 | if g[y][x] == 0 then return x, y end | |
| 45 | end | |
| 46 | end | |
| 47 | ||
| 48 | function isFull() | |
| 49 | local full = true | |
| 50 | for i=1, size do | |
| 51 | for j=1, size do | |
| 52 | if g[i][j] == 0 then full = false end | |
| 53 | end | |
| 54 | end | |
| 55 | return full | |
| 56 | end | |
| 57 | ||
| 58 | function canMove() | |
| 59 | if not isFull() then return true end | |
| 60 | local pr = nil | |
| 61 | for i=1, size do | |
| 62 | local k = 1 | |
| 63 | while k <= size do | |
| 64 | if k~=size and g[i][k] == g[i][k+1] or false then break end | |
| 65 | if pr and g[i][k] == pr[k] or false then break end | |
| 66 | k = k + 1 | |
| 67 | end | |
| 68 | if k ~= size+1 then return true end | |
| 69 | pr = g[i] | |
| 70 | end | |
| 71 | return false | |
| 72 | end | |
| 73 | ||
| 74 | function moveLeft() | |
| 75 | for i=1, size do | |
| 76 | for j=2, size do | |
| 77 | local k = j | |
| 78 | while k > 1 do | |
| 79 | if g[i][k] == 0 then break | |
| 80 | elseif g[i][k] == g[i][k-1] then | |
| 81 | g[i][k-1] = g[i][k] + 1 | |
| 82 | g[i][k] = 0 | |
| 83 | score = score + math.pow(2,g[i][k-1]) | |
| 84 | k = k-1 | |
| 85 | elseif g[i][k-1] == 0 then | |
| 86 | g[i][k-1] = g[i][k] | |
| 87 | g[i][k] = 0 | |
| 88 | else break end | |
| 89 | k = k-1 | |
| 90 | end | |
| 91 | end | |
| 92 | end | |
| 93 | end | |
| 94 | ||
| 95 | function moveUp() | |
| 96 | for j=1, size do | |
| 97 | for i=2, size do | |
| 98 | local k = i | |
| 99 | while k > 1 do | |
| 100 | if g[k][j] == 0 then break | |
| 101 | elseif g[k][j] == g[k-1][j] then | |
| 102 | g[k-1][j] = g[k][j] + 1 | |
| 103 | g[k][j] = 0 | |
| 104 | score = score + math.pow(2,g[k-1][j]) | |
| 105 | k = k-1 | |
| 106 | elseif g[k-1][j] == 0 then | |
| 107 | g[k-1][j] = g[k][j] | |
| 108 | g[k][j] = 0 | |
| 109 | else break end | |
| 110 | k = k-1 | |
| 111 | end | |
| 112 | end | |
| 113 | end | |
| 114 | end | |
| 115 | ||
| 116 | function moveRight() | |
| 117 | flipX() | |
| 118 | moveLeft() | |
| 119 | flipX() | |
| 120 | end | |
| 121 | ||
| 122 | function moveDown() | |
| 123 | flipY() | |
| 124 | moveUp() | |
| 125 | flipY() | |
| 126 | end | |
| 127 | ||
| 128 | function drawBoard() | |
| 129 | term.setBackgroundColor(colors.black) | |
| 130 | term.clear() | |
| 131 | term.setTextColor(colors.white) | |
| 132 | for x=1, size do | |
| 133 | for y=1, size do | |
| 134 | term.setCursorPos(x*5-1,y*4-2) | |
| 135 | term.setBackgroundColor(tiles[g[y][x]+1][2]) | |
| 136 | term.write(" ")
| |
| 137 | term.setCursorPos(x*5-1,y*4-1) | |
| 138 | term.write(tiles[g[y][x]+1][1]) | |
| 139 | term.setCursorPos(x*5-1,y*4) | |
| 140 | term.write(" ")
| |
| 141 | end | |
| 142 | end | |
| 143 | term.setCursorPos(4,18) | |
| 144 | term.setBackgroundColor(colors.black) | |
| 145 | term.write("Score: "..score)
| |
| 146 | drawScores() | |
| 147 | end | |
| 148 | ||
| 149 | function drawScores() | |
| 150 | term.setCursorPos(4,18) | |
| 151 | term.setBackgroundColor(colors.black) | |
| 152 | term.write(" Score: "..score)
| |
| 153 | term.setCursorPos(4,19) | |
| 154 | term.write("HiScore: "..hiscore)
| |
| 155 | end | |
| 156 | ||
| 157 | function drawHome() | |
| 158 | if not fs.exists(bg) then shell.run("pastebin get AWKwMWXW "..bg) end
| |
| 159 | term.clear() | |
| 160 | im = paintutils.loadImage(bg) | |
| 161 | paintutils.drawImage(im,2,2) | |
| 162 | end | |
| 163 | ||
| 164 | function table.reverse(tab) | |
| 165 | local newtab = { }
| |
| 166 | for i=1, #tab do | |
| 167 | newtab[#tab+1-i] = tab[i] | |
| 168 | end | |
| 169 | return newtab | |
| 170 | end | |
| 171 | ||
| 172 | function flipX() | |
| 173 | for i=1, size do | |
| 174 | g[i] = table.reverse(g[i]) | |
| 175 | end | |
| 176 | end | |
| 177 | ||
| 178 | function flipY() | |
| 179 | g = table.reverse(g) | |
| 180 | end | |
| 181 | ||
| 182 | function update() | |
| 183 | drawBoard() | |
| 184 | if not isFull() then | |
| 185 | local x, y = findEmptyPos() | |
| 186 | g[y][x] = 1 | |
| 187 | end | |
| 188 | os.sleep(0.075) | |
| 189 | end | |
| 190 | ||
| 191 | function newGame() | |
| 192 | if score > hiscore then | |
| 193 | hiscore = score | |
| 194 | end | |
| 195 | score = 0 | |
| 196 | term.setCursorPos(9,18) | |
| 197 | term.setBackgroundColor(colors.white) | |
| 198 | term.setTextColor(colors.black) | |
| 199 | term.clearLine() | |
| 200 | term.write("GAME OVER!")
| |
| 201 | term.setCursorPos(8,19) | |
| 202 | term.clearLine() | |
| 203 | term.write("New game? y/n")
| |
| 204 | while true do | |
| 205 | local event, key = os.pullEvent("key")
| |
| 206 | if event=="key" then | |
| 207 | if key==keys.y then return true end | |
| 208 | if key==keys.n then return false end | |
| 209 | end | |
| 210 | end | |
| 211 | end | |
| 212 | ||
| 213 | drawHome() | |
| 214 | ||
| 215 | os.sleep(2) | |
| 216 | while true do | |
| 217 | createBoard() | |
| 218 | while canMove() do | |
| 219 | drawBoard() | |
| 220 | event, key = os.pullEvent("key")
| |
| 221 | if event == "key" then | |
| 222 | if key == keys.left then moveLeft() | |
| 223 | elseif key == keys.right then | |
| 224 | moveRight() | |
| 225 | update() | |
| 226 | elseif key == keys.up then | |
| 227 | moveUp() | |
| 228 | update() | |
| 229 | elseif key == keys.down then | |
| 230 | moveDown() | |
| 231 | update() | |
| 232 | elseif key == keys.q then | |
| 233 | break | |
| 234 | end | |
| 235 | end | |
| 236 | end | |
| 237 | drawBoard() | |
| 238 | if not newGame() then | |
| 239 | term.setBackgroundColor(colors.black) | |
| 240 | term.setTextColor(colors.white) | |
| 241 | term.clear() | |
| 242 | term.setCursorPos(1,1) | |
| 243 | break | |
| 244 | end | |
| 245 | end |