objc/app/maze.m · 10722 字节 · 下载原文
// 迷宫 —— 用 **ObjC** 写的手机小游戏
// Maze — a small phone game written in **ObjC**
//
// 玩法(与 `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.
//
// ## 写法约束(见 `Examples/objc/app/snake.m` 文件头)
// ## Style constraints (see the header of `Examples/objc/app/snake.m`)
//
// · 循环更新写 `i = i + 1`;颜色写**负数十进制**;
// · loop updates are `i = i + 1`; colours are **negative decimals**;
// · 状态放**一个**文件级数组 `A[]`,辅助函数一律无参(move/tap 的实参走 A[1481..1484])。
// · state lives in **one** file-level array `A[]`, and helper functions take no arguments
// (move/tap arguments come in through A[1481..1484]).
//
// 跑法 —— 手机:`vml run examples/objc/maze.m`
// How to run — on the phone: `vml run examples/objc/maze.m`
// 跑法 —— 桌面:`vmlcli Examples/objc/maze.m --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/objc/maze.m --screen 480x640 --frames /tmp/fr`
#include <vml_ui.h>
#import <vml_ui.h>
// 状态表(一个数组放全部):
// 0..440 = cells[x * MH + y](1 = 墙 / 0 = 通路)
// 441..952 = stX[512](DFS 显式栈)
// 953..1464 = stY[512]
// 1465..1468= dirs[4]
// 1469=px 1470=py 1471=won 1472=steps 1473=seed
// 1474=LANG 1475=SW 1476=SH 1477=CELL 1478=OX 1479=OY 1480=tid
// 1481=moveDx 1482=moveDy 1483=tapX 1484=tapY
int A[1485];
#define MAX_FRAMES 12000
#define MW 21
#define MH 21
#define MAPCELLS 441 // MW * MH
#define STACK_BASE 441
#define STACK_MAX 512
#define DIRS_BASE 1465
#define PX 1469
#define PY 1470
#define WON 1471
#define STEPS 1472
#define SEED 1473
#define LANG 1474
#define SW 1475
#define SH 1476
#define CELL 1477
#define OX 1478
#define OY 1479
#define TID 1480
#define MDX 1481
#define MDY 1482
#define TX 1483
#define TY 1484
#define C_BG -15724520 // 0xFF101018
#define C_WALL -14013898 // 0xFF2A2A36
#define C_PATH -15066588 // 0xFF1A1A24
#define C_PLAYER -11409298 // 0xFF51E86E
#define C_GOAL -2069424 // 0xFFE06C50
#define C_TEXT -1513232 // 0xFFE8E8F0
#define C_DIM -6643536 // 0xFF9AA0B0
// 整数 → 字符串(不引 std::string;`sprintf` 在各前端形态不一)
// Integer to string (no std::string; `sprintf` differs across front ends)
char g_num[12];
char* numstr(int v) {
int n;
int m;
char tmp[12];
n = 0;
m = 0;
if (v < 0) { g_num[0] = '-'; v = 0 - v; n = 1; }
if (v == 0) { g_num[n] = '0'; g_num[n + 1] = 0; return g_num; }
while (v > 0) { tmp[m] = (char)('0' + v % 10); v = v / 10; m = m + 1; }
while (m > 0) { m = m - 1; g_num[n] = tmp[m]; n = n + 1; }
g_num[n] = 0;
return g_num;
}
// 确定性伪随机(验收要能复现同一张迷宫)
// Deterministic PRNG (acceptance needs to reproduce the same maze)
int rnd(int n) {
A[SEED] = (A[SEED] * 1103 + 12345) % 32768;
return (A[SEED] / 256) % n;
}
void relayout() {
A[SW] = ui_scr_get_w();
A[SH] = ui_scr_get_h();
if (A[SW] <= 0) { A[SW] = 360; }
if (A[SH] <= 0) { A[SH] = 620; }
A[CELL] = A[SW] - 16;
if (A[SW] >= A[SH] - 150) { A[CELL] = A[SH] - 170; }
A[CELL] = A[CELL] / MW;
if (A[CELL] < 3) { A[CELL] = 3; }
A[OX] = (A[SW] - MW * A[CELL]) / 2;
A[OY] = 110;
}
// 深度优先挖洞:从 (1,1) 出发,每次跳两格、挖通中间那格
// Depth-first carving from (1,1); each step jumps two cells and opens the one between
void generate() {
int x;
int y;
int top;
int cx;
int cy;
int n;
int k;
int i;
x = 0;
while (x < MW) {
y = 0;
while (y < MH) {
A[x * MH + y] = 1;
y = y + 1;
}
x = x + 1;
}
top = 0;
A[1 * MH + 1] = 0;
A[STACK_BASE + top] = 1;
A[STACK_BASE + STACK_MAX + top] = 1;
top = top + 1;
while (top > 0) {
cx = A[STACK_BASE + top - 1];
cy = A[STACK_BASE + STACK_MAX + top - 1];
n = 0;
// 找出四个方向里"目标格还是墙"的那些(那种才没挖过)
// Collect directions whose two-step target is still all wall (not carved yet)
if (cx + 2 < MW - 1 && A[(cx + 2) * MH + cy] == 1) { A[DIRS_BASE + n] = 0; n = n + 1; }
if (cx - 2 > 0 && A[(cx - 2) * MH + cy] == 1) { A[DIRS_BASE + n] = 1; n = n + 1; }
if (cy + 2 < MH - 1 && A[cx * MH + cy + 2] == 1) { A[DIRS_BASE + n] = 2; n = n + 1; }
if (cy - 2 > 0 && A[cx * MH + cy - 2] == 1) { A[DIRS_BASE + n] = 3; n = n + 1; }
if (n == 0) {
// 死路:回退 / dead end: backtrack
top = top - 1;
continue;
}
k = A[DIRS_BASE + rnd(n)];
if (k == 0) { A[(cx + 1) * MH + cy] = 0; cx = cx + 2; }
else if (k == 1) { A[(cx - 1) * MH + cy] = 0; cx = cx - 2; }
else if (k == 2) { A[cx * MH + cy + 1] = 0; cy = cy + 2; }
else { A[cx * MH + cy - 1] = 0; cy = cy - 2; }
A[cx * MH + cy] = 0;
if (top < STACK_MAX) {
A[STACK_BASE + top] = cx;
A[STACK_BASE + STACK_MAX + top] = cy;
top = top + 1;
}
}
// 右下角出口强制打通(保证一定到得了)
// Force the bottom-right exit open (must be reachable)
i = 0;
while (i < 3) {
if (MW - 2 - i >= 0) { A[(MW - 2 - i) * MH + MH - 2] = 0; }
if (MH - 2 - i >= 0) { A[(MW - 2) * MH + MH - 2 - i] = 0; }
i = i + 1;
}
}
void newGame() {
generate();
A[PX] = 1;
A[PY] = 1;
A[WON] = 0;
A[STEPS] = 0;
}
void draw() {
int x;
int y;
int c;
ui_2d_clear(C_BG);
x = 0;
while (x < MW) {
y = 0;
while (y < MH) {
c = C_PATH;
if (A[x * MH + y] == 1) { c = C_WALL; }
ui_2d_rect(A[OX] + x * A[CELL], A[OY] + y * A[CELL], A[CELL], A[CELL], c, 1, 0, 0);
y = y + 1;
}
x = x + 1;
}
ui_2d_rect(A[OX] + (MW - 2) * A[CELL] + 1, A[OY] + (MH - 2) * A[CELL] + 1,
A[CELL] - 2, A[CELL] - 2, C_GOAL, 1, 0, 2);
ui_2d_rect(A[OX] + A[PX] * A[CELL] + 1, A[OY] + A[PY] * A[CELL] + 1,
A[CELL] - 2, A[CELL] - 2, C_PLAYER, 1, 0, A[CELL] / 2);
ui_2d_text(10, 24, numstr(A[STEPS]), C_TEXT, 15, VML_ANCHOR_LEFT);
if (A[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 (A[WON] == 1) {
if (A[LANG] == 0) { ui_2d_text(A[SW] / 2, A[OY] + MH * A[CELL] + 28, "走出来了!点一下/ESC 换一张", C_TEXT, 15, VML_ANCHOR_CENTER); }
else { ui_2d_text(A[SW] / 2, A[OY] + MH * A[CELL] + 28, "Out! Tap / ESC for a new maze", C_TEXT, 15, VML_ANCHOR_CENTER); }
} else {
if (A[LANG] == 0) { ui_2d_text(A[SW] / 2, A[OY] + MH * A[CELL] + 28, "方向键 / 点屏幕四边,走到红格", C_DIM, 11, VML_ANCHOR_CENTER); }
else { ui_2d_text(A[SW] / 2, A[OY] + MH * A[CELL] + 28, "Arrows / tap a screen edge; reach the red cell", C_DIM, 11, VML_ANCHOR_CENTER); }
}
ui_2d_present();
}
// move 的实参走 A[MDX]/A[MDY](辅助函数无参)
// move arguments come in through A[MDX]/A[MDY] (helpers take no arguments)
void move() {
int nx;
int ny;
if (A[WON] == 1) { return; }
nx = A[PX] + A[MDX];
ny = A[PY] + A[MDY];
if (nx < 0 || ny < 0 || nx >= MW || ny >= MH) { return; }
if (A[nx * MH + ny] == 1) { return; }
A[PX] = nx;
A[PY] = ny;
A[STEPS] = A[STEPS] + 1;
if (A[PX] == MW - 2 && A[PY] == MH - 2) { A[WON] = 1; }
}
// 点屏幕四边:离哪边最近就往那边走
// A screen edge decides the direction (whichever side is nearest)
void tap() {
int dl;
int dr;
int dt;
int db;
if (A[WON] == 1) {
newGame();
return;
}
dl = A[TX];
dr = A[SW] - A[TX];
dt = A[TY];
db = A[SH] - A[TY];
if (dl <= dr && dl <= dt && dl <= db) { A[MDX] = -1; A[MDY] = 0; move(); }
else if (dr <= dt && dr <= db) { A[MDX] = 1; A[MDY] = 0; move(); }
else if (dt <= db) { A[MDX] = 0; A[MDY] = -1; move(); }
else { A[MDX] = 0; A[MDY] = 1; move(); }
}
int main() {
int t;
int k;
int frames;
int done;
A[LANG] = ui_scr_get_language();
relayout();
if (A[LANG] == 0) { ui_win_open("迷宫", 0, 0); }
else { ui_win_open("Maze", 0, 0); }
relayout();
A[SEED] = 20268;
newGame();
draw();
A[TID] = ui_timer_set(120, 0);
frames = 0;
done = 0;
while (done == 0 && ui_win_is_closed() == 0) {
t = ui_msg_fetch_wait(0);
if (t == 0) { continue; }
if (t == 10) { break; } // MSG_WINDOWCLOSE
if (t == 6 || t == 4) { // MSG_TOUCHDOWN / MSG_MOUSEDOWN
A[TX] = ui_msg_get_a();
A[TY] = ui_msg_get_b();
tap();
draw();
continue;
}
if (t == 1) { // MSG_KEYDOWN
k = ui_msg_get_a();
if (k == 27) { newGame(); }
else if (k == 37) { A[MDX] = -1; A[MDY] = 0; move(); }
else if (k == 39) { A[MDX] = 1; A[MDY] = 0; move(); }
else if (k == 38) { A[MDX] = 0; A[MDY] = -1; move(); }
else if (k == 40) { A[MDX] = 0; A[MDY] = 1; move(); }
draw();
continue;
}
if (t == 11) { relayout(); draw(); continue; } // MSG_WINDOWRESIZE
if (t == 12) { continue; } // MSG_WINDOWORIENT
if (t == 9) { // MSG_TIMER
frames = frames + 1;
if (frames >= MAX_FRAMES) { done = 1; }
continue;
}
}
ui_timer_kill(A[TID]);
ui_win_close();
return 0;
}