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

lua/app/puzzle15.lua · 7589 字节 · 下载原文

▶ 在浏览器里试玩

-- 十五格拼图 —— 用 **Lua** 写的手机小游戏
-- Puzzle 15 — a small phone game written in **Lua**
--
-- 玩法(与 Examples/c/app/puzzle15.c / Examples/python/app/puzzle15.py 同一套):
--   4×4 共 15 个数字加一个空格;点空格旁边的格子(或按方向键)把格子滑进空格;
--   把 1..15 按顺序排好、空格落在右下角即通关;打乱靠随机走步(一定可解)。
--
-- ## 本语言的实测约束(见 Examples/lua/app/snake.lua / memory.lua 文件头)
--   · 全局状态**必须写在函数定义之前**;入口是 `main()`;
--   · **没有整数除法**:`/` 返回浮点、UI 参数按位型解释 ⇒ 一律 `idiv`;
--   · 逻辑运算符是 `or` / `and`;`!=` 是词法错误,写 `~=`;裸 `return` 写 `return 0`;
--   · 表先按字面量建(`{}` 只有 8 个键位);数字上屏走**点阵**。
--
-- 跑法 —— 手机:vml run examples/lua/puzzle15.lua
-- 跑法 —— 桌面:vmlcli Examples/lua/puzzle15.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 = 80
MZ_OX = 20
MZ_OY = 110
SEED = 20264
MOVES = 0
BLANK = 15
WON = 0
FRAMES = 0
LANG = 0
ALIVE = 1
T_MOVES = "步数"
T_SOLVED = "排好了!"
T_HINT = "点空格旁的数字 / 方向键滑动"
BOARD = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 pz_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
    -- 4x4 棋盘按宽、高两个上界取小者,并在剩余高度里垂直居中
    -- The 4x4 board takes the smaller of the width/height bounds and is centred vertically.
    local side = MZ_SW - 20
    if MZ_SH - 86 < side then
        side = MZ_SH - 86
    end
    MZ_CELL = idiv(side, 4)
    if MZ_CELL < 6 then
        MZ_CELL = 6
    end
    MZ_OX = idiv(MZ_SW - 4 * MZ_CELL, 2)
    MZ_OY = 56 + idiv(MZ_SH - 86 - 4 * MZ_CELL, 2)
end

function new_game()
    local i = 0
    while i < 16 do
        BOARD[i + 1] = i + 1
        i = i + 1
    end
    BOARD[16] = 0
    BLANK = 15
    MOVES = 0
    WON = 0
    local n = 0
    while n < 200 do
        local r = pz_rnd(4)
        local bx = BLANK % 4
        local by = idiv(BLANK, 4)
        local j = BLANK
        if r == 0 and bx > 0 then
            j = BLANK - 1
        end
        if r == 1 and bx < 3 then
            j = BLANK + 1
        end
        if r == 2 and by > 0 then
            j = BLANK - 4
        end
        if r == 3 and by < 3 then
            j = BLANK + 4
        end
        if j ~= BLANK then
            BOARD[BLANK + 1] = BOARD[j + 1]
            BOARD[j + 1] = 0
            BLANK = j
        end
        n = n + 1
    end
end

function check_win()
    local i = 0
    while i < 15 do
        if BOARD[i + 1] ~= i + 1 then
            return 0
        end
        i = i + 1
    end
    if BOARD[16] ~= 0 then
        return 0
    end
    return 1
end

function draw()
    ui_2d_clear(-15724520)

    local i = 0
    while i < 16 do
        local px = MZ_OX + (i % 4) * MZ_CELL
        local py = MZ_OY + idiv(i, 4) * MZ_CELL
        if BOARD[i + 1] == 0 then
            ui_2d_rect(px + 2, py + 2, MZ_CELL - 4, MZ_CELL - 4, -15066588, 1, 0, 4)
        else
            local col = -14013898
            if i == BOARD[i + 1] - 1 then
                col = -12960184
            end
            ui_2d_rect(px + 2, py + 2, MZ_CELL - 4, MZ_CELL - 4, col, 1, 0, 4)
            draw_number(px + idiv(MZ_CELL, 2) - idiv(MZ_CELL, 6), py + idiv(MZ_CELL, 2),
                BOARD[i + 1], idiv(MZ_CELL, 10), -1513232)
        end
        i = i + 1
    end

    draw_number(10, 12, MOVES, 4, -1513232)
    ui_2d_text(10, 46, T_MOVES, -6643536, 10, VML_ANCHOR_LEFT)

    if WON == 1 then
        ui_2d_text(idiv(MZ_SW, 2), MZ_OY + 4 * MZ_CELL + 18, T_SOLVED, -11409298, 16, VML_ANCHOR_CENTER)
    else
        ui_2d_text(idiv(MZ_SW, 2), MZ_OY + 4 * 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()
    SEED = 20264
    new_game()
    draw()

    TID = ui_timer_set(60, 1)

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

        if t == 1 then
            local k = ui_msg_get_a()
            if k == 27 then
                ALIVE = 0
            end
            local bx = BLANK % 4
            local by = idiv(BLANK, 4)
            local j = BLANK
            if k == 37 and bx < 3 then
                j = BLANK + 1
            end
            if k == 39 and bx > 0 then
                j = BLANK - 1
            end
            if k == 38 and by < 3 then
                j = BLANK + 4
            end
            if k == 40 and by > 0 then
                j = BLANK - 4
            end
            if j ~= BLANK then
                BOARD[BLANK + 1] = BOARD[j + 1]
                BOARD[j + 1] = 0
                BLANK = j
                MOVES = MOVES + 1
            end
        end

        if t == 6 or t == 4 then
            local tx = ui_msg_get_a()
            local ty = ui_msg_get_b()
            if tx >= MZ_OX and ty >= MZ_OY then
                local cx = idiv(tx - MZ_OX, MZ_CELL)
                local cy = idiv(ty - MZ_OY, MZ_CELL)
                if cx >= 0 and cy >= 0 and cx < 4 and cy < 4 then
                    local j3 = cy * 4 + cx
                    local bx3 = BLANK % 4
                    local by3 = idiv(BLANK, 4)
                    if (cx == bx3 and (cy == by3 + 1 or cy == by3 - 1)) or (cy == by3 and (cx == bx3 + 1 or cx == bx3 - 1)) then
                        BOARD[BLANK + 1] = BOARD[j3 + 1]
                        BOARD[j3 + 1] = 0
                        BLANK = j3
                        MOVES = MOVES + 1
                    end
                end
            end
        end

        if t == 11 then
            relayout()
        end

        WON = check_win()

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

        draw()
    end

    ui_timer_kill(TID)
    ui_win_close()
end

main()