-- Merge: slide the tiles with the arrow keys or a swipe; two tiles of
-- the same number that meet become one of twice that. A new 2 (now and
-- then a 4) comes in after every move. The best score goes on the tile.
local c8 = require "c8"
local N = 4
local saved = c8.load("merge") or {}
local best = saved.best or 0
local grid, score, over, won_shown
local win, board, score_label, best_label
local down -- where a press or touch began, for swipes
-- Metro colours, darker as the numbers grow
local colors = { 0x00aba9, 0x2d89ef, 0x2b5797, 0x603cba, 0x9f00a7,
0xb91d47, 0xe51400, 0xda532c, 0xf09609, 0x00a300, 0x1e7145, 0x1d1d1d }
local function keep()
c8.save("merge", { best = best, grid = not over and grid or nil,
score = not over and score or nil })
end
local function empties()
local list = {}
for r = 1, N do
for c = 1, N do
if grid[r][c] == 0 then
list[#list + 1] = { r, c }
end
end
end
return list
end
local function drop()
local list = empties()
if #list > 0 then
local r, c = table.unpack(list[math.random(#list)])
grid[r][c] = math.random() < 0.9 and 2 or 4
end
end
local function can_move()
if #empties() > 0 then
return true
end
for r = 1, N do
for c = 1, N do
local v = grid[r][c]
if (c < N and grid[r][c + 1] == v) or (r < N and grid[r + 1][c] == v) then
return true
end
end
end
return false
end
local function show()
score_label:set(tostring(score))
best_label:set(tostring(best))
board:redraw()
end
local function new_game()
grid = {}
for r = 1, N do
grid[r] = {}
for c = 1, N do
grid[r][c] = 0
end
end
score, over, won_shown = 0, false, false
drop()
drop()
keep()
if board then
show()
end
end
-- One line slid towards its start: { 2, 2, 0, 4 } -> { 4, 4, 0, 0 }
local function slide(line)
local out, gained = {}, 0
local last
for _, v in ipairs(line) do
if v ~= 0 then
if last == v then
out[#out] = v * 2
gained = gained + v * 2
last = nil
else
out[#out + 1] = v
last = v
end
end
end
for i = #out + 1, N do
out[i] = 0
end
return out, gained
end
-- dr, dc: the way the tiles go
local function move(dr, dc)
if over then
return
end
local moved = false
for i = 1, N do
-- The cells of this line, from the edge the tiles go to
local cells = {}
for j = 1, N do
local k = (dr + dc > 0) and (N + 1 - j) or j
cells[j] = dr ~= 0 and { k, i } or { i, k }
end
local line = {}
for j, rc in ipairs(cells) do
line[j] = grid[rc[1]][rc[2]]
end
local out, gained = slide(line)
score = score + gained
for j, rc in ipairs(cells) do
if grid[rc[1]][rc[2]] ~= out[j] then
moved = true
end
grid[rc[1]][rc[2]] = out[j]
end
end
if not moved then
return
end
drop()
if score > best then
best = score
c8.tile.update("org.cont8nuum.Merge", { template = "block",
block = tostring(best), text1 = "best score" })
end
over = not can_move()
keep()
show()
local top = 0
for r = 1, N do
for c = 1, N do
top = math.max(top, grid[r][c])
end
end
if over then
win:dialog { title = "No more moves",
message = "You scored " .. score .. ".",
buttons = { "New game", "Close" },
done = function(choice) if choice == 1 then new_game() end end }
elseif top >= 2048 and not won_shown then
won_shown = true
win:message("2048", "You made 2048. Keep going if you like.")
end
end
local function geometry(w, h)
local side = math.min(w, h, 560)
local gap = math.max(4, side // 50)
local cell = (side - gap * (N + 1)) / N
return side, gap, cell, (w - side) / 2, (h - side) / 2
end
local function draw(g, x, y, w, h)
local side, gap, cell, ox, oy = geometry(w, h)
g:rect(x + ox, y + oy, side, side, g.fg, 0.08)
for r = 1, N do
for c = 1, N do
local v = grid[r][c]
local cx = x + ox + gap + (c - 1) * (cell + gap)
local cy = y + oy + gap + (r - 1) * (cell + gap)
if v == 0 then
g:rect(cx, cy, cell, cell, g.fg, 0.06)
else
local step = math.min(math.floor(math.log(v, 2)), #colors)
g:rect(cx, cy, cell, cell, colors[step])
local text = tostring(v)
local size = cell * (#text <= 2 and 0.42 or #text == 3 and 0.34 or 0.26)
local opts = { size = size, weight = "semibold", color = 0xffffff }
local tw, th = g:measure(text, opts)
g:text(text, cx + (cell - tw) / 2, cy + (cell - th) / 2, opts)
end
end
end
if over then
g:rect(x + ox, y + oy, side, side, g.bg, 0.6)
local opts = { style = "header" }
local tw, th = g:measure("Game over", opts)
g:text("Game over", x + (w - tw) / 2, y + (h - th) / 2, opts)
end
end
local function on_event(_, ev)
if ev.type == "press" or ev.type == "touch_down" then
down = { ev.x, ev.y }
return true
elseif (ev.type == "release" or ev.type == "touch_up") and down then
local dx, dy = ev.x - down[1], ev.y - down[2]
down = nil
if math.max(math.abs(dx), math.abs(dy)) < 30 then
return true
end
if math.abs(dx) > math.abs(dy) then
move(0, dx > 0 and 1 or -1)
else
move(dy > 0 and 1 or -1, 0)
end
return true
end
return false
end
local ways = {
Left = { 0, -1 }, Right = { 0, 1 }, Up = { -1, 0 }, Down = { 1, 0 },
a = { 0, -1 }, d = { 0, 1 }, w = { -1, 0 }, s = { 1, 0 },
h = { 0, -1 }, l = { 0, 1 }, k = { -1, 0 }, j = { 1, 0 },
}
local function build()
score_label = c8.label { text = "0", style = "subheader" }
best_label = c8.label { text = "0", style = "subheader", color = "accent" }
board = c8.canvas { fill = true, expand = true, draw = draw,
on_event = on_event }
return c8.column { spacing = 16, padding = { 24, 24, 24, 24 },
c8.row { spacing = 24, wrap = true,
c8.label { text = "Merge", style = "header" },
},
c8.row { spacing = 24, wrap = true,
c8.column { c8.label { text = "Score", color = "secondary" }, score_label },
c8.column { c8.label { text = "Best", color = "secondary" }, best_label },
c8.button { label = "New game", align = "center", on_click = function()
if over or score == 0 then
return new_game()
end
win:dialog { title = "Start again?",
message = "This game will be lost.",
buttons = { "New game", "Keep playing" },
done = function(choice) if choice == 1 then new_game() end end }
end },
},
board,
}
end
c8.app {
id = "org.cont8nuum.Merge",
open = function()
if not (win and win.open) then
if saved.grid then
grid, score, over = saved.grid, saved.score or 0, false
else
new_game()
end
win = c8.window { title = "Merge", width = 640, height = 800, build(),
on_key = function(_, ev)
local way = not (ev.ctrl or ev.alt) and ways[ev.key]
if way then
move(way[1], way[2])
return true
end
end }
show()
end
win:present()
end,
}