objc/app/memory.m · 10612 字节 · 下载原文
// 记忆翻牌 —— 用 **ObjC** 写的手机小游戏
// Memory matching — a small phone game written in **ObjC**
//
// 玩法(与 `Examples/c/app/memory.c` / `Examples/python/app/memory.py` 同一套):
// Gameplay (same rules as the C / Python versions):
// · 4×4 共 8 对;点一张翻开,再点第二张;
// · 4x4, eight pairs; tap one card to flip it, then tap a second;
// · 一样就留在明面,不一样 **0.4 秒**后扣回去;全部配对完成即通关;
// · an equal pair stays revealed, a different pair flips back after **0.4 s**; clear all pairs to win;
// · 通关后点屏幕(或按 ESC / R)重开。
// · after a win, tap the screen (or press ESC / R) for a new game.
//
// ## 这一份演示「延时动作」怎么写
// ## This sample shows **how to write a delayed action**
//
// 翻错的两张不能立刻扣回去(玩家会看不到第二张是什么),所以记一个**倒计时拍数**,
// 主循环每拍减一,减到 0 才扣 —— 不另起线程、不睡眠。
// A wrong pair must not flip back immediately (the player would never see the second card), so we
// keep a countdown in **ticks** and decrement it each frame -- no extra thread, no sleep.
//
// ## 写法约束(见 `Examples/objc/app/snake.m` 文件头)
// ## Style constraints (see the header of `Examples/objc/app/snake.m`)
//
// · 循环更新写 `i = i + 1`,不能写 `i++`;数组**没有初始化列表**,逐元素赋值;
// · loop updates are `i = i + 1`, never `i++`; arrays have **no initializer lists** — assign element by element;
// · 颜色写**负数十进制**(词法器十进制专用,不认 `0x`);
// · colours are **negative decimals** (the lexer is decimal-only and does not accept `0x`);
// · 状态放**一个**文件级数组 `A[]`,辅助函数一律无参。
// · state lives in **one** file-level array `A[]`, and helper functions take no arguments.
//
// 跑法 —— 手机:`vml run examples/objc/memory.m`
// How to run — on the phone: `vml run examples/objc/memory.m`
// 跑法 —— 桌面:`vmlcli Examples/objc/memory.m --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/objc/memory.m --screen 480x640 --frames /tmp/fr`
#include <vml_ui.h>
#import <vml_ui.h>
// 状态表(一个数组放全部):
// 0..15 = pairOf[i](这张牌属于哪一对;-1 = 已配对)
// 16..31 = up[i](这张牌是否正翻开)
// 32=firstX 33=firstY 34=hide 35=moves 36=matched 37=won 38=seed
// 39=LANG 40=SW 41=SH 42=CELL 43=OX 44=OY 45=maxPrims 46=tid
// 47=tapX 48=tapY(tap 的实参走这里,辅助函数无参)
int A[49];
#define MAX_FRAMES 8000
#define N 4
#define CELLS 16 // N * N
#define HIDE_TICKS 8 // 8 拍 × 50ms ≈ 0.4 秒
#define C_BG -15724520 // 0xFF101018
#define C_BACK -14013898 // 0xFF2A2A36
#define C_MATCHED -15329762 // 0xFF16161E
#define C_TEXT -1513232 // 0xFFE8E8F0
#define C_DIM -6643536 // 0xFF9AA0B0
#define C_FACE -15724520 // 0xFF101018
#define P0 -2069424 // 0xFFE06C50
#define P1 -2509750 // 0xFFD9B44A
#define P2 -11409298 // 0xFF51E86E
#define P3 -11890471 // 0xFF4A90D9
#define P4 -6591248 // 0xFF9B6CF0
#define P5 -2069336 // 0xFFE06CA8
#define P6 -11871802 // 0xFF4AD9C6
#define P7 -4145088 // 0xFFC0C040
// 整数 → 字符串(不引 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 run)
int rnd(int n) {
A[38] = (A[38] * 1103 + 12345) % 32768;
return (A[38] / 256) % n;
}
// 一对牌的颜色 —— 8 种固定色,按配对号取
// The colour of a pair — eight fixed colours indexed by pair number
int pairColor(int p) {
if (p == 0) { return P0; }
if (p == 1) { return P1; }
if (p == 2) { return P2; }
if (p == 3) { return P3; }
if (p == 4) { return P4; }
if (p == 5) { return P5; }
if (p == 6) { return P6; }
return P7;
}
void relayout() {
A[40] = ui_scr_get_w();
A[41] = ui_scr_get_h();
if (A[40] <= 0) { A[40] = 360; }
if (A[41] <= 0) { A[41] = 620; }
A[42] = A[40] - 20;
if (A[40] >= A[41] - 140) { A[42] = A[41] - 170; }
A[42] = A[42] / N;
if (A[42] < 6) { A[42] = 6; }
A[43] = (A[40] - N * A[42]) / 2;
A[44] = 110;
}
// 开一局:先铺成 0,0,1,1,2,2…再 Fisher–Yates 洗牌(自带随机源,可复现)
// New game: lay out 0,0,1,1,2,2... then Fisher-Yates shuffle (own RNG, reproducible)
void newGame() {
int x;
int y;
int i;
int j;
int t;
x = 0;
while (x < N) {
y = 0;
while (y < N) {
A[x * N + y] = (y * N + x) / 2;
A[16 + x * N + y] = 0;
y = y + 1;
}
x = x + 1;
}
i = CELLS - 1;
while (i > 0) {
j = rnd(i + 1);
t = A[i];
A[i] = A[j];
A[j] = t;
i = i - 1;
}
A[32] = -1;
A[33] = -1;
A[34] = 0;
A[35] = 0;
A[36] = 0;
A[37] = 0;
}
void draw() {
int x;
int y;
int px;
int py;
int p;
ui_2d_clear(C_BG);
x = 0;
while (x < N) {
y = 0;
while (y < N) {
px = A[43] + x * A[42];
py = A[44] + y * A[42];
p = A[x * N + y];
if (p < 0) {
// 已配对:淡淡地留个印子
// Matched: leave a faint imprint
ui_2d_rect(px + 3, py + 3, A[42] - 6, A[42] - 6, C_MATCHED, 1, 0, 6);
} else if (A[16 + x * N + y] == 1) {
ui_2d_rect(px + 3, py + 3, A[42] - 6, A[42] - 6, pairColor(p), 1, 0, 8);
// 牌面用数字(与 C/Python 版一致:随包字体缺花色字形会成豆腐块)
// The face is a number (same as the C/Python versions: glyphs like suits may be missing)
ui_2d_text(px + A[42] / 2, py + A[42] / 2 + A[42] / 8,
numstr(p + 1), C_FACE, A[42] / 2, VML_ANCHOR_CENTER);
} else {
ui_2d_rect(px + 3, py + 3, A[42] - 6, A[42] - 6, C_BACK, 1, 0, 8);
}
y = y + 1;
}
x = x + 1;
}
ui_2d_text(10, 22, numstr(A[35]), C_TEXT, 15, VML_ANCHOR_LEFT);
if (A[39] == 0) { ui_2d_text(10, 42, "步数", C_DIM, 10, VML_ANCHOR_LEFT); }
else { ui_2d_text(10, 42, "moves", C_DIM, 10, VML_ANCHOR_LEFT); }
if (A[37] == 1) {
if (A[39] == 0) { ui_2d_text(A[40] / 2, A[44] + N * A[42] + 28, "全配好了!点一下再来", C_TEXT, 15, VML_ANCHOR_CENTER); }
else { ui_2d_text(A[40] / 2, A[44] + N * A[42] + 28, "All matched! Tap for a new one", C_TEXT, 15, VML_ANCHOR_CENTER); }
} else {
if (A[39] == 0) { ui_2d_text(A[40] / 2, A[44] + N * A[42] + 28, "翻两张一样的", C_DIM, 12, VML_ANCHOR_CENTER); }
else { ui_2d_text(A[40] / 2, A[44] + N * A[42] + 28, "Flip two that match", C_DIM, 12, VML_ANCHOR_CENTER); }
}
ui_2d_present();
}
// tap 的实参走 A[47]/A[48](辅助函数无参)
// tap arguments come in through A[47]/A[48] (helpers take no arguments)
void tap() {
int x;
int y;
int i;
int fi;
if (A[37] == 1) {
newGame();
return;
}
x = (A[47] - A[43]) / A[42];
y = (A[48] - A[44]) / A[42];
if (x < 0 || y < 0 || x >= N || y >= N) { return; }
i = x * N + y;
if (A[i] < 0 || A[16 + i] == 1 || A[34] > 0) { return; }
A[16 + i] = 1;
if (A[32] < 0) {
A[32] = x;
A[33] = y;
return;
}
A[35] = A[35] + 1;
fi = A[32] * N + A[33];
if (A[fi] == A[i]) {
A[fi] = -1;
A[i] = -1;
A[36] = A[36] + 2;
if (A[36] >= CELLS) { A[37] = 1; }
A[32] = -1;
} else {
// 记个倒计时,主循环到点扣回
// Countdown; the main loop flips them back at 0
A[34] = HIDE_TICKS;
}
}
int main() {
int t;
int k;
int frames;
int done;
int x;
int y;
A[39] = ui_scr_get_language();
relayout();
if (A[39] == 0) { ui_win_open("记忆翻牌", 0, 0); }
else { ui_win_open("Memory", 0, 0); }
relayout();
A[38] = 20265; // 与其它语言版同一个种子 / same seed as the other language versions
newGame();
draw();
A[46] = ui_timer_set(50, 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[47] = ui_msg_get_a();
A[48] = ui_msg_get_b();
tap();
draw();
continue;
}
if (t == 1) { // MSG_KEYDOWN
k = ui_msg_get_a();
if (k == 27 || k == 82) { newGame(); 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; }
if (A[34] > 0) {
A[34] = A[34] - 1;
if (A[34] == 0) {
// 扣回所有翻开的牌
// Flip every face-up card back
x = 0;
while (x < N) {
y = 0;
while (y < N) {
A[16 + x * N + y] = 0;
y = y + 1;
}
x = x + 1;
}
A[32] = -1;
}
}
draw();
continue;
}
}
ui_timer_kill(A[46]);
ui_win_close();
return 0;
}