objc/app/minesweeper.m · 11413 字节 · 下载原文
// 扫雷 —— 用 **ObjC** 写的手机小游戏(网格推理)
// Minesweeper — a small phone game written in **ObjC** (grid deduction)
//
// 玩法(与 `Examples/c/app/minesweeper.c` / `Examples/python/app/minesweeper.py` 同一套):
// Gameplay (same rules as the C / Python versions):
// · 9×9 共 10 颗雷;点格子翻开,数字是周围 8 格的雷数;
// · 9x9 with ten mines; tap a cell to reveal it, the number counts mines in the surrounding 8 cells;
// · 翻到 0 会**连锁展开**;顶部按钮切到「插旗」模式再点就是标雷;
// · revealing a 0 **floods** outward; the top button switches to "flag" mode and taps then mark mines;
// · 把所有非雷格子翻开即通关,踩到雷就结束;结束后按 ESC / R 重开。
// · revealing every non-mine cell wins, stepping on a mine ends it; ESC / R restarts.
//
// ## 写法约束(见 `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`; arrays have no initializer lists — assign element by element;
// · 颜色写负数十进制;状态放**一个**文件级数组 `A[]`,辅助函数一律无参。
// · colours are negative decimals; state lives in **one** file-level array `A[]`, helpers take no arguments.
//
// 跑法 —— 手机:`vml run examples/objc/minesweeper.m`
// How to run — on the phone: `vml run examples/objc/minesweeper.m`
// 跑法 —— 桌面:`vmlcli Examples/objc/minesweeper.m --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/objc/minesweeper.m --screen 480x640 --frames /tmp/fr`
#include <vml_ui.h>
#import <vml_ui.h>
// 状态表:
// 0..80 = 雷;81..161 = 翻开;162..242 = 旗
// 243..323 = 队列 X;324..404 = 队列 Y
// 405=OPENED 406=DEAD 407=WON 408=FLAGMODE 409=FRAMES 410=CELL 411=OX 412=OY
// 413=SW 414=SH 415=LANG 416=TID 417=I 418=HEAD 419=TAIL 420=CX 421=CY
// 422=DX 423=DY 424=NX 425=NY 426=N 427=FLAGS 428=TX 429=TY 430=SEED
// 431=X 432=Y 433=T 434=BTN_Y 435=BTN_H 436=FOOT_Y 437=CELL_H
int A[438];
#define GW 9
#define MINES 10
#define MAX_FRAMES 4000
#define C_BG -15724520 // 0xFF101018
#define C_HIDDEN -14013898 // 0xFF2A2A36
#define C_OPEN -15066588 // 0xFF1A1A24
#define C_FLAG -2069424 // 0xFFE06C50
#define C_TEXT -1513232 // 0xFFE8E8F0
#define C_DIM -6643536 // 0xFF9AA0B0
#define C_MINE -2069424 // 0xFFE06C50
char g_num[12];
char* numstr(int v) {
int n = 0;
int m = 0;
char tmp[12];
if (v < 0) { g_num[0] = '-'; v = -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;
}
int idx() {
return A[431] * GW + A[432];
}
// 周围 8 格的雷数
int neighbors() {
A[426] = 0;
A[422] = -1;
while (A[422] <= 1) {
A[423] = -1;
while (A[423] <= 1) {
if (!(A[422] == 0 && A[423] == 0)) {
A[424] = A[431] + A[422];
A[425] = A[432] + A[423];
if (A[424] >= 0 && A[425] >= 0 && A[424] < GW && A[425] < GW) {
if (A[A[424] * GW + A[425]] == 1) { A[426] = A[426] + 1; }
}
}
A[423] = A[423] + 1;
}
A[422] = A[422] + 1;
}
return A[426];
}
void relayout() {
A[413] = ui_scr_get_w();
A[414] = ui_scr_get_h();
if (A[413] <= 0) { A[413] = 360; }
if (A[414] <= 0) { A[414] = 620; }
// 小屏压缩表头,把高度让给 9 行雷区
if (A[414] < 360) {
A[434] = 26;
A[435] = 30;
A[412] = 70;
} else {
A[434] = 44;
A[435] = 36;
A[412] = 96;
}
A[410] = (A[413] - 16) / GW;
// 高度也要放得下 9 行 + 底部提示
A[437] = (A[414] - A[412] - 30) / GW;
if (A[437] < A[410]) { A[410] = A[437]; }
if (A[410] < 6) { A[410] = 6; }
A[411] = (A[413] - GW * A[410]) / 2;
if (A[411] < 4) { A[411] = 4; }
A[436] = A[412] + GW * A[410] + 26;
if (A[436] > A[414] - 14) { A[436] = A[414] - 14; }
}
void newGame() {
A[417] = 0;
while (A[417] < 81) {
A[A[417]] = 0;
A[81 + A[417]] = 0;
A[162 + A[417]] = 0;
A[417] = A[417] + 1;
}
A[417] = 0;
while (A[417] < MINES) {
A[430] = (A[430] * 1103 + 12345) % 32768;
A[431] = (A[430] / 256) % GW;
A[430] = (A[430] * 1103 + 12345) % 32768;
A[432] = (A[430] / 256) % GW;
if (A[idx()] == 0) {
A[idx()] = 1;
A[417] = A[417] + 1;
}
}
A[405] = 0;
A[406] = 0;
A[407] = 0;
A[408] = 0;
A[409] = 0;
}
// 连锁展开(显式队列)
void reveal() {
if (A[81 + idx()] != 0) { return; }
if (A[162 + idx()] != 0) { return; }
A[417] = idx();
A[418] = 0;
A[419] = 0;
A[243] = A[417] % GW;
A[324] = A[417] / GW;
A[419] = 1;
while (A[418] < A[419]) {
A[431] = A[243 + A[418]];
A[432] = A[324 + A[418]];
A[418] = A[418] + 1;
if (A[81 + idx()] != 0) { continue; }
if (A[162 + idx()] != 0) { continue; }
A[81 + idx()] = 1;
A[405] = A[405] + 1;
if (A[idx()] == 1) { A[406] = 1; return; }
if (neighbors() != 0) { continue; }
A[422] = -1;
while (A[422] <= 1) {
A[423] = -1;
while (A[423] <= 1) {
if (!(A[422] == 0 && A[423] == 0)) {
A[424] = A[431] + A[422];
A[425] = A[432] + A[423];
if (A[424] >= 0 && A[425] >= 0 && A[424] < GW && A[425] < GW) {
A[433] = A[424] * GW + A[425];
if (A[81 + A[433]] == 0 && A[162 + A[433]] == 0) {
if (A[419] < 81) {
A[243 + A[419]] = A[424];
A[324 + A[419]] = A[425];
A[419] = A[419] + 1;
}
}
}
}
A[423] = A[423] + 1;
}
A[422] = A[422] + 1;
}
}
if (A[405] >= GW * GW - MINES) { A[407] = 1; }
}
void draw() {
ui_2d_clear(C_BG);
ui_2d_rect(8, A[434], A[413] / 3, A[435], A[408] != 0 ? C_FLAG : C_HIDDEN, 1, 0, 8);
if (A[408] != 0) {
if (A[415] == 0) { ui_2d_text(8 + A[413] / 6, A[434] + A[435] / 2, "插旗模式", C_BG, 13, VML_ANCHOR_CENTER); }
else { ui_2d_text(8 + A[413] / 6, A[434] + A[435] / 2, "Flag mode", C_BG, 13, VML_ANCHOR_CENTER); }
} else {
if (A[415] == 0) { ui_2d_text(8 + A[413] / 6, A[434] + A[435] / 2, "翻开模式", C_TEXT, 13, VML_ANCHOR_CENTER); }
else { ui_2d_text(8 + A[413] / 6, A[434] + A[435] / 2, "Reveal mode", C_TEXT, 13, VML_ANCHOR_CENTER); }
}
A[427] = 0;
A[417] = 0;
while (A[417] < 81) {
if (A[162 + A[417]] != 0) { A[427] = A[427] + 1; }
A[417] = A[417] + 1;
}
ui_2d_text(A[413] - 10, A[434] + 8, numstr(MINES - A[427]), C_FLAG, 16, VML_ANCHOR_RIGHT);
if (A[415] == 0) { ui_2d_text(A[413] - 10, A[434] + 28, "剩余雷数", C_DIM, 10, VML_ANCHOR_RIGHT); }
else { ui_2d_text(A[413] - 10, A[434] + 28, "mines left", C_DIM, 10, VML_ANCHOR_RIGHT); }
A[431] = 0;
while (A[431] < GW) {
A[432] = 0;
while (A[432] < GW) {
if (A[81 + idx()] != 0) {
if (A[idx()] == 1) {
ui_2d_rect(A[411] + A[431] * A[410] + 1, A[412] + A[432] * A[410] + 1, A[410] - 2, A[410] - 2, C_MINE, 1, 0, 2);
} else {
ui_2d_rect(A[411] + A[431] * A[410] + 1, A[412] + A[432] * A[410] + 1, A[410] - 2, A[410] - 2, C_OPEN, 1, 0, 2);
A[426] = neighbors();
if (A[426] > 0) {
ui_2d_text(A[411] + A[431] * A[410] + A[410] / 2, A[412] + A[432] * A[410] + A[410] / 2 + 5,
numstr(A[426]), C_TEXT, A[410] / 2, VML_ANCHOR_CENTER);
}
}
} else {
ui_2d_rect(A[411] + A[431] * A[410] + 1, A[412] + A[432] * A[410] + 1, A[410] - 2, A[410] - 2, C_HIDDEN, 1, 0, 3);
if (A[162 + idx()] != 0) {
ui_2d_circle(A[411] + A[431] * A[410] + A[410] / 2, A[412] + A[432] * A[410] + A[410] / 2, A[410] / 4, C_FLAG, 1, 0);
}
}
A[432] = A[432] + 1;
}
A[431] = A[431] + 1;
}
if (A[406] != 0) {
if (A[415] == 0) { ui_2d_text(A[413] / 2, A[436], "踩雷了,ESC 重来", C_MINE, 14, VML_ANCHOR_CENTER); }
else { ui_2d_text(A[413] / 2, A[436], "Boom - ESC to restart", C_MINE, 14, VML_ANCHOR_CENTER); }
} else if (A[407] != 0) {
if (A[415] == 0) { ui_2d_text(A[413] / 2, A[436], "扫清了!ESC 再来", C_TEXT, 14, VML_ANCHOR_CENTER); }
else { ui_2d_text(A[413] / 2, A[436], "Cleared! ESC for a new board", C_TEXT, 14, VML_ANCHOR_CENTER); }
} else {
if (A[415] == 0) { ui_2d_text(A[413] / 2, A[436], "点格子翻开 / 点顶部按钮插旗", C_DIM, 11, VML_ANCHOR_CENTER); }
else { ui_2d_text(A[413] / 2, A[436], "Tap a cell / tap the top button to flag", C_DIM, 11, VML_ANCHOR_CENTER); }
}
ui_2d_present();
}
int main() {
A[415] = ui_scr_get_language();
if (A[415] == 0) { ui_win_open("扫雷", 0, 0); }
else { ui_win_open("Minesweeper", 0, 0); }
ui_scr_set_keep_on(1);
relayout();
A[430] = 20261117;
newGame();
draw();
A[416] = ui_timer_set(80, 0);
while (ui_win_is_closed() == 0) {
A[433] = ui_msg_fetch_wait(0);
if (A[433] == 10) { break; }
if (A[433] == 6 || A[433] == 4) {
A[428] = ui_msg_get_a();
A[429] = ui_msg_get_b();
if (A[428] >= 8 && A[428] <= 8 + A[413] / 3 && A[429] >= A[434] && A[429] <= A[434] + A[435]) {
if (A[408] != 0) { A[408] = 0; }
else { A[408] = 1; }
} else {
A[431] = (A[428] - A[411]) / A[410];
A[432] = (A[429] - A[412]) / A[410];
if (A[431] >= 0 && A[432] >= 0 && A[431] < GW && A[432] < GW) {
if (A[406] == 0 && A[407] == 0) {
if (A[408] != 0) {
if (A[81 + idx()] == 0) {
if (A[162 + idx()] != 0) { A[162 + idx()] = 0; }
else { A[162 + idx()] = 1; }
}
} else {
reveal();
}
}
}
}
}
if (A[433] == 1) {
A[417] = ui_msg_get_a();
if (A[417] == 27 || A[417] == 82) { newGame(); }
}
if (A[433] == 11) { relayout(); }
A[409] = A[409] + 1;
if (A[409] >= MAX_FRAMES) { break; }
draw();
}
ui_timer_kill(A[416]);
ui_scr_set_keep_on(0);
ui_win_close();
return 0;
}