← 都来码 / Dolaima← 例程索引c/app/memory.c▶ 在浏览器里试玩 原文

c/app/memory.c · 6828 字节 · 下载原文

▶ 在浏览器里试玩

/* memory.c —— 记忆翻牌(配对:翻两张一样的就留下)
 * memory.c -- memory matching (flip two cards; an equal pair stays face up)
 *
 * 玩法:4×4 共 8 对;点一张翻开,再点一张;一样就留在明面,不一样 0.4 秒后扣回去;
 * Rules: 4x4, eight pairs; tap to flip, tap a second card; an equal pair stays revealed, a different
 * pair flips back after 0.4 s;
 * 全部配对完成即通关。步数越少越好。
 * clearing all pairs wins. Fewer moves is better.
 *
 * 这里刻意演示的是**"延时动作"怎么写**:翻错的两张不能立刻扣回去(用户看不到第二张是什么),
 * What this sample deliberately shows is **how to write a delayed action**: a wrong pair cannot flip
 * back immediately (the player would never see the second card),
 * 所以记一个倒计时拍数,主循环每拍减一,减到 0 才扣 —— 不另起线程、不睡眠。
 * so it keeps a countdown in ticks and decrements it each frame, flipping back at 0 -- no extra
 * thread and no sleep.
 */

#include <vml_ui.h>
#include <stdlib.h>
#include <stdio.h>

#define MAX_FRAMES 8000
#define N 4
#define PAIRS ((N * N) / 2)
#define HIDE_TICKS 8               /* 8 拍 × 50ms = 0.4 秒 */

#define C_BG     0xFF101018
#define C_BACK   0xFF2A2A36
#define C_TEXT   0xFFE8E8F0
#define C_DIM    0xFF9AA0B0

/* 8 对卡片的颜色(同一个字母 = 同一对) */
static const char *PAIR_TEXT[PAIRS] = { "A", "B", "C", "D", "E", "F", "G", "H" };
static const int PAIR_COLOR[PAIRS] = {
    0xFFE06C50, 0xFFD9B44A, 0xFF51E86E, 0xFF4A90D9,
    0xFF9B6CF0, 0xFFE06CA8, 0xFF4AD9C6, 0xFFC0C040
};

static int SW, SH, g_lang, g_frames;
static int cell, ox, oy;
static int pair_of[N][N];         /* -1 = 已配好 */
static int up[N][N];              /* 翻开中 */
static int first_x, first_y;      /* 第一张(-1 = 没有) */
static int hide_countdown;        /* >0 时到点扣回 */
static int moves, matched, won;

static unsigned int seed = 20261119;
static int rnd(int n)
{
    seed = seed * 1103515245u + 12345u;
    return (int)((seed >> 16) % (unsigned int)n);
}

static void relayout(void)
{
    int side;
    int top;
    int bottom;
    SW = ui_scr_get_w();
    SH = ui_scr_get_h();
    if (SW <= 0) SW = 360;
    if (SH <= 0) SH = 620;
    top = (SH < 420 || SW < 360) ? 64 : 110;
    bottom = (SH < 420 || SW < 360) ? 40 : 60;
    side = SW - 20;
    if (side > SH - top - bottom) side = SH - top - bottom;
    if (side < 60) side = 60;
    cell = side / N;
    if (cell < 6) cell = 6;
    ox = (SW - N * cell) / 2;
    oy = top + (SH - top - bottom - N * cell) / 2;
    if (oy < top) oy = top;
}

static void new_game(void)
{
    int x, y, i;
    /* 先铺成 0,0,1,1,2,2…再洗牌(洗牌用 Fisher–Yates,自己带随机源) */
    for (x = 0; x < N; x++)
        for (y = 0; y < N; y++) {
            pair_of[x][y] = (y * N + x) / 2;
            up[x][y] = 0;
        }
    for (i = N * N - 1; i > 0; i--) {
        int j = rnd(i + 1);
        int ax = i / N, ay = i % N, bx = j / N, by = j % N, t;
        t = pair_of[ax][ay]; pair_of[ax][ay] = pair_of[bx][by]; pair_of[bx][by] = t;
    }
    first_x = -1;
    first_y = -1;
    hide_countdown = 0;
    moves = 0;
    matched = 0;
    won = 0;
}

