← 都来码 / Dolaima← 例程索引lua/app/maze.lua▶ 在浏览器里试玩 原文

lua/app/maze.lua · 7717 字节 · 下载原文

▶ 在浏览器里试玩

-- 迷宫 —— 用 **Lua** 写的手机小游戏(探索 / 每局现生成)
-- Maze — a small phone game written in **Lua**
--
-- 玩法(与 Examples/c/app/maze.c / Examples/python/app/maze.py 同一套):
--   每局**现生成**一张新迷宫(深度优先挖洞,一定连通、一定有路);
--   方向键 / 点屏幕四边移动,走到右下角红格即通关;ESC / 通关后点一下换一张。
--
-- ## ⚠ 本 Lua 版用**公式化蛇形迷宫**,不用 DFS 表
-- ## ⚠ This Lua edition uses a **formulaic serpentine maze**, not a DFS table
--
-- 实测(2026-11-14):`{}` 只能先按字面量建 8 个键位,441 格的迷宫表用 `table.insert`
-- 建出来之后,逐格写入/读取仍然不可靠(整列读成 0、迷宫画不出来)。这一版改成**用公式判定
-- 每格是不是墙**:偶数行整行通道、奇数行交替在左右两端开口 —— 一定连通、一定有路,
-- 也不再依赖任何大表。
-- Measured 2026-11-14: a `{}` table holds 8 keys, and a 441-slot maze table built with
-- `table.insert` still read back wrong (whole columns became 0 and the maze never drew). This
-- edition decides every cell with a **formula** instead: even rows are open corridors, odd rows
-- open at alternating ends -- always connected, always solvable, and no big table at all.
--
-- ## 本语言的实测约束(见 Examples/lua/app/snake.lua / memory.lua 文件头)
--   · 全局状态**必须写在函数定义之前**;入口是 `main()`;
--   · **没有整数除法**:`/` 返回浮点、UI 参数按位型解释 ⇒ 一律 `idiv`;
--   · 逻辑运算符是 `or` / `and`;裸 `return` 写 `return 0`;
--   · 数字上屏走**点阵**,不拼字符串。
--
-- 跑法 —— 手机:vml run examples/lua/maze.lua
-- 跑法 —— 桌面:vmlcli Examples/lua/maze.lua --screen 480x640 --frames /tmp/fr

require("vml_ui")

FONT = {31599, 29850, 29671, 31207, 18925, 31183, 31695, 18727, 31727, 31215}
BIT = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384}

MZ_SW = 360
MZ_SH = 620
MZ_CELL = 20
MZ_OX = 0
MZ_OY = 110
PX = 1
PY = 1
WON = 0
STEPS = 0
SEED = 20268
TOP = 0
LANG = 0
FRAMES = 0
ALIVE = 1
T_STEPS = "步数"
T_WON = "走出来了!点一下/ESC 换一张"
T_HINT = "方向键 / 点屏幕四边,走到红格"

-- 先按字面量建表拿到容量
CELLS = {}
STX = {}
STY = {}
DIRS = {0, 0, 0, 0}

function idiv(a, b)
    local q = 0
    local r = a
    if r < 0 then r = 0 - r end
    while r >= b do
        r = r - b
        q = q + 1
    end
    if a < 0 then return 0 - q end
    return q
end

function draw_digit(x, y, d, px, color)
    if d < 0 then d = 0 end
    if d > 9 then d = 9 end
    local mask = FONT[d + 1]
    local r = 0
    while r < 5 do
        local c = 0
        while c < 3 do
            local bitv = BIT[r * 3 + c + 1]
            if mask % (bitv * 2) >= bitv then
                ui_2d_rect(x + c * px, y + r * px, px, px, color, 1, 0, 0)
            end
            c = c + 1
        end
        r = r + 1
    end
end

function draw_number(x, y, v, px, color)
    if v < 0 then v = 0 end
    if v > 999999 then v = 999999 end
    local digits = 1
    local n = v
    while n >= 10 do
        n = idiv(n, 10)
        digits = digits + 1
    end
    local i = digits
    local rest = v
    while i >= 1 do
        local d = rest % 10
        rest = idiv(rest, 10)
        draw_digit(x + (i - 1) * px * 4, y, d, px, color)
        i = i - 1
    end
end

function maze_rnd(n)
    SEED = (SEED * 1103 + 12345) % 32768
    return idiv(SEED, 256) % n
end

