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

rust/app/maze.rs · 15926 字节 · 下载原文

▶ 在浏览器里试玩

// 迷宫 —— 用 **Rust** 写的手机小游戏
// Maze — a small phone game written in **Rust**
//
// 玩法(与 `Examples/c/app/maze.c` / `Examples/python/app/maze.py` 同一套):
// Gameplay (same rules as the C / Python versions):
//   · 每局**现生成**一张新迷宫(深度优先挖洞,一定连通、一定有路);
//   · every round **generates** a fresh maze (depth-first carving; always connected, always solvable);
//   · 方向键 / 点屏幕四边移动,走到右下角红格即通关;ESC / 通关后点一下换一张。
//   · arrow keys / tapping a screen edge move; reaching the bottom-right red cell wins;
//     ESC or a tap after winning gives a new maze.
//
// ## 生成用**显式栈**,不用递归
// ## Generation uses an **explicit stack**, not recursion
//
// 递归在这种规模上容易把固定额度的栈吃掉,症状是"跑到一半没了",很难查。
// Recursion at this size can eat the fixed stack budget and the symptom is "it just vanished
// halfway", which is hard to diagnose.
//
// ## Rust 这一路的写法约束(见 `Examples/rust/app/snake.rs` 文件头)
// ## Style constraints on this Rust path (see the header of `Examples/rust/app/snake.rs`)
//
//   · `#include <vml_ui.rs>` + `use vml_ui::*;` 两行都要;
//   · both `#include <vml_ui.rs>` and `use vml_ui::*;` are required;
//   · **循环变量在 `main` 顶部声明一次**(本前端不支持同名 `let` 的重复声明);
//   · **loop variables are declared once at the top of `main`** (no repeated `let` / shadowing);
//   · 数字用 `int_to_str`(共享静态缓冲,取一次立刻用掉)。
//   · numbers use `int_to_str` (one shared static buffer; take and use it at once).
//
// 跑法 —— 手机:`vml run examples/rust/maze.rs`
// How to run — on the phone: `vml run examples/rust/maze.rs`
// 跑法 —— 桌面:`vmlcli Examples/rust/maze.rs --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/rust/maze.rs --screen 480x640 --frames /tmp/fr`

#include <vml_ui.rs>
use vml_ui::*;

