cpp/app/g2048.cpp · 10748 字节 · 下载原文
// 2048 —— 用 **C++** 写的手机小游戏(合并 / 数字)
// 2048 — a small phone game written in **C++** (merge / numbers)
//
// 玩法(与 `Examples/c/app/game2048.c` / `Examples/python/app/game2048.py` 同一套):
// Gameplay (same rules as the C / Python versions):
// · 4×4 棋盘,方向键 / 点屏幕四边往一个方向滑;
// · a 4x4 board; arrow keys / tapping one of the four screen edges slide everything that way;
// · 相同数字相撞合成两倍、得分加上合成值,每滑一次冒一个新数字(10% 是 4);
// · equal numbers merge and score the merged value; each slide spawns a new tile (4 with 10% odds);
// · 到 2048 算赢(还能继续),没空格且四邻无相等算输;ESC / R 重开。
// · reaching 2048 wins (you may keep playing); no empty cell and no adjacent equal pair loses; ESC / R restarts.
//
// ## 合并的**先后**容易写错
// ## The **merge order** is easy to get wrong
//
// 一次滑动里每个块只许合并一次(`4 4 4 4` 滑一次是 `8 8`,不是 `16`),所以从滑动方向的**最远端**往回收。
// Each tile may merge only once per slide (`4 4 4 4` becomes `8 8`, not `16`), so collapse from the far end.
//
// ## C++ 这一路的写法约束(见 `Examples/cpp/app/snake.cpp` 文件头)
// ## Style constraints on this C++ path (see the header of `Examples/cpp/app/snake.cpp`)
//
// · 颜色写**负数十进制**;整数转字符串走 `numstr()`;数组维度只认字面量宏(→E391);
// · colours are **negative decimals**; integers to strings go through `numstr()`; array dimensions
// use literal macros only (→E391);
// · 循环更新写 `i = i + 1`。
// · loop updates are written `i = i + 1`.
//
// 跑法 —— 手机:`vml run examples/cpp/g2048.cpp`
// How to run — on the phone: `vml run examples/cpp/g2048.cpp`
// 跑法 —— 桌面:`vmlcli Examples/cpp/g2048.cpp --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/cpp/g2048.cpp --screen 480x640 --frames /tmp/fr`
#include <vml_ui.h>
#define GT_MAX_FRAMES 20000
#define C_BG -15724520 // 0xFF101018
#define C_EMPTY -15066588 // 0xFF1A1A24
#define C_TEXT -1513232 // 0xFFE8E8F0
#define C_DIM -6643536 // 0xFF9AA0B0
static int LANG = 0;
static int SW = 360;
static int SH = 620;
static int CELL = 80;
static int OX = 20;
static int OY = 120;
static int score = 0;
static int won = 0;
static int lost = 0;
static int seed = 20261;
static int frames = 0;
// 棋盘 16 格(4×4 平铺)+ 行缓冲(维度只写字面量,见文件头)
// Board: 16 slots (4x4 flat) + a line buffer (literal dimensions only; see the header)
static int board[16];
static int line[8];
static int old[8];
static char g_num[12];
static 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;
}
static int gtRnd(int n)
{
seed = (seed * 1103 + 12345) % 32768;
return (seed / 256) % n;
}
static void relayout()
{
SW = ui_scr_get_w();
SH = ui_scr_get_h();
if (SW <= 0) { SW = 360; }
if (SH <= 0) { SH = 620; }
int side = SW - 20;
if (SW >= SH - 140) { side = SH - 170; }
CELL = side / 4;
if (CELL < 6) { CELL = 6; }
OX = (SW - 4 * CELL) / 2;
OY = 120;
}
static void spawn()
{
int n = 0;
int i = 0;
while (i < 16) {
if (board[i] == 0) { n = n + 1; }
i = i + 1;
}
if (n == 0) { return; }
int pick = gtRnd(n);
i = 0;
while (i < 16) {
if (board[i] == 0) {
if (pick == 0) {
if (gtRnd(10) == 0) { board[i] = 4; }
else { board[i] = 2; }
i = 16;
} else {
pick = pick - 1;
}
}
if (i < 16) { i = i + 1; }
}
}
static void newGame()
{
int i = 0;
while (i < 16) {
board[i] = 0;
i = i + 1;
}
score = 0;
won = 0;
lost = 0;
frames = 0;
spawn();
spawn();
}
static void readLine(int d, int k)
{
int i = 0;
while (i < 4) {
if (d == 0) { line[i] = board[k * 4 + i]; }
else if (d == 1) { line[i] = board[k * 4 + (3 - i)]; }
else if (d == 2) { line[i] = board[i * 4 + k]; }
else { line[i] = board[(3 - i) * 4 + k]; }
i = i + 1;
}
}
static void writeLine(int d, int k)
{
int i = 0;
while (i < 4) {
if (d == 0) { board[k * 4 + i] = line[i]; }
else if (d == 1) { board[k * 4 + (3 - i)] = line[i]; }
else if (d == 2) { board[i * 4 + k] = line[i]; }
else { board[(3 - i) * 4 + k] = line[i]; }
i = i + 1;
}
}
// 滑一次:从远端收拢 + 每个块只合并一次;有变化返回 1
// Slide once: collapse from the far end and merge each tile at most once; returns 1 on change
static int slide(int d)
{
int changed = 0;
int k = 0;
while (k < 4) {
readLine(d, k);
int i = 0;
while (i < 4) {
old[i] = line[i];
i = i + 1;
}
int n = 0;
i = 0;
while (i < 4) {
if (line[i] != 0) {
line[n] = line[i];
n = n + 1;
}
i = i + 1;
}
i = n;
while (i < 4) {
line[i] = 0;
i = i + 1;
}
int out = 0;
i = 0;
while (i < n) {
if (i + 1 < n && line[i] == line[i + 1]) {
line[out] = line[i] * 2;
score = score + line[i] * 2;
if (line[i] * 2 >= 2048) { won = 1; }
i = i + 2;
} else {
line[out] = line[i];
i = i + 1;
}
out = out + 1;
}
i = out;
while (i < 4) {
line[i] = 0;
i = i + 1;
}
i = 0;
while (i < 4) {
if (line[i] != old[i]) { changed = 1; }
i = i + 1;
}
writeLine(d, k);
k = k + 1;
}
return changed;
}
static int canMove()
{
int i = 0;
while (i < 16) {
if (board[i] == 0) { return 1; }
i = i + 1;
}
int x = 0;
while (x < 4) {
int y = 0;
while (y < 4) {
if (x + 1 < 4 && board[x * 4 + y] == board[(x + 1) * 4 + y]) { return 1; }
if (y + 1 < 4 && board[x * 4 + y] == board[x * 4 + y + 1]) { return 1; }
y = y + 1;
}
x = x + 1;
}
return 0;
}
static int tileColor(int v)
{
if (v == 0) { return C_EMPTY; }
if (v == 2) { return -14013898; } // 0xFF2A2A36
if (v == 4) { return -12960184; } // 0xFF3A3A48
if (v == 8) { return -11890471; } // 0xFF4A90D9
if (v == 16) { return -11409298; } // 0xFF51E86E
if (v == 32) { return -2509750; } // 0xFFD9B44A
if (v == 64) { return -2069424; } // 0xFFE06C50
if (v == 128) { return -2069336; } // 0xFFE06CA8
if (v == 256) { return -6591248; } // 0xFF9B6CF0
if (v == 512) { return -11871802; } // 0xFF4AD9C6
if (v == 1024) { return -9688080; } // 0xFF6C7BF0
return -4145088; // 0xFFC0C040
}
static int numSize(int v)
{
if (v >= 1000) { return CELL / 4; }
if (v >= 100) { return CELL / 3; }
return CELL / 2;
}
static void draw()
{
ui_2d_clear(C_BG);
int x = 0;
while (x < 4) {
int y = 0;
while (y < 4) {
int v = board[x * 4 + y];
int px = OX + x * CELL;
int py = OY + y * CELL;
ui_2d_rect(px + 2, py + 2, CELL - 4, CELL - 4, tileColor(v), 1, 0, 6);
if (v != 0) {
ui_2d_text(px + CELL / 2, py + CELL / 2 + numSize(v) / 3,
numstr(v), C_TEXT, numSize(v), 1);
}
y = y + 1;
}
x = x + 1;
}
ui_2d_text(10, 24, numstr(score), C_TEXT, 18, 0);
if (LANG == 0) { ui_2d_text(10, 48, "得分", C_DIM, 10, 0); }
else { ui_2d_text(10, 48, "score", C_DIM, 10, 0); }
if (won != 0) {
if (LANG == 0) { ui_2d_text(SW / 2, 24, "2048 达成!还能继续", -2509750, 14, 1); }
else { ui_2d_text(SW / 2, 24, "2048! keep going", -2509750, 14, 1); }
}
if (lost != 0) {
if (LANG == 0) { ui_2d_text(SW / 2, OY + 4 * CELL + 28, "没路可走了,ESC / R 重开", C_TEXT, 14, 1); }
else { ui_2d_text(SW / 2, OY + 4 * CELL + 28, "No moves left - ESC / R to restart", C_TEXT, 14, 1); }
} else if (won == 0) {
if (LANG == 0) { ui_2d_text(SW / 2, OY + 4 * CELL + 28, "方向键 / 点屏幕四边滑动", C_DIM, 11, 1); }
else { ui_2d_text(SW / 2, OY + 4 * CELL + 28, "Arrows / tap a screen edge to slide", C_DIM, 11, 1); }
}
ui_2d_present();
}
static void move(int d)
{
if (lost != 0) { return; }
if (slide(d) != 0) {
spawn();
if (canMove() == 0) { lost = 1; }
}
}
int main()
{
LANG = ui_scr_get_language();
if (LANG == 0) { ui_win_open("2048", 0, 0); }
else { ui_win_open("2048", 0, 0); }
ui_scr_set_keep_on(1);
relayout();
newGame();
draw();
int tid = ui_timer_set(50, 0);
int done = 0;
while (done == 0 && ui_win_is_closed() == 0) {
int t = ui_msg_fetch_wait(0);
if (t == 10) { break; }
if (t == 1) {
int k = ui_msg_get_a();
if (k == 27 || k == 82) { newGame(); }
else if (k == 37) { move(0); }
else if (k == 39) { move(1); }
else if (k == 38) { move(2); }
else if (k == 40) { move(3); }
draw();
continue;
}
if (t == 6 || t == 4) {
int tx = ui_msg_get_a();
int ty = ui_msg_get_b();
int dl = tx;
int dr = SW - tx;
int dt = ty;
int db = SH - ty;
if (dl <= dr && dl <= dt && dl <= db) { move(0); }
else if (dr <= dt && dr <= db) { move(1); }
else if (dt <= db) { move(2); }
else { move(3); }
draw();
continue;
}
if (t == 11) { relayout(); draw(); continue; }
if (t == 9) {
frames = frames + 1;
if (frames >= GT_MAX_FRAMES) { done = 1; }
draw();
continue;
}
}
ui_timer_kill(tid);
ui_scr_set_keep_on(0);
ui_win_close();
return 0;
}