c/app/game2048.c · 8864 字节 · 下载原文
/* game2048.c —— 2048(滑动合并:同数字撞在一起合成两倍)
* game2048.c -- 2048 (slide and merge: equal numbers combine into double)
*
* 玩法:4×4 棋盘,方向键(或点屏幕四边)往一个方向滑;
* Rules: a 4x4 board; arrow keys (or tapping one of the four screen edges) slide everything one way;
* 相同数字相撞合成一倍、得分加上合成值;每滑一次冒一个新数字(2 或 4);
* colliding equal numbers merge and score the merged value; each slide spawns a new 2 or 4;
* 出现 2048 就算赢,没有空格且四邻都不能合并算输。
* reaching 2048 wins; no empty cell and no adjacent equal pair loses.
*
* 合并的**先后**容易写错:一次滑动里每个块只许合并一次(`4 4 4 4` 滑一次应是 `8 8`,不是 `16`),
* The order of merging is easy to get wrong: in one slide each tile may merge only once
* (`4 4 4 4` must become `8 8`, not `16`),
* 所以从滑动方向的**最远端**往回收,收的时候看"目标格刚才是空的还是刚合并过"。
* so collapse from the far end and check whether the target cell was empty or just merged.
*/
#include <vml_ui.h>
#include <stdlib.h>
#include <stdio.h>
#define MAX_FRAMES 20000
#define N 4
#define C_BG 0xFF101018
#define C_EMPTY 0xFF1A1A24
#define C_TEXT 0xFFE8E8F0
#define C_DIM 0xFF9AA0B0
static int SW, SH, g_lang, g_frames;
static int cell, ox, oy;
static int board[N][N];
static int score, won, over;
static unsigned int seed = 20261120;
static int rnd(int n)
{
seed = seed * 1103515245u + 12345u;
return (int)((seed >> 16) % (unsigned int)n);
}
static void relayout(void)
{
int side;
SW = ui_scr_get_w();
SH = ui_scr_get_h();
if (SW <= 0) SW = 360;
if (SH <= 0) SH = 620;
if (SH < 420 || SW < 360) {
/* 240 级别:分数压到顶部两行,棋盘吃掉剩下的大半屏 */
/* 240-class: pack the score into two top lines and let the board take the rest */
side = SW - 16;
if (side > SH - 72) side = SH - 72;
if (side < 40) side = 40;
cell = side / N;
if (cell < 6) cell = 6;
ox = (SW - N * cell) / 2;
oy = 48;
} else {
side = SW < SH - 150 ? SW - 20 : SH - 180;
cell = side / N;
if (cell < 6) cell = 6;
ox = (SW - N * cell) / 2;
oy = 120;
}
}
static int empty_count(void)
{
int x, y, n = 0;
for (x = 0; x < N; x++)
for (y = 0; y < N; y++)
if (board[x][y] == 0) n++;
return n;
}
static void spawn(void)
{
int x, y, guard = 0;
if (empty_count() == 0) return;
do { x = rnd(N); y = rnd(N); guard++; } while (board[x][y] != 0 && guard < 500);
board[x][y] = (rnd(10) == 0) ? 4 : 2; /* 10% 出 4 */
}
static void new_game(void)
{
int x, y;
for (x = 0; x < N; x++)
for (y = 0; y < N; y++) board[x][y] = 0;
score = 0;
won = 0;
over = 0;
spawn();
spawn();
}
/* 往一个方向滑:dir 0=左 1=右 2=上 3=下。返回是否真的动了 */
static int slide(int dir)
{
int x, y, i, moved = 0;
for (i = 0; i < N; i++) {
/* 把这一行/列按滑动方向**从远端往回收** */
int line[N];
int n = 0;
for (x = 0; x < N; x++) {
if (dir == 0) { if (board[x][i] ) line[n++] = board[x][i]; }
else if (dir == 1) { int xr = N - 1 - x; if (board[xr][i]) line[n++] = board[xr][i]; }
else if (dir == 2) { if (board[i][x]) line[n++] = board[i][x]; }
else { int yr = N - 1 - x; if (board[i][yr]) line[n++] = board[i][yr]; }
}
/* 合并:相邻相等的合成一个(每块只合并一次) */
{
int out[N];
int on = 0, k = 0;
while (k < n) {
if (k + 1 < n && line[k] == line[k + 1]) {
out[on++] = line[k] * 2;
score = score + line[k] * 2;
if (line[k] * 2 >= 2048) won = 1;
k = k + 2;
} else {
out[on++] = line[k];
k = k + 1;
}
}
while (on < N) out[on++] = 0;
/* 写回 */
for (x = 0; x < N; x++) {
int old;
if (dir == 0) { old = board[x][i]; board[x][i] = out[x]; }
else if (dir == 1) { old = board[N - 1 - x][i]; board[N - 1 - x][i] = out[x]; }
else if (dir == 2) { old = board[i][x]; board[i][x] = out[x]; }
else { old = board[i][N - 1 - x]; board[i][N - 1 - x] = out[x]; }
if (old != out[x]) moved = 1;
}
}
}
/* 没动就没得玩(也不该冒新数字) */
if (moved) spawn();
/* 输了:没空格,且四邻无相等 */
if (empty_count() == 0) {
int any = 0;
for (x = 0; x < N; x++)
for (y = 0; y < N; y++) {
if (x + 1 < N && board[x][y] == board[x + 1][y]) any = 1;
if (y + 1 < N && board[x][y] == board[x][y + 1]) any = 1;
}
if (!any) over = 1;
}
return moved;
}
static int tile_color(int v)
{
if (v == 0) return C_EMPTY;
if (v == 2) return 0xFF3A3A48;
if (v == 4) return 0xFF4A5A6A;
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;
return 0xFF9B6CF0;
}
static void draw(void)
{
int x, y;
char buf[12];
ui_2d_clear(C_BG);
for (x = 0; x < N; x++) {
for (y = 0; y < N; y++) {
int px = ox + x * cell, py = oy + y * cell;
int v = board[x][y];
ui_2d_rect(px + 3, py + 3, cell - 6, cell - 6, tile_color(v), 1, 0, 10);
if (v == 0) continue;
itoa(v, buf);
ui_2d_text(px + cell / 2, py + cell / 2 + cell / 8, buf,
v <= 4 ? C_TEXT : 0xFF101018, cell / (v >= 100 ? 4 : 3), VML_ANCHOR_CENTER);
}
}
itoa(score, buf);
if (SH < 420 || SW < 360) {
ui_2d_text(10, 12, buf, C_TEXT, 13, VML_ANCHOR_LEFT);
ui_2d_text(10, 28, g_lang == 0 ? "分数" : "score", C_DIM, 9, VML_ANCHOR_LEFT);
} else {
ui_2d_text(10, 22, buf, C_TEXT, 16, VML_ANCHOR_LEFT);
ui_2d_text(10, 44, g_lang == 0 ? "分数" : "score", C_DIM, 10, VML_ANCHOR_LEFT);
}
{
int hy = (SH < 420 || SW < 360) ? SH - 14 : oy + N * cell + 28;
int hfs = (SH < 420 || SW < 360) ? 9 : 14;
if (over)
ui_2d_text(SW / 2, hy, g_lang == 0 ? "滑不动了,ESC 重开" : "No moves left - ESC to restart",
C_TEXT, hfs, VML_ANCHOR_CENTER);
else if (won)
ui_2d_text(SW / 2, hy, g_lang == 0 ? "到 2048!继续或 ESC 重开" : "2048! Keep going or ESC",
C_TEXT, hfs, VML_ANCHOR_CENTER);
else
ui_2d_text(SW / 2, hy, g_lang == 0 ? "方向键 / 点屏幕四边" : "Arrows / tap a screen edge",
C_DIM, (SH < 420 || SW < 360) ? 8 : 12, VML_ANCHOR_CENTER);
}
ui_2d_present();
}
int main(void)
{
int msg[4];
int tid;
int done = 0;
char buf[12];
g_lang = ui_scr_get_language();
relayout();
ui_win_open_ex("game2048.c", 0, 0, VML_WIN_ROTATABLE, VML_WIN_NO_GAMEPAD);
relayout();
new_game();
draw();
tid = ui_timer_set(120, 1);
while (!done && ui_win_is_closed() == 0) {
int t = ui_msg_wait(msg, 0);
if (t == VML_MSG_NONE) continue;
if (t == VML_MSG_TOUCHDOWN || t == VML_MSG_MOUSEDOWN) {
/* 点四边决定方向 */
int x = msg[1], y = msg[2];
int dl = x, dr = SW - x, dt = y, db = SH - y;
if (dl <= dr && dl <= dt && dl <= db) slide(0);
else if (dr <= dt && dr <= db) slide(1);
else if (dt <= db) slide(2);
else slide(3);
draw();
continue;
}
if (t == VML_MSG_KEYDOWN) {
if (msg[1] == VML_KEY_ESCAPE) { new_game(); draw(); continue; }
if (msg[1] == VML_KEY_LEFT) slide(0);
if (msg[1] == VML_KEY_RIGHT) slide(1);
if (msg[1] == VML_KEY_UP) slide(2);
if (msg[1] == VML_KEY_DOWN) slide(3);
draw();
continue;
}
if (t == VML_MSG_TIMER) {
g_frames = g_frames + 1;
if (g_frames >= MAX_FRAMES) done = 1;
continue;
}
if (t == VML_MSG_WINDOWRESIZE) { relayout(); draw(); continue; }
if (t == VML_MSG_WINDOWCLOSE) break;
}
ui_timer_kill(tid);
ui_win_close();
itoa(score, buf);
printf("game2048: score=%s won=%d over=%d done\n", buf, won, over);
return 0;
}