-- Mines: find the mines without stepping on one. A click opens a
-- square, a right click or a hold flags it; clicking a number whose
-- flags are all down opens the squares round it.
local c8 = require "c8"
local levels = {
{ name = "Easy", cols = 9, rows = 9, mines = 10 },
{ name = "Medium", cols = 16, rows = 16, mines = 40 },
{ name = "Hard", cols = 30, rows = 16, mines = 99 },
}
local saved = c8.load("mines") or {}
local level = saved.level or 1
local best = saved.best or {}
local win, board, status, clock
local cells, cols, rows, mines
local state -- "new" (no mines laid yet), "playing", "won", "lost"
local started, ended, flags, opened
local held -- the press waiting to be a click or a hold
local function save()
c8.save("mines", { level = level, best = best })
end
local function at(c, r)
return cells[(r - 1) * cols + c]
end
local function around(c, r, fn)
for dr = -1, 1 do
for dc = -1, 1 do
local cc, rr = c + dc, r + dr
if (dc ~= 0 or dr ~= 0) and cc >= 1 and cc <= cols and rr >= 1
and rr <= rows then
fn(at(cc, rr), cc, rr)
end
end
end
end
local function elapsed()
if not started then
return 0
end
return (ended or os.time()) - started
end
local function show_status()
local text = string.format("%d mines left %d s", mines - flags, elapsed())
if state == "won" then
text = string.format("Cleared in %d s", elapsed())
if best[level] then
text = text .. string.format(" best %d s", best[level])
end
elseif state == "lost" then
text = "Boom. Press New game to try again."
end
status:set(text)
end
local function new_game()
local l = levels[level]
cols, rows, mines = l.cols, l.rows, l.mines
cells = {}
for i = 1, cols * rows do
cells[i] = { mine = false, open = false, flag = false, n = 0 }
end
state, started, ended, flags, opened = "new", nil, nil, 0, 0
if clock then
clock:cancel()
clock = nil
end
if board then
board:redraw()
show_status()
end
end
-- Mines go down after the first click, never on it or next to it
local function lay(c0, r0)
local free = {}
for r = 1, rows do
for c = 1, cols do
if math.abs(c - c0) > 1 or math.abs(r - r0) > 1 then
free[#free + 1] = at(c, r)
end
end
end
for i = 1, mines do
local j = math.random(i, #free)
free[i], free[j] = free[j], free[i]
free[i].mine = true
end
for r = 1, rows do
for c = 1, cols do
around(c, r, function(o)
if o.mine then
at(c, r).n = at(c, r).n + 1
end
end)
end
end
state, started = "playing", os.time()
clock = c8.every(1, function()
show_status()
return state == "playing"
end)
end
local function finish(won)
state, ended = won and "won" or "lost", os.time()
if won then
for _, cell in ipairs(cells) do
if cell.mine and not cell.flag then
cell.flag = true
flags = flags + 1
end
end
if not best[level] or elapsed() < best[level] then
best[level] = elapsed()
save()
end
c8.tile.update("org.cont8nuum.Mines", { template = "block",
block = tostring(best[level]), text1 = "seconds",
text2 = "best on " .. levels[level].name })
end
show_status()
end
local function open(c, r)
local todo = { { c, r } }
while #todo > 0 do
local c1, r1 = table.unpack(table.remove(todo))
local cell = at(c1, r1)
if not cell.open and not cell.flag then
cell.open = true
opened = opened + 1
if cell.mine then
cell.boom = true
return finish(false)
end
if cell.n == 0 then
around(c1, r1, function(_, cc, rr)
todo[#todo + 1] = { cc, rr }
end)
end
end
end
if opened == cols * rows - mines then
finish(true)
end
end
local function click(c, r)
if state == "won" or state == "lost" then
return
end
if state == "new" then
lay(c, r)
end
local cell = at(c, r)
if cell.open and cell.n > 0 then
local f = 0
around(c, r, function(o) if o.flag then f = f + 1 end end)
if f == cell.n then
around(c, r, function(_, cc, rr)
if state == "playing" then open(cc, rr) end
end)
end
else
open(c, r)
end
board:redraw()
show_status()
end
local function flag(c, r)
local cell = at(c, r)
if state ~= "playing" and state ~= "new" or cell.open then
return
end
cell.flag = not cell.flag
flags = flags + (cell.flag and 1 or -1)
board:redraw()
show_status()
end
-- A tall window turns a wide board on its side, so Hard fits a phone
-- sized window
local function geometry(w, h)
local turned = h > w and cols > rows
local across, down = cols, rows
if turned then
across, down = rows, cols
end
local size = math.floor(math.min(w / across, h / down, 44))
local ox = (w - size * across) // 2
local oy = (h - size * down) // 2
return size, ox, oy, turned
end
local number_colors = { 0x1e6fb8, 0x00a300, 0xe51400, 0x603cba,
0x9f00a7, 0x00aba9, 0x1d1d1d, 0x7a7a7a }
local function draw(g, x, y, w, h)
local size, ox, oy, turned = geometry(w, h)
for r = 1, rows do
for c = 1, cols do
local cell = at(c, r)
local col, row = c, r
if turned then
col, row = r, c
end
local cx = x + ox + (col - 1) * size
local cy = y + oy + (row - 1) * size
local s = size - 2
if cell.open then
g:rect(cx, cy, s, s, g.fg, cell.boom and 0 or 0.08)
if cell.boom then
g:rect(cx, cy, s, s, 0xe51400)
end
elseif state == "lost" and cell.mine then
g:rect(cx, cy, s, s, g.fg, 0.2)
else
g:rect(cx, cy, s, s, g.accent, cell.flag and 1 or 0.75)
end
local text, color
if cell.flag and not (state == "lost" and cell.mine) then
text, color = "⚑", 0xffffff
if state == "lost" then
text = "✕"
end
elseif (cell.open or state == "lost") and cell.mine then
text, color = "●", cell.boom and 0xffffff or g.fg
elseif cell.open and cell.n > 0 then
text = tostring(cell.n)
color = g.dark and 0xffffff or number_colors[cell.n]
end
if text then
local opts = { size = s * 0.55, weight = "semibold", color = color }
local tw, th = g:measure(text, opts)
g:text(text, cx + (s - tw) / 2, cy + (s - th) / 2, opts)
end
end
end
end
local function cell_at(ev)
local size, ox, oy, turned = geometry(board.width, board.height)
local col = math.floor((ev.x - board.x - ox) / size) + 1
local row = math.floor((ev.y - board.y - oy) / size) + 1
if turned then
col, row = row, col
end
if col >= 1 and col <= cols and row >= 1 and row <= rows then
return col, row
end
end
local function on_event(_, ev)
if ev.type == "press" then
local c, r = cell_at(ev)
if not c then
return false
end
if ev.button == "right" then
flag(c, r)
return true
end
-- Held half a second, a press flags (for touch)
held = { c = c, r = r }
local mine = held
held.timer = c8.after(0.5, function()
if held == mine then
held = nil
flag(c, r)
end
end)
return true
elseif ev.type == "release" and held then
held.timer:cancel()
local c, r = cell_at(ev)
if c == held.c and r == held.r then
click(c, r)
end
held = nil
return true
end
return false
end
local function build()
status = c8.label { text = "", color = "secondary" }
board = c8.canvas { fill = true, expand = true, draw = draw,
on_event = on_event }
local names = {}
for i, l in ipairs(levels) do
names[i] = l.name
end
local pick = c8.combo { items = names, selected = level, width = 160,
on_change = function(_, i)
level = i
save()
new_game()
end }
local view = c8.column { spacing = 16, padding = { 24, 24, 24, 24 },
c8.row { spacing = 16, wrap = true,
c8.label { text = "Mines", style = "header" },
},
c8.row { spacing = 12, wrap = true,
c8.button { label = "New game", primary = true,
on_click = new_game },
pick,
status,
},
board,
}
return view
end
c8.app {
id = "org.cont8nuum.Mines",
open = function()
if not (win and win.open) then
new_game()
win = c8.window { title = "Mines", width = 900, height = 760, build(),
on_key = function(_, ev)
if ev.key == "F2" or (ev.ctrl and ev.key == "n") then
new_game()
return true
end
end }
show_status()
end
win:present()
end,
}