static void draw(void)
{
    int x, y;
    char buf[8];

    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 p = pair_of[x][y];
            if (p < 0) {
                /* 已配对:淡淡地留个印子 */
                ui_2d_rect(px + 3, py + 3, cell - 6, cell - 6, 0xFF16161E, 1, 0, 6);
                continue;
            }
            if (up[x][y]) {
                ui_2d_rect(px + 3, py + 3, cell - 6, cell - 6, PAIR_COLOR[p], 1, 0, 8);
                ui_2d_text(px + cell / 2, py + cell / 2 + cell / 8, (char *)PAIR_TEXT[p], 0xFF101018,
                           cell / 2, VML_ANCHOR_CENTER);
            } else {
                ui_2d_rect(px + 3, py + 3, cell - 6, cell - 6, C_BACK, 1, 0, 8);
            }
        }
    }

    itoa(moves, buf);
    ui_2d_text(10, 22, buf, C_TEXT, 15, VML_ANCHOR_LEFT);
    ui_2d_text(10, 42, g_lang == 0 ? "步数" : "moves", C_DIM, 10, VML_ANCHOR_LEFT);

    if (won)
        ui_2d_text(SW / 2, oy + N * cell + 28, g_lang == 0 ? "全配好了!ESC 再来" : "All matched! ESC for a new one",
                   C_TEXT, 15, VML_ANCHOR_CENTER);
    else
        ui_2d_text(SW / 2, oy + N * cell + 28, g_lang == 0 ? "翻两张一样的" : "Flip two that match",
                   C_DIM, 12, VML_ANCHOR_CENTER);
    ui_2d_present();
}

static void tap(int tx, int ty)
{
    int x = (tx - ox) / cell, y = (ty - oy) / cell;
    if (x < 0 || y < 0 || x >= N || y >= N) return;
    if (pair_of[x][y] < 0 || up[x][y] || hide_countdown > 0 || won) return;

    up[x][y] = 1;
    if (first_x < 0) { first_x = x; first_y = y; return; }

    moves = moves + 1;
    if (pair_of[first_x][first_y] == pair_of[x][y]) {
        pair_of[first_x][first_y] = -1;
        pair_of[x][y] = -1;
        matched = matched + 2;
        if (matched >= N * N) won = 1;
        first_x = -1;
    } else {
        hide_countdown = HIDE_TICKS;              /* 记个倒计时,主循环到点扣回 */
    }
}

int main(void)
{
    int msg[4];
    int tid;
    int done = 0;
    char buf[8];

    g_lang = ui_scr_get_language();
    relayout();
    ui_win_open_ex("memory.c", 0, 0, VML_WIN_ROTATABLE, VML_WIN_NO_GAMEPAD);
    relayout();
    new_game();
    draw();

    tid = ui_timer_set(50, 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) { tap(msg[1], msg[2]); draw(); continue; }
        if (t == VML_MSG_KEYDOWN) {
            if (msg[1] == VML_KEY_ESCAPE) { new_game(); draw(); }
            continue;
        }
        if (t == VML_MSG_TIMER) {
            g_frames = g_frames + 1;
            if (g_frames >= MAX_FRAMES) done = 1;
            if (hide_countdown > 0) {
                hide_countdown = hide_countdown - 1;
                if (hide_countdown == 0 && first_x >= 0) {
                    /* 扣回这两张 */
                    int x, y;
                    for (x = 0; x < N; x++)
                        for (y = 0; y < N; y++) up[x][y] = 0;
                    first_x = -1;
                }
            }
            draw();
            continue;
        }
        if (t == VML_MSG_WINDOWRESIZE) { relayout(); draw(); continue; }
        if (t == VML_MSG_WINDOWCLOSE) break;
    }

    ui_timer_kill(tid);
    ui_win_close();
    itoa(moves, buf);
    printf("memory: moves=%s matched=%d won=%d done\n", buf, matched, won);
    return 0;
}