typescript/app/minesweeper.ts · 10377 字节 · 下载原文
// minesweeper.ts —— 扫雷(VML 的 TypeScript 前端)
// minesweeper.ts -- Minesweeper (VML's TypeScript frontend)
//
// 与 `Examples/c/app/minesweeper.c` / `python/app/minesweeper.py` **同一套玩法**:
// Same rules as the C / Python versions:
// 点格子翻开;数字是周围 8 格的雷数;翻到 0 会**连锁展开**(显式队列,不用递归);
// 顶部按钮切「插旗模式」再点就是标雷;翻开所有非雷格即通关,踩雷结束。
// Tap to reveal; the number counts mines in the surrounding 8 cells; revealing a 0
// **floods** outward (explicit queue, no recursion); the top button switches to flag mode;
// revealing every non-mine cell wins, a mine ends it.
//
// ⚠ `/` 是浮点除法 ⇒ 所有坐标/下标用 `~~(a / b)`;`VML_MSG_*` / `VML_ANCHOR_*`
// 常量不会被自动引入 ⇒ 自己声明需要的几个。
// ⚠ `/` is floating division => every coordinate/index uses `~~(a / b)`; constants like
// `VML_MSG_*` / `VML_ANCHOR_*` are not auto-imported => declare the few we need.
const MSG_KEYDOWN: number = 1;
const MSG_MOUSEDOWN: number = 4;
const MSG_TOUCHDOWN: number = 6;
const MSG_TIMER: number = 9;
const MSG_CLOSE: number = 10;
const MSG_RESIZE: number = 11;
const KEY_ESC: number = 27;
const MAX_FRAMES: number = 4000;
const GW: number = 9;
const GH: number = 9;
const MINES: number = 10;
const QMAX: number = GW * GH;
const C_BG: number = 0xFF101018;
const C_HIDDEN: number = 0xFF2A2A36;
const C_OPEN: number = 0xFF1A1A24;
const C_FLAG: number = 0xFFE06C50;
const C_TEXT: number = 0xFFE8E8F0;
const C_DIM: number = 0xFF9AA0B0;
// S slots:
// 0 SW 1 SH 2 cell 3 ox 4 oy 5 lang 6 opened 7 dead 8 won 9 flagMode 10 frames 11 seed
let S: number[] = [360, 620, 36, 18, 96, 0, 0, 0, 0, 0, 0, 20264];
let mine: number[] = [];
let opn: number[] = [];
let flag: number[] = [];
let qx: number[] = [];
let qy: number[] = [];
let i: number = 0;
while (i < GW * GH) {
mine[i] = 0;
opn[i] = 0;
flag[i] = 0;
qx[i] = 0;
qy[i] = 0;
i = i + 1;
}
function rnd(n: number): number {
S[11] = (S[11] * 1103 + 12345) % 32768;
return ~~(S[11] / 256) % n;
}
function at(x: number, y: number): number {
return x * GH + y;
}
function relayout(): void {
S[0] = ui_scr_get_w();
S[1] = ui_scr_get_h();
if (S[0] <= 0) { S[0] = 360; }
if (S[1] <= 0) { S[1] = 620; }
// 小屏把顶栏压成一行(按钮 + 雷数),棋盘从 44 起;大屏保留宽松布局。
// Tiny screens use a one-row top bar (button + mine count); the board starts at 44.
let top: number = 96;
let bottom: number = 34;
if (S[1] < 400) { top = 44; bottom = 32; }
let side: number = S[0] - 16;
let hside: number = S[1] - top - bottom;
if (hside < side) { side = hside; }
if (side < 18) { side = 18; }
S[2] = ~~(side / GW);
if (S[2] < 6) { S[2] = 6; }
S[3] = ~~((S[0] - GW * S[2]) / 2);
S[4] = top;
}
function neighbors(x: number, y: number): number {
let n: number = 0;
let dy: number = -1;
while (dy <= 1) {
let dx: number = -1;
while (dx <= 1) {
if (dx !== 0 || dy !== 0) {
let nx: number = x + dx;
let ny: number = y + dy;
if (nx >= 0 && ny >= 0 && nx < GW && ny < GH) {
if (mine[at(nx, ny)] === 1) { n = n + 1; }
}
}
dx = dx + 1;
}
dy = dy + 1;
}
return n;
}
function newGame(): void {
let k: number = 0;
while (k < GW * GH) {
mine[k] = 0;
opn[k] = 0;
flag[k] = 0;
k = k + 1;
}
k = 0;
while (k < MINES) {
let x: number = rnd(GW);
let y: number = rnd(GH);
if (mine[at(x, y)] === 0) {
mine[at(x, y)] = 1;
k = k + 1;
}
}
S[6] = 0;
S[7] = 0;
S[8] = 0;
S[9] = 0;
}
// 连锁展开(显式队列)/ flood fill with an explicit queue
function reveal(x: number, y: number): void {
if (opn[at(x, y)] === 1 || flag[at(x, y)] === 1) { return; }
let head: number = 0;
let tail: number = 0;
qx[tail] = x;
qy[tail] = y;
tail = tail + 1;
while (head < tail) {
let cx: number = qx[head];
let cy: number = qy[head];
head = head + 1;
if (opn[at(cx, cy)] === 1 || flag[at(cx, cy)] === 1) { continue; }
opn[at(cx, cy)] = 1;
S[6] = S[6] + 1;
if (mine[at(cx, cy)] === 1) {
S[7] = 1;
return;
}
if (neighbors(cx, cy) !== 0) { continue; }
let dy: number = -1;
while (dy <= 1) {
let dx: number = -1;
while (dx <= 1) {
let nx: number = cx + dx;
let ny: number = cy + dy;
if (dx !== 0 || dy !== 0) {
if (nx >= 0 && ny >= 0 && nx < GW && ny < GH) {
if (opn[at(nx, ny)] === 0 && flag[at(nx, ny)] === 0) {
if (tail < QMAX) {
qx[tail] = nx;
qy[tail] = ny;
tail = tail + 1;
}
}
}
}
dx = dx + 1;
}
dy = dy + 1;
}
}
if (S[6] >= GW * GH - MINES) { S[8] = 1; }
}
function draw(): void {
ui_2d_clear(C_BG);
// 顶栏:模式按钮 + 剩余雷数 / top bar: mode button + mines left
let barY: number = 44;
let barH: number = 36;
let btnFs: number = 13;
if (S[1] < 400) { barY = 8; barH = 28; btnFs = 12; }
let barW: number = ~~(S[0] / 3);
let btnCX: number = 8 + ~~(barW / 2);
let btnCY: number = barY + ~~(barH / 2);
if (S[9] === 1) {
ui_2d_rect(8, barY, barW, barH, C_FLAG, 1, 0, 8);
if (S[5] === 0) { ui_2d_text(btnCX, btnCY, "插旗模式", 0xFF101018, btnFs, 1); }
else { ui_2d_text(btnCX, btnCY, "Flag mode", 0xFF101018, btnFs, 1); }
} else {
ui_2d_rect(8, barY, barW, barH, C_HIDDEN, 1, 0, 8);
if (S[5] === 0) { ui_2d_text(btnCX, btnCY, "翻开模式", C_TEXT, btnFs, 1); }
else { ui_2d_text(btnCX, btnCY, "Reveal mode", C_TEXT, btnFs, 1); }
}
let f: number = 0;
let k: number = 0;
while (k < GW * GH) {
if (flag[k] === 1) { f = f + 1; }
k = k + 1;
}
ui_2d_text(S[0] - 10, barY + ~~(barH * 0.22), "" + (MINES - f), C_FLAG, 16, 2);
if (S[5] === 0) { ui_2d_text(S[0] - 10, barY + ~~(barH * 0.78), "剩余雷数", C_DIM, 10, 2); }
else { ui_2d_text(S[0] - 10, barY + ~~(barH * 0.78), "mines left", C_DIM, 10, 2); }
let x: number = 0;
while (x < GW) {
let y: number = 0;
while (y < GH) {
let px: number = S[3] + x * S[2];
let py: number = S[4] + y * S[2];
if (opn[at(x, y)] === 1) {
let c: number = C_OPEN;
if (mine[at(x, y)] === 1) { c = C_FLAG; }
ui_2d_rect(px + 1, py + 1, S[2] - 2, S[2] - 2, c, 1, 0, 2);
if (mine[at(x, y)] === 0) {
let n: number = neighbors(x, y);
if (n > 0) {
ui_2d_text(px + ~~(S[2] / 2), py + ~~(S[2] / 2) + 5, "" + n, C_TEXT, ~~(S[2] / 2), 1);
}
}
} else {
ui_2d_rect(px + 1, py + 1, S[2] - 2, S[2] - 2, C_HIDDEN, 1, 0, 3);
if (flag[at(x, y)] === 1) {
ui_2d_circle(px + ~~(S[2] / 2), py + ~~(S[2] / 2), ~~(S[2] / 4), C_FLAG, 1, 0);
}
}
y = y + 1;
}
x = x + 1;
}
// 提示行贴着棋盘下沿,但绝不越出画布 / hint sits under the board but never leaves the canvas
let hy: number = S[4] + GH * S[2] + 26;
if (hy > S[1] - 12) { hy = S[1] - 12; }
if (S[7] === 1) {
if (S[5] === 0) { ui_2d_text(~~(S[0] / 2), hy, "踩雷了,点一下重来", C_FLAG, 14, 1); }
else { ui_2d_text(~~(S[0] / 2), hy, "Boom - tap to restart", C_FLAG, 14, 1); }
} else if (S[8] === 1) {
if (S[5] === 0) { ui_2d_text(~~(S[0] / 2), hy, "扫清了!点一下再来", C_TEXT, 14, 1); }
else { ui_2d_text(~~(S[0] / 2), hy, "Cleared! Tap for a new board", C_TEXT, 14, 1); }
} else {
if (S[5] === 0) { ui_2d_text(~~(S[0] / 2), hy, "点格子翻开 / 点顶部按钮插旗", C_DIM, 11, 1); }
else { ui_2d_text(~~(S[0] / 2), hy, "Tap a cell / tap the top button to flag", C_DIM, 11, 1); }
}
ui_2d_present();
}
function tap(tx: number, ty: number): void {
if (S[7] === 1 || S[8] === 1) {
newGame();
return;
}
let barY: number = 44;
let barH: number = 36;
if (S[1] < 400) { barY = 8; barH = 28; }
if (tx >= 8 && tx <= 8 + ~~(S[0] / 3) && ty >= barY && ty <= barY + barH) {
if (S[9] === 1) { S[9] = 0; } else { S[9] = 1; }
return;
}
let x: number = ~~((tx - S[3]) / S[2]);
let y: number = ~~((ty - S[4]) / S[2]);
if (x < 0 || y < 0 || x >= GW || y >= GH) { return; }
if (S[9] === 1) {
if (opn[at(x, y)] === 0) {
if (flag[at(x, y)] === 1) { flag[at(x, y)] = 0; } else { flag[at(x, y)] = 1; }
}
return;
}
reveal(x, y);
}
// ── 主程序 / main program ──
S[5] = ui_scr_get_language();
relayout();
ui_win_open_ex("扫雷 (ts)", 0, 0, 1, 0);
relayout();
newGame();
draw();
let tid: number = ui_timer_set(80, 1);
let done: number = 0;
let guardMain: number = 0;
while (done === 0 && ui_win_is_closed() === 0 && guardMain < 1000000) {
guardMain = guardMain + 1;
let t: number = ui_msg_fetch();
if (t === 0) { t = ui_msg_fetch_wait(80); }
if (t === 0) { continue; }
if (t === MSG_TOUCHDOWN || t === MSG_MOUSEDOWN) {
tap(ui_msg_get_a(), ui_msg_get_b());
draw();
continue;
}
if (t === MSG_KEYDOWN) {
if (ui_msg_get_a() === KEY_ESC) { newGame(); draw(); }
continue;
}
if (t === MSG_RESIZE) { relayout(); draw(); continue; }
if (t === MSG_TIMER) {
S[10] = S[10] + 1;
if (S[10] >= MAX_FRAMES) { done = 1; }
draw();
continue;
}
if (t === MSG_CLOSE) { break; }
}
ui_timer_kill(tid);
ui_win_close();