fn main() {
    let mut lang = ui_scr_get_language();
    let mut sw = 360;
    let mut sh = 620;
    let mut cell = 16;
    let mut ox = 0;
    let mut oy = 110;

    let c_bg = -15724520;        // 0xFF101018
    let c_wall = -14013898;      // 0xFF2A2A36
    let c_path = -15066588;      // 0xFF1A1A24
    let c_player = -11409298;    // 0xFF51E86E
    let c_goal = -2069424;       // 0xFFE06C50
    let c_text = -1513232;       // 0xFFE8E8F0
    let c_dim = -6643536;        // 0xFF9AA0B0

    // 迷宫状态:21×21 的格子图 + 256 格的显式栈(DFS 深度不超过约 220)
    // Maze state: a 21x21 cell map plus a 256-slot explicit stack (DFS depth stays under ~220)
    let mut cells = vec![0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
    let mut dirs = [0, 0, 0, 0];
    let mut px = 1;
    let mut py = 1;
    let mut won = 0;
    let mut steps = 0;
    let mut seed = 20268;
    let mut frames = 0;
    let mut done = 0;

    // 循环变量一次声明、之后复用
    // Loop variables are declared once and reused afterwards
    let mut i = 0;
    let mut x = 0;
    let mut y = 0;
    let mut top = 0;
    let mut n = 0;
    let mut k = 0;
    let mut cx = 0;
    let mut cy = 0;
    let mut nx = 0;
    let mut ny = 0;
    let mut dl = 0;
    let mut dr = 0;
    let mut dt = 0;
    let mut db = 0;
    let mut side = 0;
    let mut hud = "";

    lang = ui_scr_get_language();
    ui_win_open("迷宫", 0, 0);

    sw = ui_scr_get_w();
    sh = ui_scr_get_h();
    if sw <= 0 { sw = 360; }
    if sh <= 0 { sh = 620; }
    side = sw - 16;
    if sw >= sh - 150 { side = sh - 170; }
    cell = side / 21;
    if cell < 3 { cell = 3; }
    ox = (sw - 21 * cell) / 2;
    oy = 110;

    // ── 开一局:DFS 挖洞(显式栈),每局现生成 ──
    // ── New game: DFS carving (explicit stack), generated fresh every round ──
    x = 0;
    while x < 21 {
        y = 0;
        while y < 21 { cells[x * 21 + y] = 1; y = y + 1; }
        x = x + 1;
    }
    top = 0;
    cells[21 + 1] = 0;
    st_x[top] = 1;
    st_y[top] = 1;
    top = top + 1;
    while top > 0 {
        cx = st_x[top - 1];
        cy = st_y[top - 1];
        n = 0;
        // 找出四个方向里"目标格还是墙"的那些(那种才没挖过)
        if cx + 2 < 20 && cells[(cx + 2) * 21 + cy] == 1 { dirs[n] = 0; n = n + 1; }
        if cx - 2 > 0 && cells[(cx - 2) * 21 + cy] == 1 { dirs[n] = 1; n = n + 1; }
        if cy + 2 < 20 && cells[cx * 21 + cy + 2] == 1 { dirs[n] = 2; n = n + 1; }
        if cy - 2 > 0 && cells[cx * 21 + cy - 2] == 1 { dirs[n] = 3; n = n + 1; }
        if n == 0 {
            // 死路:回退 / dead end: backtrack
            top = top - 1;
        } else {
            seed = (seed * 1103 + 12345) % 32768;
            k = dirs[(seed / 256) % n];
            if k == 0 { cells[(cx + 1) * 21 + cy] = 0; cx = cx + 2; }
            else if k == 1 { cells[(cx - 1) * 21 + cy] = 0; cx = cx - 2; }
            else if k == 2 { cells[cx * 21 + cy + 1] = 0; cy = cy + 2; }
            else { cells[cx * 21 + cy - 1] = 0; cy = cy - 2; }
            cells[cx * 21 + cy] = 0;
            if top < 256 {
                st_x[top] = cx;
                st_y[top] = cy;
                top = top + 1;
            }
        }
    }
    // 右下角出口强制打通(保证一定到得了)
    i = 0;
    while i < 3 {
        if 21 - 2 - i >= 0 { cells[(21 - 2 - i) * 21 + 21 - 2] = 0; }
        if 21 - 2 - i >= 0 { cells[(21 - 2) * 21 + 21 - 2 - i] = 0; }
        i = i + 1;
    }

    let mut tid = ui_timer_set(120, 0);
    frames = 0;
    done = 0;

    while done == 0 && ui_win_is_closed() == 0 {
        let t = ui_msg_fetch_wait(0);
        if t == 0 { continue; }
        if t == 10 { break; }                    // MSG_WINDOWCLOSE

        if t == 1 {                              // MSG_KEYDOWN
            k = ui_msg_get_a();
            nx = px;
            ny = py;
            if k == 37 { nx = px - 1; }
            if k == 39 { nx = px + 1; }
            if k == 38 { ny = py - 1; }
            if k == 40 { ny = py + 1; }
            if k == 27 {
                // ESC:生成一张新迷宫(与开局同一段逻辑)
                seed = 20268;
                x = 0;
                while x < 21 {
                    y = 0;
                    while y < 21 { cells[x * 21 + y] = 1; y = y + 1; }
                    x = x + 1;
                }
                top = 0;
                cells[21 + 1] = 0;
                st_x[top] = 1;
                st_y[top] = 1;
                top = top + 1;
                while top > 0 {
                    cx = st_x[top - 1];
                    cy = st_y[top - 1];
                    n = 0;
                    if cx + 2 < 20 && cells[(cx + 2) * 21 + cy] == 1 { dirs[n] = 0; n = n + 1; }
                    if cx - 2 > 0 && cells[(cx - 2) * 21 + cy] == 1 { dirs[n] = 1; n = n + 1; }
                    if cy + 2 < 20 && cells[cx * 21 + cy + 2] == 1 { dirs[n] = 2; n = n + 1; }
                    if cy - 2 > 0 && cells[cx * 21 + cy - 2] == 1 { dirs[n] = 3; n = n + 1; }
                    if n == 0 {
                        top = top - 1;
                    } else {
                        seed = (seed * 1103 + 12345) % 32768;
                        k = dirs[(seed / 256) % n];
                        if k == 0 { cells[(cx + 1) * 21 + cy] = 0; cx = cx + 2; }
                        else if k == 1 { cells[(cx - 1) * 21 + cy] = 0; cx = cx - 2; }
                        else if k == 2 { cells[cx * 21 + cy + 1] = 0; cy = cy + 2; }
                        else { cells[cx * 21 + cy - 1] = 0; cy = cy - 2; }
                        cells[cx * 21 + cy] = 0;
                        if top < 256 { st_x[top] = cx; st_y[top] = cy; top = top + 1; }
                    }
                }
                i = 0;
                while i < 3 {
                    if 21 - 2 - i >= 0 { cells[(21 - 2 - i) * 21 + 21 - 2] = 0; }
                    if 21 - 2 - i >= 0 { cells[(21 - 2) * 21 + 21 - 2 - i] = 0; }
                    i = i + 1;
                }
                px = 1;
                py = 1;
                won = 0;
                steps = 0;
                nx = px;
                ny = py;
            }
            if won == 0 && (nx != px || ny != py) {
                if nx >= 0 && ny >= 0 && nx < 21 && ny < 21 && cells[nx * 21 + ny] == 0 {
                    px = nx;
                    py = ny;
                    steps = steps + 1;
                    if px == 19 && py == 19 { won = 1; }
                }
            }
        }

        if t == 6 || t == 4 {                    // MSG_TOUCHDOWN / MSG_MOUSEDOWN
            x = ui_msg_get_a();
            y = ui_msg_get_b();
            if won == 1 {
                // 通关后点一下换一张(同 ESC)
                seed = 20268;
                px = 1;
                py = 1;
                won = 0;
                steps = 0;
                // 直接用 ESC 那段代码太绕,这里只重置到起点并把迷宫重新生成一遍
                x = 0;
                while x < 21 {
                    y = 0;
                    while y < 21 { cells[x * 21 + y] = 1; y = y + 1; }
                    x = x + 1;
                }
                top = 0;
                cells[21 + 1] = 0;
                st_x[top] = 1;
                st_y[top] = 1;
                top = top + 1;
                while top > 0 {
                    cx = st_x[top - 1];
                    cy = st_y[top - 1];
                    n = 0;
                    if cx + 2 < 20 && cells[(cx + 2) * 21 + cy] == 1 { dirs[n] = 0; n = n + 1; }
                    if cx - 2 > 0 && cells[(cx - 2) * 21 + cy] == 1 { dirs[n] = 1; n = n + 1; }
                    if cy + 2 < 20 && cells[cx * 21 + cy + 2] == 1 { dirs[n] = 2; n = n + 1; }
                    if cy - 2 > 0 && cells[cx * 21 + cy - 2] == 1 { dirs[n] = 3; n = n + 1; }
                    if n == 0 {
                        top = top - 1;
                    } else {
                        seed = (seed * 1103 + 12345) % 32768;
                        k = dirs[(seed / 256) % n];
                        if k == 0 { cells[(cx + 1) * 21 + cy] = 0; cx = cx + 2; }
                        else if k == 1 { cells[(cx - 1) * 21 + cy] = 0; cx = cx - 2; }
                        else if k == 2 { cells[cx * 21 + cy + 1] = 0; cy = cy + 2; }
                        else { cells[cx * 21 + cy - 1] = 0; cy = cy - 2; }
                        cells[cx * 21 + cy] = 0;
                        if top < 256 { st_x[top] = cx; st_y[top] = cy; top = top + 1; }
                    }
                }
                i = 0;
                while i < 3 {
                    if 21 - 2 - i >= 0 { cells[(21 - 2 - i) * 21 + 21 - 2] = 0; }
                    if 21 - 2 - i >= 0 { cells[(21 - 2) * 21 + 21 - 2 - i] = 0; }
                    i = i + 1;
                }
            } else {
                // 点屏幕四边:离哪边最近就往那边走
                dl = x;
                dr = sw - x;
                dt = y;
                db = sh - y;
                nx = px;
                ny = py;
                if dl <= dr && dl <= dt && dl <= db { nx = px - 1; }
                else if dr <= dt && dr <= db { nx = px + 1; }
                else if dt <= db { ny = py - 1; }
                else { ny = py + 1; }
                if nx >= 0 && ny >= 0 && nx < 21 && ny < 21 && cells[nx * 21 + ny] == 0 {
                    px = nx;
                    py = ny;
                    steps = steps + 1;
                    if px == 19 && py == 19 { won = 1; }
                }
            }
        }

        if t == 11 {                             // MSG_WINDOWRESIZE
            sw = ui_scr_get_w();
            sh = ui_scr_get_h();
            if sw <= 0 { sw = 360; }
            if sh <= 0 { sh = 620; }
            side = sw - 16;
            if sw >= sh - 150 { side = sh - 170; }
            cell = side / 21;
            if cell < 3 { cell = 3; }
            ox = (sw - 21 * cell) / 2;
            oy = 110;
        }

        if t == 9 {                              // MSG_TIMER
            frames = frames + 1;
            if frames >= 12000 { done = 1; }
        }

        // ── 绘制(每帧重画)──
        ui_2d_clear(c_bg);
        x = 0;
        while x < 21 {
            y = 0;
            while y < 21 {
                if cells[x * 21 + y] == 1 {
                    ui_2d_rect(ox + x * cell, oy + y * cell, cell, cell, c_wall, 1, 0, 0);
                } else {
                    ui_2d_rect(ox + x * cell, oy + y * cell, cell, cell, c_path, 1, 0, 0);
                }
                y = y + 1;
            }
            x = x + 1;
        }
        ui_2d_rect(ox + 19 * cell + 1, oy + 19 * cell + 1, cell - 2, cell - 2, c_goal, 1, 0, 2);
        ui_2d_rect(ox + px * cell + 1, oy + py * cell + 1, cell - 2, cell - 2, c_player, 1, 0, cell / 2);

        hud = int_to_str(steps);
        ui_2d_text(10, 24, hud, c_text, 15, VML_ANCHOR_LEFT);
        if lang == 0 { ui_2d_text(10, 46, "步数", c_dim, 10, VML_ANCHOR_LEFT); }
        else { ui_2d_text(10, 46, "steps", c_dim, 10, VML_ANCHOR_LEFT); }

        if won == 1 {
            if lang == 0 { ui_2d_text(sw / 2, oy + 21 * cell + 28, "走出来了!点一下/ESC 换一张", c_text, 15, VML_ANCHOR_CENTER); }
            else { ui_2d_text(sw / 2, oy + 21 * cell + 28, "Out! Tap / ESC for a new maze", c_text, 15, VML_ANCHOR_CENTER); }
        } else {
            if lang == 0 { ui_2d_text(sw / 2, oy + 21 * cell + 28, "方向键 / 点屏幕四边,走到红格", c_dim, 11, VML_ANCHOR_CENTER); }
            else { ui_2d_text(sw / 2, oy + 21 * cell + 28, "Arrows / tap a screen edge; reach the red cell", c_dim, 11, VML_ANCHOR_CENTER); }
        }
        ui_2d_present();
    }

    ui_timer_kill(tid);
    ui_win_close();
}