java/app/g2048.java · 13037 字节 · 下载原文
// 2048 —— 用 **Java** 写的手机小游戏(合并 / 数字)
// 2048 — a small phone game written in **Java** (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 of the slide direction.
//
// ## Java 前端四条实测约束(见 `Examples/java/app/memory.java` 文件头)
// ## Four measured conventions of this Java front end (see `Examples/java/app/memory.java`)
//
// ① `import vml.ui.*;` 带来 `ui_*` 函数但**不带常量** ⇒ 常量就地声明;
// ① `import vml.ui.*;` brings `ui_*` functions but **no constants** ⇒ declare them here;
// ② 状态一律放 `static int[]`(堆上,读写可靠);
// ② state lives in `static int[]` (heap-backed, reliable);
// ③ 屏幕坐标 → 格子必须**先判负再除**(Java 除法向零截断);
// ③ screen coordinates → cell must **test for negatives before dividing** (Java truncates toward zero);
// ④ **数组维度与下标一律用字面量**,不要用 `static final` 常量(实测:常量会被写坏,
// 数组少分配一格)—— 本文件棋盘 16 格、4×4 全部写死。
// ④ **Array dimensions and subscripts are always literals**, never a `static final` constant
// (measured: the constant gets clobbered and the array ends up one slot short) — the board
// here is a hard-coded 16-slot array over a 4x4 grid.
//
// 跑法 —— 手机:`vml run examples/java/g2048.java`
// How to run — on the phone: `vml run examples/java/g2048.java`
// 跑法 —— 桌面:`vmlcli Examples/java/g2048.java --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/java/g2048.java --screen 480x640 --frames /tmp/fr`
import vml.ui.*;
public class G2048 {
static final int MSG_KEYDOWN = 1;
static final int MSG_MOUSEDOWN = 4;
static final int MSG_TOUCHDOWN = 6;
static final int MSG_TIMER = 9;
static final int MSG_CLOSE = 10;
static final int MSG_RESIZE = 11;
static final int KEY_ESC = 27;
static final int KEY_R = 82;
static final int ANCHOR_LEFT = 0;
static final int ANCHOR_CENTER = 1;
static final int C_BG = 0xFF101018;
static final int C_EMPTY = 0xFF1A1A24;
static final int C_TEXT = 0xFFE8E8F0;
static final int C_DIM = 0xFF9AA0B0;
static final int MAX_FRAMES = 20000;
// 标量状态槽(见文件头 ②):0=score 1=won 2=lost 3=seed 4=cell 5=ox 6=oy
// 7=sw 8=sh 9=lang 10=frames 11=moved
static int[] S = new int[24];
// 棋盘 16 格(4×4 平铺,下标字面量见文件头 ④)
static int[] board = new int[16];
// 滑动用的行/列缓冲 + 原值快照(16 格足够)
// Per-line working buffer + a snapshot of the old values
static int[] line = new int[8];
static int[] old = new int[8];
static String gTr(String zh, String en) {
if (S[9] == 0) return zh;
return en;
}
static int gRnd(int n) {
S[3] = (S[3] * 1103 + 12345) % 32768;
return (S[3] / 256) % n;
}
static void gLayout() {
S[7] = ui_scr_get_w();
S[8] = ui_scr_get_h();
if (S[7] <= 0) S[7] = 360;
if (S[8] <= 0) S[8] = 620;
// 240×240 这种矮屏把页眉/页脚压紧,棋盘才能从 68px 长到 132px 左右。
// On a short 240x240 screen tighten the header/footer so the board grows from ~68px to ~132px.
int top = 120;
int foot = 50;
if (S[8] < 480) { top = 78; foot = 30; }
int side = S[7] - 20;
if (S[8] - top - foot < side) side = S[8] - top - foot;
S[4] = side / 4;
if (S[4] < 6) S[4] = 6;
S[5] = (S[7] - 4 * S[4]) / 2;
S[6] = top;
}
static void gSpawn() {
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 = gRnd(n);
i = 0;
while (i < 16) {
if (board[i] == 0) {
if (pick == 0) {
if (gRnd(10) == 0) board[i] = 4;
else board[i] = 2;
i = 16;
} else {
pick = pick - 1;
}
}
if (i < 16) i = i + 1;
}
}
static void gNewGame() {
int i = 0;
while (i < 16) {
board[i] = 0;
i = i + 1;
}
S[0] = 0;
S[1] = 0;
S[2] = 0;
S[10] = 0;
gSpawn();
gSpawn();
}
// 取第 d 个方向上的第 k 行/列(0..3)
// Fetch row/column k (0..3) along direction d
static void gReadLine(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 gWriteLine(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 gSlide(int d) {
int changed = 0;
int k = 0;
while (k < 4) {
gReadLine(d, k);
int i = 0;
while (i < 4) {
old[i] = line[i];
i = i + 1;
}
// 收拢(去掉 0)
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;
}
// 合并(从远端往回收 = 从下标 0 开始两两合并)
int out = 0;
i = 0;
while (i < n) {
if (i + 1 < n && line[i] == line[i + 1]) {
line[out] = line[i] * 2;
S[0] = S[0] + line[i] * 2;
if (line[i] * 2 >= 2048) S[1] = 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;
}
// 比较是否变化(line vs 快照 old),再写回
// Compare with the snapshot to detect a change, then write back
i = 0;
while (i < 4) {
if (line[i] != old[i]) changed = 1;
i = i + 1;
}
gWriteLine(d, k);
k = k + 1;
}
return changed;
}
// 是否还有可走的步(有空格或有相邻相等)
// Whether any move is left (an empty cell, or an adjacent equal pair)
static int gCanMove() {
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 gTileColor(int v) {
if (v == 0) return C_EMPTY;
if (v == 2) return 0xFF2A2A36;
if (v == 4) return 0xFF3A3A48;
if (v == 8) return 0xFF4A90D9;
if (v == 16) return 0xFF51E86E;
if (v == 32) return 0xFFD9B44A;
if (v == 64) return 0xFFE06C50;
if (v == 128) return 0xFFE06CA8;
if (v == 256) return 0xFF9B6CF0;
if (v == 512) return 0xFF4AD9C6;
if (v == 1024) return 0xFF6C7BF0;
return 0xFFC0C040;
}
static int gNumSize(int v) {
if (v >= 1000) return S[4] / 4;
if (v >= 100) return S[4] / 3;
return S[4] / 2;
}
static void gDraw() {
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 = S[5] + x * S[4];
int py = S[6] + y * S[4];
ui_2d_rect(px + 2, py + 2, S[4] - 4, S[4] - 4, gTileColor(v), 1, 0, 6);
if (v != 0) {
ui_2d_text(px + S[4] / 2, py + S[4] / 2 + gNumSize(v) / 3,
"" + v, C_TEXT, gNumSize(v), ANCHOR_CENTER);
}
y = y + 1;
}
x = x + 1;
}
ui_2d_text(10, 24, "" + S[0], C_TEXT, 18, ANCHOR_LEFT);
ui_2d_text(10, 48, gTr("得分", "score"), C_DIM, 10, ANCHOR_LEFT);
if (S[1] != 0) {
ui_2d_text(S[7] / 2, 24, gTr("2048 达成!还能继续", "2048! keep going"), 0xFFD9B44A, 14, ANCHOR_CENTER);
}
// 复用循环结束后的 py 当提示行 y,并把它钳进画布(240×240 时页脚只剩几像素)。
// Reuse py after the loops as the hint line's y, clamped into the canvas (a 240x240 footer is only a few pixels).
py = S[6] + 4 * S[4] + 28;
if (py > S[8] - 10) py = S[8] - 10;
if (S[2] != 0) {
ui_2d_text(S[7] / 2, py, gTr("没路可走了,ESC / R 重开", "No moves left - ESC / R to restart"),
C_TEXT, 14, ANCHOR_CENTER);
} else if (S[1] == 0) {
ui_2d_text(S[7] / 2, py, gTr("方向键 / 点屏幕四边滑动", "Arrows / tap a screen edge to slide"),
C_DIM, 11, ANCHOR_CENTER);
}
}
static void gMove(int d) {
if (S[2] != 0) return;
int changed = gSlide(d);
if (changed != 0) {
gSpawn();
if (gCanMove() == 0) S[2] = 1;
}
}
public static void main(String[] args) {
S[9] = ui_scr_get_language();
ui_win_open(gTr("2048", "2048"), 0, 0);
ui_scr_set_keep_on(1);
gLayout();
S[3] = 20261;
gNewGame();
gDraw();
int tid = ui_timer_set(50, 1);
while (ui_win_is_closed() == 0) {
int t = ui_msg_fetch_wait(50);
if (t == MSG_CLOSE) break;
if (t == MSG_KEYDOWN) {
int k = ui_msg_get_a();
if (k == KEY_ESC || k == KEY_R) gNewGame();
else if (k == 37) gMove(0);
else if (k == 39) gMove(1);
else if (k == 38) gMove(2);
else if (k == 40) gMove(3);
}
if (t == MSG_TOUCHDOWN || t == MSG_MOUSEDOWN) {
int tx = ui_msg_get_a();
int ty = ui_msg_get_b();
int dl = tx;
int dr = S[7] - tx;
int dt = ty;
int db = S[8] - ty;
if (dl <= dr && dl <= dt && dl <= db) gMove(0);
else if (dr <= dt && dr <= db) gMove(1);
else if (dt <= db) gMove(2);
else gMove(3);
}
if (t == MSG_RESIZE) gLayout();
if (t == MSG_TIMER) {
S[10] = S[10] + 1;
if (S[10] >= MAX_FRAMES) break;
}
gDraw();
ui_2d_present();
}
ui_timer_kill(tid);
ui_scr_set_keep_on(0);
ui_win_close();
}
}