typescript/app/puzzle15.ts · 8125 字节 · 下载原文
// puzzle15.ts —— 数字华容道 / 15 格滑块拼图(VML 的 TypeScript 前端)
// puzzle15.ts -- the 15-puzzle (VML's TypeScript frontend)
//
// 与 `Examples/c/app/puzzle15.c` / `python/app/puzzle15.py` **同一套玩法**:
// Same rules as the C / Python versions:
// 4×4 里 15 个数字 + 1 个空格;点空格**旁边**的块就滑过去;排成 1..15 即通关;
// 打乱用的是"从解好的局面随机走合法步" ⇒ 一定可解。
// 15 numbers + one blank; tap a tile next to the blank to slide it; 1..15 in order wins;
// the shuffle is random legal moves from the solved board => always solvable.
//
// ⚠ `/` 是浮点除法 ⇒ 全用 `~~(a / b)`;常量 `VML_MSG_*` 等不会被自动引入。
// ⚠ `/` is floating division => `~~(a / b)` everywhere; `VML_MSG_*` constants are not auto-imported.
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 = 6000;
const N: number = 4;
const C_BG: number = 0xFF101018;
const C_TILE: number = 0xFF4A90D9;
const C_TILE2: number = 0xFF3A6FA8;
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 blankX 7 blankY 8 moves 9 won 10 frames 11 seed
let S: number[] = [360, 620, 80, 20, 110, 0, 3, 3, 0, 0, 0, 20266];
let tile: number[] = [];
let i: number = 0;
while (i < N * N) {
tile[i] = 0;
i = i + 1;
}
function rnd(n: number): number {
S[11] = (S[11] * 1103 + 12345) % 32768;
return ~~(S[11] / 256) % n;
}
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; }
// 顶部信息区随画布缩放:小屏压到 62,大屏最多 120;底部给提示行留 38。
// Header band scales with the canvas: 62 px on tiny screens, capped at 120 on large ones.
let top: number = ~~(S[1] / 8);
if (top < 62) { top = 62; }
if (top > 120) { top = 120; }
let side: number = S[0] - 16;
let hside: number = S[1] - top - 38;
if (hside < side) { side = hside; }
if (side < 16) { side = 16; }
S[2] = ~~(side / N);
if (S[2] < 6) { S[2] = 6; }
S[3] = ~~((S[0] - N * S[2]) / 2);
S[4] = top;
}
function swapCells(ax: number, ay: number, bx: number, by: number): void {
let t: number = tile[ax * N + ay];
tile[ax * N + ay] = tile[bx * N + by];
tile[bx * N + by] = t;
}
// 合法步:空格与四邻里挑一个 / one legal move: pick a neighbour of the blank
function slideRandom(): number {
// ⚠ 局部变量名不要和**调用方正在用的**同名:本前端的函数局部量会落进同一段
// 全局数据区,`newGame` 的循环计数器 `k` 会被这里的 `k` 踩掉 ⇒ 洗牌循环跑不完
// (实测:卡死在 400 次循环里)。统一用不重名的局部量。
// ⚠ Do not reuse a local name that the **caller** is using: locals of this frontend
// land in one global data segment, so `newGame`'s loop counter `k` was clobbered by
// this `k` and the shuffle loop never finished (measured: stuck in the 400-iteration loop).
let dir: number = rnd(4);
let dx: number = 0;
let dy: number = 0;
if (dir === 0) { dx = 1; }
else if (dir === 1) { dx = -1; }
else if (dir === 2) { dy = 1; }
else { dy = -1; }
let nx: number = S[6] + dx;
let ny: number = S[7] + dy;
if (nx < 0 || ny < 0 || nx >= N || ny >= N) { return 0; }
swapCells(S[6], S[7], nx, ny);
S[6] = nx;
S[7] = ny;
return 1;
}
function newGame(): void {
let v: number = 1;
let x: number = 0;
while (x < N) {
let y: number = 0;
while (y < N) {
tile[x * N + y] = v;
v = v + 1;
y = y + 1;
}
x = x + 1;
}
tile[N * N - 1] = 0;
S[6] = N - 1;
S[7] = N - 1;
let sh: number = 0;
while (sh < 400) {
slideRandom(); // 只走合法步 ⇒ 局面一定可解 / legal moves only => always solvable
sh = sh + 1;
}
S[8] = 0;
S[9] = 0;
}
function checkWin(): number {
let v: number = 1;
let x: number = 0;
while (x < N) {
let y: number = 0;
while (y < N) {
if (x === N - 1 && y === N - 1) {
if (tile[x * N + y] === 0) { return 1; }
return 0;
}
if (tile[x * N + y] !== v) { return 0; }
v = v + 1;
y = y + 1;
}
x = x + 1;
}
return 1;
}
function draw(): void {
ui_2d_clear(C_BG);
// 提示行贴着棋盘下沿,但绝不越出画布 / hint sits under the board but never leaves the canvas
let hy: number = S[4] + N * S[2] + 28;
if (hy > S[1] - 12) { hy = S[1] - 12; }
let x: number = 0;
while (x < N) {
let y: number = 0;
while (y < N) {
let px: number = S[3] + x * S[2];
let py: number = S[4] + y * S[2];
let v: number = tile[x * N + y];
if (v === 0) {
ui_2d_rect(px + 2, py + 2, S[2] - 4, S[2] - 4, 0xFF16161E, 1, 0, 6);
} else {
// 排好的亮蓝、没回去的暗蓝 / tiles in place bright blue, the rest dark blue
let c: number = C_TILE2;
if ((v - 1) % N === x && ~~((v - 1) / N) === y) { c = C_TILE; }
ui_2d_rect(px + 2, py + 2, S[2] - 4, S[2] - 4, c, 1, 0, 8);
ui_2d_text(px + ~~(S[2] / 2), py + ~~(S[2] / 2) + ~~(S[2] / 8),
"" + v, C_TEXT, ~~(S[2] / 2), 1);
}
y = y + 1;
}
x = x + 1;
}
ui_2d_text(10, 22, "" + S[8], C_TEXT, 15, 0);
if (S[5] === 0) { ui_2d_text(10, 42, "步数", C_DIM, 10, 0); }
else { ui_2d_text(10, 42, "moves", C_DIM, 10, 0); }
if (S[9] === 1) {
if (S[5] === 0) { ui_2d_text(~~(S[0] / 2), hy, "拼好了!点一下再来", C_TEXT, 15, 1); }
else { ui_2d_text(~~(S[0] / 2), hy, "Solved! Tap for a new one", C_TEXT, 15, 1); }
} else {
if (S[5] === 0) { ui_2d_text(~~(S[0] / 2), hy, "点空格旁边的块", C_DIM, 12, 1); }
else { ui_2d_text(~~(S[0] / 2), hy, "Tap a tile next to the blank", C_DIM, 12, 1); }
}
ui_2d_present();
}
function tap(tx: number, ty: number): void {
if (S[9] === 1) {
newGame();
return;
}
let x: number = ~~((tx - S[3]) / S[2]);
let y: number = ~~((ty - S[4]) / S[2]);
if (x < 0 || y < 0 || x >= N || y >= N) { return; }
// 只认上下左右相邻的(斜着不算)/ only orthogonal neighbours
let adjacent: number = 0;
if (x === S[6] && (y === S[7] - 1 || y === S[7] + 1)) { adjacent = 1; }
if (y === S[7] && (x === S[6] - 1 || x === S[6] + 1)) { adjacent = 1; }
if (adjacent === 1) {
swapCells(S[6], S[7], x, y);
S[6] = x;
S[7] = y;
S[8] = S[8] + 1;
S[9] = checkWin();
}
}
// ── 主程序 / 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(120, 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(120); }
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();