dart/app/maze.dart · 13658 字节 · 下载原文
// 迷宫(Maze)—— Dart 版小游戏
// Maze — a small game in the Dart edition
//
// 玩法(与 `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.
//
// ## Dart 这一路的写法约束(见 `Examples/dart/app/snake.dart` 文件头)
// ## Style constraints on this Dart path (see the header of `Examples/dart/app/snake.dart`)
//
// · **全部状态放 `main` 的局部变量**;颜色写十六进制 `0xAARRGGBB`;整除写 `~/`;
// · **all state lives in locals of `main`**; colours are hexadecimal `0xAARRGGBB`; integer division is `~/`;
// · 数字用**字符串插值**。
// · numbers go through **string interpolation**.
//
// 跑法 —— 手机:`vml run examples/dart/maze.dart`
// How to run — on the phone: `vml run examples/dart/maze.dart`
// 跑法 —— 桌面:`vmlcli Examples/dart/maze.dart --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/dart/maze.dart --screen 480x640 --frames /tmp/fr`
import 'vml_ui.dart';
void main() {
// ── 全部状态都放 main 的局部变量(见文件头)──
int sw = 360;
int sh = 620;
int cell = 16;
int ox = 0;
int oy = 110;
int lang = 0;
int i = 0;
int x = 0;
int y = 0;
int top = 0;
int n = 0;
int k = 0;
int t = 0;
int frames = 0;
int done = 0;
int cx = 0;
int cy = 0;
int px = 1;
int py = 1;
int won = 0;
int steps = 0;
int seed = 20268;
int nx = 0;
int ny = 0;
int dl = 0;
int dr = 0;
int dt = 0;
int db = 0;
int c = 0;
String hud = "";
int cBg = 0xFF101018;
int cWall = 0xFF2A2A36;
int cPath = 0xFF1A1A24;
int cPlayer = 0xFF51E86E;
int cGoal = 0xFFE06C50;
int cText = 0xFFE8E8F0;
int cDim = 0xFF9AA0B0;
// 迷宫状态:21×21 的格子图 + 512 格的显式栈
// Maze state: a 21x21 cell map plus a 512-slot explicit stack
// List.generate 一次分配(等价于逐个 add(0)):小屏入口脚本 350ms 就发 close,
// 逐个 add 要 ~750ms,窗口还没开就被关掉 ⇒ 导出帧为空。
// List.generate allocates in one go (same values as repeated add(0)): the entry script
// sends `close` 350 ms in and the add loop alone took ~750 ms here, so the window was
// already closed before the first frame could be presented.
List<int> cells = List.generate(441, (idx) => 0);
List<int> stX = List.generate(512, (idx) => 0);
List<int> stY = List.generate(512, (idx) => 0);
List<int> dirs = [0, 0, 0, 0];
lang = ui_scr_get_language();
ui_win_open_ex("迷宫", 0, 0, 1, 1);
sw = ui_scr_get_w();
sh = ui_scr_get_h();
if (sw <= 0) { sw = 360; }
if (sh <= 0) { sh = 620; }
// 宽高两头夹:小屏把顶部空白压掉,大屏不让迷宫顶出底部提示
// Clamp on both axes: small screens drop the top gap, big screens keep the maze above the hint.
int side = sw - 16;
int sideH = sh - 64 - 36;
if (sideH < side) { side = sideH; }
cell = side ~/ 21;
if (cell < 3) { cell = 3; }
ox = (sw - 21 * cell) ~/ 2;
oy = 64;
// ── 开一局: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[1 * 21 + 1] = 0;
stX[top] = 1;
stY[top] = 1;
top = top + 1;
while (top > 0) {
cx = stX[top - 1];
cy = stY[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 < 512) {
stX[top] = cx;
stY[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;
// 先画一帧再进事件循环:21×21 的 DFS 生成可能比入口脚本的 350ms 还久,
// 若先取消息会把 close 排在首帧之前 ⇒ 导出帧为空(NOFRAME)。
// Draw one frame before the event loop: generating the 21x21 maze can outlast the
// 350 ms entry script; fetching messages first would consume `close` before any present.
ui_2d_clear(cBg);
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, cWall, 1, 0, 0);
} else {
ui_2d_rect(ox + x * cell, oy + y * cell, cell, cell, cPath, 1, 0, 0);
}
y = y + 1;
}
x = x + 1;
}
ui_2d_rect(ox + 19 * cell + 1, oy + 19 * cell + 1, cell - 2, cell - 2, cGoal, 1, 0, 2);
ui_2d_rect(ox + px * cell + 1, oy + py * cell + 1, cell - 2, cell - 2, cPlayer, 1, 0, cell ~/ 2);
ui_2d_present();
int tid = ui_timer_set(120, 1);
frames = 0;
done = 0;
while (done == 0 && ui_win_is_closed() == 0) {
t = ui_msg_fetch_wait(120);
if (t == 0) { continue; }
if (t == 10) { break; } // MSG_WINDOWCLOSE
if (t == 1) { // MSG_KEYDOWN
k = ui_msg_get_a();
// 方向键移动(与其它语言版同一套键码)
// Arrow-key movement (same key codes as the other language versions)
if (k == 37) { nx = px - 1; ny = py; }
else if (k == 39) { nx = px + 1; ny = py; }
else if (k == 38) { nx = px; ny = py - 1; }
else if (k == 40) { nx = px; ny = py + 1; }
else { nx = px; ny = py; }
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;
stX[top] = 1;
stY[top] = 1;
top = top + 1;
while (top > 0) {
cx = stX[top - 1];
cy = stY[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 < 512) { stX[top] = cx; stY[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)
// Tap after winning gives a new maze (same as 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;
stX[top] = 1;
stY[top] = 1;
top = top + 1;
while (top > 0) {
cx = stX[top - 1];
cy = stY[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 < 512) { stX[top] = cx; stY[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;
} else {
// 点屏幕四边:离哪边最近就往那边走
// A screen edge decides the direction (whichever side is nearest)
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;
sideH = sh - 64 - 36;
if (sideH < side) { side = sideH; }
cell = side ~/ 21;
if (cell < 3) { cell = 3; }
ox = (sw - 21 * cell) ~/ 2;
oy = 64;
}
if (t == 9) { // MSG_TIMER
frames = frames + 1;
if (frames >= 12000) { done = 1; }
}
// ── 绘制(每帧重画)──
ui_2d_clear(cBg);
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, cWall, 1, 0, 0);
} else {
ui_2d_rect(ox + x * cell, oy + y * cell, cell, cell, cPath, 1, 0, 0);
}
y = y + 1;
}
x = x + 1;
}
ui_2d_rect(ox + 19 * cell + 1, oy + 19 * cell + 1, cell - 2, cell - 2, cGoal, 1, 0, 2);
ui_2d_rect(ox + px * cell + 1, oy + py * cell + 1, cell - 2, cell - 2, cPlayer, 1, 0, cell ~/ 2);
hud = "$steps";
ui_2d_text(10, 24, hud, cText, 15, 0);
if (lang == 0) { ui_2d_text(10, 46, "步数", cDim, 10, 0); }
else { ui_2d_text(10, 46, "steps", cDim, 10, 0); }
int msgY = oy + 21 * cell + 24;
if (msgY > sh - 16) { msgY = sh - 16; }
if (won == 1) {
if (lang == 0) { ui_2d_text(sw ~/ 2, msgY, "走出来了!点一下/ESC 换一张", cText, 15, 1); }
else { ui_2d_text(sw ~/ 2, msgY, "Out! Tap / ESC for a new maze", cText, 15, 1); }
} else {
if (lang == 0) { ui_2d_text(sw ~/ 2, msgY, "方向键 / 点屏幕四边,走到红格", cDim, 11, 1); }
else { ui_2d_text(sw ~/ 2, msgY, "Arrows / tap a screen edge; reach the red cell", cDim, 11, 1); }
}
ui_2d_present();
}
ui_timer_kill(tid);
ui_win_close();
}