csharp/app/maze.cs · 12093 字节 · 下载原文
// 迷宫 —— 用 **C#** 写的手机小游戏
// Maze — a small phone game written in **C#**
//
// 玩法(与 `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/csharp/app/snake.cs` 文件头)
// ## Style constraints (same as the header of `Examples/csharp/app/snake.cs`)
//
// · 常量写**数字**、颜色写**负数十进制**;
// · constants are **numbers** and colours are **negative decimals**;
// · 数字转字符串走本文件的 `numToStr()` / `digit()`;
// · numbers to strings go through `numToStr()` / `digit()` here;
// · 循环更新写 `i = i + 1`。
// · loop updates are written `i = i + 1`.
//
// 跑法 —— 手机:`vml run examples/csharp/maze.cs`
// How to run — on the phone: `vml run examples/csharp/maze.cs`
// 跑法 —— 桌面:`vmlcli Examples/csharp/maze.cs --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/csharp/maze.cs --screen 480x640 --frames /tmp/fr`
// 显式引用 UI 库:`ui_*` 的声明在 `Lib/csharp/VmlUi.cs`(tools/GenLib 生成,别手改)。
// Explicit reference to the UI library: `ui_*` is declared in `Lib/csharp/VmlUi.cs` (generated by tools/GenLib; don't hand-edit).
using Vml.Ui;
class MazeGame
{
// ── 常量(源:Lib/c/vml_ui.h)──
// ── Constants (source: Lib/c/vml_ui.h) ──
const int MSG_KEYDOWN = 1;
const int MSG_MOUSEDOWN = 4;
const int MSG_TOUCHDOWN = 6;
const int MSG_TIMER = 9;
const int MSG_WINDOWCLOSE = 10;
const int MSG_WINDOWRESIZE = 11;
const int KEY_ESCAPE = 27;
const int KEY_LEFT = 37;
const int KEY_UP = 38;
const int KEY_RIGHT = 39;
const int KEY_DOWN = 40;
const int ANCHOR_LEFT = 0;
const int ANCHOR_CENTER = 1;
const int MAX_FRAMES = 12000;
const int MW = 21;
const int MH = 21;
const int MAPCELLS = 441; // MW * MH
const int STACK_MAX = 512;
const int C_BG = -15724520; // 0xFF101018
const int C_WALL = -14013898; // 0xFF2A2A36
const int C_PATH = -15066588; // 0xFF1A1A24
const int C_PLAYER = -11409298; // 0xFF51E86E
const int C_GOAL = -2069424; // 0xFFE06C50
const int C_TEXT = -1513232; // 0xFFE8E8F0
const int C_DIM = -6643536; // 0xFF9AA0B0
static int lang;
static int sw = 360;
static int sh = 620;
static int cell = 16;
static int ox = 0;
static int oy = 110;
static int[] cells = new int[MAPCELLS];
static int[] stX = new int[STACK_MAX];
static int[] stY = new int[STACK_MAX];
static int[] dirs = new int[4];
static int px = 1;
static int py = 1;
static int won = 0;
static int steps = 0;
static int seed = 20268;
// 数字 → 字符串。**不能借标准库的字符串转换**:那是 C 的运行库,别的前端不一定有。
// Number → string. **Cannot borrow the standard library's string conversion**: that is C's runtime library.
static string numToStr(int v)
{
if (v == 0) return "0";
string s = "";
int neg = 0;
if (v < 0) { neg = 1; v = 0 - v; }
while (v > 0)
{
int d = v % 10;
s = digit(d) + s;
v = v / 10;
}
if (neg != 0) s = "-" + s;
return s;
}
static string digit(int d)
{
if (d == 0) return "0";
if (d == 1) return "1";
if (d == 2) return "2";
if (d == 3) return "3";
if (d == 4) return "4";
if (d == 5) return "5";
if (d == 6) return "6";
if (d == 7) return "7";
if (d == 8) return "8";
return "9";
}
// 确定性伪随机(验收要能复现同一张迷宫)
// Deterministic PRNG (acceptance needs to reproduce the same maze)
static int rnd(int n)
{
seed = (seed * 1103 + 12345) % 32768;
return (seed / 256) % n;
}
static void layout()
{
sw = ui_scr_get_w();
sh = ui_scr_get_h();
if (sw <= 0) sw = 360;
if (sh <= 0) sh = 620;
// 迷宫边长取「宽度可用」与「高度可用」中小的那个;240x240 这类方屏上
// 原来的绝对阈值会把迷宫压成 70px。
// The maze side is the smaller of the available width and height; the old absolute
// threshold squashed it to 70px on square screens such as 240x240.
int topReserve = 60; // 顶部步数 HUD
int botReserve = 44; // 迷宫下方提示行
int maxW = sw - 16;
int maxH = sh - topReserve - botReserve;
int side = maxW < maxH ? maxW : maxH;
cell = side / MW;
if (cell < 3) cell = 3;
ox = (sw - MW * cell) / 2;
oy = 110;
if (oy + MH * cell > sh - botReserve)
oy = topReserve + (maxH - MH * cell) / 2;
if (oy < topReserve) oy = topReserve;
}
// 深度优先挖洞:从 (1,1) 出发,每次跳两格、挖通中间那格
// Depth-first carving from (1,1); each step jumps two cells and opens the one between
static void generate()
{
int x = 0;
while (x < MW)
{
int y = 0;
while (y < MH)
{
cells[x * MH + y] = 1;
y = y + 1;
}
x = x + 1;
}
int top = 0;
int x0 = 1;
int y0 = 1;
cells[x0 * MH + y0] = 0;
stX[top] = x0;
stY[top] = y0;
top = top + 1;
while (top > 0)
{
int cx = stX[top - 1];
int cy = stY[top - 1];
int n = 0;
// 找出四个方向里"目标格还是墙"的那些(那种才没挖过)
// Collect directions whose two-step target is still all wall (not carved yet)
if (cx + 2 < MW - 1 && cells[(cx + 2) * MH + cy] == 1) { dirs[n] = 0; n = n + 1; }
if (cx - 2 > 0 && cells[(cx - 2) * MH + cy] == 1) { dirs[n] = 1; n = n + 1; }
if (cy + 2 < MH - 1 && cells[cx * MH + cy + 2] == 1) { dirs[n] = 2; n = n + 1; }
if (cy - 2 > 0 && cells[cx * MH + cy - 2] == 1) { dirs[n] = 3; n = n + 1; }
if (n == 0)
{
// 死路:回退 / dead end: backtrack
top = top - 1;
continue;
}
int k = dirs[rnd(n)];
if (k == 0) { cells[(cx + 1) * MH + cy] = 0; cx = cx + 2; }
else if (k == 1) { cells[(cx - 1) * MH + cy] = 0; cx = cx - 2; }
else if (k == 2) { cells[cx * MH + cy + 1] = 0; cy = cy + 2; }
else { cells[cx * MH + cy - 1] = 0; cy = cy - 2; }
cells[cx * MH + cy] = 0;
if (top < STACK_MAX)
{
stX[top] = cx;
stY[top] = cy;
top = top + 1;
}
}
// 右下角出口强制打通(保证一定到得了)
// Force the bottom-right exit open (must be reachable)
int i = 0;
while (i < 3)
{
if (MW - 2 - i >= 0) cells[(MW - 2 - i) * MH + MH - 2] = 0;
if (MH - 2 - i >= 0) cells[(MW - 2) * MH + MH - 2 - i] = 0;
i = i + 1;
}
}
static void newGame()
{
generate();
px = 1;
py = 1;
won = 0;
steps = 0;
}
static void draw()
{
ui_2d_clear(C_BG);
int x = 0;
while (x < MW)
{
int y = 0;
while (y < MH)
{
int c = C_PATH;
if (cells[x * MH + y] == 1) c = C_WALL;
ui_2d_rect(ox + x * cell, oy + y * cell, cell, cell, c, 1, 0, 0);
y = y + 1;
}
x = x + 1;
}
ui_2d_rect(ox + (MW - 2) * cell + 1, oy + (MH - 2) * 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);
ui_2d_text(10, 24, numToStr(steps), C_TEXT, 15, ANCHOR_LEFT);
if (lang == 0) ui_2d_text(10, 46, "步数", C_DIM, 10, ANCHOR_LEFT);
else ui_2d_text(10, 46, "steps", C_DIM, 10, ANCHOR_LEFT);
if (won == 1)
{
if (lang == 0) ui_2d_text(sw / 2, oy + MH * cell + 28, "走出来了!点一下/ESC 换一张", C_TEXT, 15, ANCHOR_CENTER);
else ui_2d_text(sw / 2, oy + MH * cell + 28, "Out! Tap / ESC for a new maze", C_TEXT, 15, ANCHOR_CENTER);
}
else
{
if (lang == 0) ui_2d_text(sw / 2, oy + MH * cell + 28, "方向键 / 点屏幕四边,走到红格", C_DIM, 11, ANCHOR_CENTER);
else ui_2d_text(sw / 2, oy + MH * cell + 28, "Arrows / tap a screen edge; reach the red cell", C_DIM, 11, ANCHOR_CENTER);
}
ui_2d_present();
}
static void move(int dx, int dy)
{
if (won == 1) return;
int nx = px + dx;
int ny = py + dy;
if (nx < 0 || ny < 0 || nx >= MW || ny >= MH) return;
if (cells[nx * MH + ny] == 1) return;
px = nx;
py = ny;
steps = steps + 1;
if (px == MW - 2 && py == MH - 2) won = 1;
}
// 点屏幕四边:离哪边最近就往那边走
// A screen edge decides the direction (whichever side is nearest)
static void tap(int tx, int ty)
{
if (won == 1)
{
newGame();
return;
}
int dl = tx;
int dr = sw - tx;
int dt = ty;
int db = sh - ty;
if (dl <= dr && dl <= dt && dl <= db) move(-1, 0);
else if (dr <= dt && dr <= db) move(1, 0);
else if (dt <= db) move(0, -1);
else move(0, 1);
}
static void Main()
{
lang = ui_scr_get_language();
ui_win_open(lang == 0 ? "迷宫" : "Maze", 0, 0);
layout();
ui_scr_set_keep_on(1);
newGame();
draw();
int tid = ui_timer_set(120, 0);
int frames = 0;
int done = 0;
while (done == 0 && ui_win_is_closed() == 0)
{
int t = ui_msg_fetch_wait(0);
if (t == 0) continue;
if (t == MSG_WINDOWCLOSE) break;
if (t == MSG_TOUCHDOWN || t == MSG_MOUSEDOWN)
{
tap(ui_msg_get_a(), ui_msg_get_b());
draw();
continue;
}
if (t == MSG_KEYDOWN)
{
int k = ui_msg_get_a();
if (k == KEY_ESCAPE) newGame();
else if (k == KEY_LEFT) move(-1, 0);
else if (k == KEY_RIGHT) move(1, 0);
else if (k == KEY_UP) move(0, -1);
else if (k == KEY_DOWN) move(0, 1);
draw();
continue;
}
if (t == MSG_WINDOWRESIZE) { layout(); draw(); continue; }
if (t == MSG_TIMER)
{
frames = frames + 1;
if (frames >= MAX_FRAMES) done = 1;
continue;
}
}
ui_timer_kill(tid);
ui_scr_set_keep_on(0);
ui_win_close();
}
}