function relayout()
    MZ_SW = ui_scr_get_w()
    MZ_SH = ui_scr_get_h()
    if MZ_SW <= 0 then
        MZ_SW = 360
    end
    if MZ_SH <= 0 then
        MZ_SH = 620
    end
    -- 棋盘边长同时受宽、高约束(上留标题、下留提示),再在剩余高度里垂直居中
    -- The board side is bounded by both width and height (header above, hint below), then centred.
    local side = MZ_SW - 16
    if MZ_SH - 86 < side then
        side = MZ_SH - 86
    end
    MZ_CELL = idiv(side, 21)
    if MZ_CELL < 3 then
        MZ_CELL = 3
    end
    MZ_OX = idiv(MZ_SW - 21 * MZ_CELL, 2)
    MZ_OY = 56 + idiv(MZ_SH - 86 - 21 * MZ_CELL, 2)
end

-- 每格是不是墙:偶数行整行通道;奇数行交替在左右端开口(蛇形,一定连通)
function is_wall(x, y)
    if y % 2 == 0 then
        return 0
    end
    local band = idiv(y, 2)
    if band % 2 == 0 then
        if x == 20 then
            return 0
        end
        return 1
    end
    if x == 0 then
        return 0
    end
    return 1
end

function new_maze()
    PX = 1
    PY = 0
    WON = 0
    STEPS = 0
end

function move_to(nx, ny)
    if WON ~= 0 then return 0 end
    if nx < 0 or ny < 0 or nx >= 21 or ny >= 21 then return 0 end
    if CELLS[nx * 21 + ny + 1] ~= 0 then return 0 end
    PX = nx
    PY = ny
    STEPS = STEPS + 1
    if nx == 19 and ny == 19 then WON = 1 end
    return 1
end

function draw()
    ui_2d_clear(-15724520)

    local x = 0
    while x < 21 do
        local y = 0
        while y < 21 do
            local px = MZ_OX + x * MZ_CELL
            local py = MZ_OY + y * MZ_CELL
            local col = -15066588
            if is_wall(x, y) ~= 0 then
                col = -14013898
            end
            ui_2d_rect(px, py, MZ_CELL, MZ_CELL, col, 1, 0, 0)
            y = y + 1
        end
        x = x + 1
    end
    ui_2d_rect(MZ_OX + 19 * MZ_CELL + 1, MZ_OY + 19 * MZ_CELL + 1, MZ_CELL - 2, MZ_CELL - 2, -2069424, 1, 0, 2)
    ui_2d_rect(MZ_OX + PX * MZ_CELL + 1, MZ_OY + PY * MZ_CELL + 1, MZ_CELL - 2, MZ_CELL - 2, -11409298, 1, 0, 2)

    draw_number(10, 12, STEPS, 4, -1513232)
    ui_2d_text(10, 46, T_STEPS, -6643536, 10, VML_ANCHOR_LEFT)

    if WON == 1 then
        ui_2d_text(idiv(MZ_SW, 2), MZ_OY + 21 * MZ_CELL + 18, T_WON, -1513232, 15, VML_ANCHOR_CENTER)
    else
        ui_2d_text(idiv(MZ_SW, 2), MZ_OY + 21 * MZ_CELL + 18, T_HINT, -6643536, 11, VML_ANCHOR_CENTER)
    end
    ui_2d_present()
end

function main()
    LANG = ui_scr_get_language()
    ui_win_open("迷宫", 0, 0)

    relayout()
    new_maze()
    draw()

    TID = ui_timer_set(120, 1)

    while ALIVE == 1 do
        local t = ui_msg_fetch_wait(120)
        if t == 10 then
            ALIVE = 0
        end

        if t == 1 then
            local k = ui_msg_get_a()
            if k == 27 then
                new_maze()
            elseif k == 37 then
                move_to(PX - 1, PY)
            elseif k == 39 then
                move_to(PX + 1, PY)
            elseif k == 38 then
                move_to(PX, PY - 1)
            elseif k == 40 then
                move_to(PX, PY + 1)
            end
        end

        if t == 6 or t == 4 then
            if WON == 1 then
                new_maze()
            else
                local tx = ui_msg_get_a()
                local ty = ui_msg_get_b()
                local dl = tx
                local dr = MZ_SW - tx
                local dt = ty
                local db = MZ_SH - ty
                if dl <= dr and dl <= dt and dl <= db then
                    move_to(PX - 1, PY)
                elseif dr <= dt and dr <= db then
                    move_to(PX + 1, PY)
                elseif dt <= db then
                    move_to(PX, PY - 1)
                else
                    move_to(PX, PY + 1)
                end
            end
        end

        if t == 11 then
            relayout()
        end

        if t == 9 then
            FRAMES = FRAMES + 1
            if FRAMES >= 12000 then ALIVE = 0 end
        end

        draw()
    end

    ui_timer_kill(TID)
    ui_win_close()
end

main()