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

d/app/memory.d · 8087 字节 · 下载原文

▶ 在浏览器里试玩

// 翻牌配对 —— 用 **D** 写的手机小游戏(记忆)
// Memory -- a small phone game written in **D** (matching pairs)
//
// 玩法:4×4 共 16 张牌、8 对颜色;同色就留着,不同色过一会儿翻回去;
// 全配对即通关,步数计分。ESC 重洗。
//
// ⚠ D 前端多个数组会**共享存储**(互相覆盖)⇒ 这里把「牌面 + 翻开 + 已配对」
//   编进**一个**数组:0 = 未翻,1..8 = 翻开的对号,101..108 = 已配对。
//
// 跑法 —— 手机:vml run examples/d/memory.d
// 跑法 —— 桌面:vmlcli Examples/d/memory.d --screen 480x640 --frames /tmp/fr

#include <vml_ui.d>

char[] digit(int d) {
    if (d == 0) { return "0"; }
    if (d == 1) { return "1"; }
    if (d == 2) { return "2"; }
    if (d == 3) { return "3"; }
    if (d == 4) { return "4"; }
    if (d == 5) { return "5"; }
    if (d == 6) { return "6"; }
    if (d == 7) { return "7"; }
    if (d == 8) { return "8"; }
    return "9";
}

void drawNum(int x, int y, int v, int col, int sz) {
    int p1000 = v / 1000;
    int p100 = (v / 100) % 10;
    int p10 = (v / 10) % 10;
    int p1 = v % 10;
    int cx = x;
    if (p1000 > 0) {
        ui_2d_text(cx, y, digit(p1000), col, sz, 0);
        cx = cx + sz / 2;
        ui_2d_text(cx, y, digit(p100), col, sz, 0);
        cx = cx + sz / 2;
    } else if (p100 > 0) {
        ui_2d_text(cx, y, digit(p100), col, sz, 0);
        cx = cx + sz / 2;
    }
    if (p1000 > 0 || p100 > 0 || p10 > 0) {
        ui_2d_text(cx, y, digit(p10), col, sz, 0);
        cx = cx + sz / 2;
    }
    ui_2d_text(cx, y, digit(p1), col, sz, 0);
}

void main() {
    int k = ui_win_open("翻牌配对", 0, 0);
    k = ui_scr_set_keep_on(1);
    int w = ui_scr_get_w();
    int h = ui_scr_get_h();
    if (w <= 0) { w = 360; }
    if (h <= 0) { h = 620; }

    int small = w;
    if (h < small) { small = h; }

    // 版面按画布现算:顶部两栏计数 + 中间 4×4 牌面 + 底部提示。
    int stat_fs = small / 16;
    if (stat_fs < 16) { stat_fs = 16; }
    if (stat_fs > 40) { stat_fs = 40; }
    int label_fs = small / 30;
    if (label_fs < 9) { label_fs = 9; }
    if (label_fs > 16) { label_fs = 16; }
    int hint_fs = small / 26;
    if (hint_fs < 9) { hint_fs = 9; }
    if (hint_fs > 16) { hint_fs = 16; }

    int stat_y = stat_fs + 4;
    int label_y = stat_y + label_fs + 4;
    int hint_y = label_y + hint_fs + 8;
    int top = hint_y + 8;
    int pad = w / 40;
    if (pad < 8) { pad = 8; }
    int bottom_y = h - 8;
    int bottom_top = h - (hint_fs + 12);
    int avail_h = bottom_top - top;
    if (avail_h < 24) { avail_h = 24; }
    int cellw = w - 2 * pad;
    if (avail_h < cellw) { cellw = avail_h; }
    cellw = cellw / 4;
    if (cellw < 6) { cellw = 6; }
    int ox = (w - cellw * 4) / 2;
    int oy = top + (avail_h - cellw * 4) / 2;
    if (oy < top) { oy = top; }
    int gap = cellw / 6;
    if (gap < 4) { gap = 4; }
    if (gap > 14) { gap = 14; }
    int second_x = w * 3 / 5;
    int frames = 0;
    int first = -1;
    int second = -1;
    int pend = 0;
    int tick = 0;
    int pairs = 0;
    int moves = 0;
    int won = 0;

    int[16] card;
    int[16] pair;
    int i = 0;
    while (i < 16) {
        pair[i] = i / 2 + 1;
        card[i] = 0;
        i = i + 1;
    }
    int seed = 1234;
    i = 0;
    while (i < 300) {
        seed = seed + 7919;
        if (seed > 32767) { seed = seed - 32768; }
        int a = seed / 2048;
        seed = seed + 7919;
        if (seed > 32767) { seed = seed - 32768; }
        int b = seed / 2048;
        int tt = pair[a];
        pair[a] = pair[b];
        pair[b] = tt;
        i = i + 1;
    }
    int tid = ui_timer_set(120, 0);

    while (frames < 30000) {
        k = ui_2d_clear(-15724520);
        int idx = 0;
        while (idx < 16) {
            int r = idx / 4;
            int c = idx - r * 4;
            int tx = ox + c * cellw + gap / 2;
            int ty = oy + r * cellw + gap / 2;
            if (card[idx] == 0) {
                k = ui_2d_rect(tx, ty, cellw - gap, cellw - gap, -14013902, 1, 0, 6);
            } else {
                int cc = -2073552;
                int pv = card[idx];
                if (pv > 100) { pv = pv - 100; }
                if (pv == 2) { cc = -2516442; }
                if (pv == 3) { cc = -10044566; }
                if (pv == 4) { cc = -11890471; }
                if (pv == 5) { cc = -6587448; }
                if (pv == 6) { cc = -16728155; }
                if (pv == 7) { cc = -2265265; }
                if (pv == 8) { cc = -2961710; }
                k = ui_2d_rect(tx, ty, cellw - gap, cellw - gap, cc, 1, 0, 6);
            }
            idx = idx + 1;
        }
        drawNum(pad, stat_y, moves, -1513232, stat_fs);
        k = ui_2d_text(pad, label_y, "翻牌次数", -6643536, label_fs, 0);
        drawNum(second_x, stat_y, pairs, -10044566, stat_fs);
        k = ui_2d_text(second_x, label_y, "已配对", -6643536, label_fs, 0);
        if (won == 1) {
            k = ui_2d_text(w / 2, hint_y, "全配上了!ESC 再来", -10044566, hint_fs, 1);
        } else {
            k = ui_2d_text(w / 2, hint_y, "点两张一样的颜色", -6643536, hint_fs, 1);
        }
        k = ui_2d_text(w / 2, bottom_y, "ESC 重洗", -6643536, hint_fs, 1);
        k = ui_2d_present();

        int msg = ui_msg_fetch_wait(0);
        if (msg == 10) { frames = 30000; }
        if (msg == 6) {
            int ax = ui_msg_get_a();
            int ay = ui_msg_get_b();
            if (pend == 0 && won == 0 && ax >= ox && ay >= oy) {
                int c2 = (ax - ox) / cellw;
                int r2 = (ay - oy) / cellw;
                if (c2 < 4 && r2 < 4) {
                    int idx2 = r2 * 4 + c2;
                    if (card[idx2] == 0) {
                        card[idx2] = pair[idx2];
                        moves = moves + 1;
                        if (first < 0) {
                            first = idx2;
                        } else {
                            second = idx2;
                            if (pair[idx2] == pair[first]) {
                                card[idx2] = card[idx2] + 100;
                                card[first] = card[first] + 100;
                                pairs = pairs + 1;
                                first = -1;
                                second = -1;
                                if (pairs >= 8) { won = 1; }
                            } else {
                                pend = 1;
                                tick = 0;
                            }
                        }
                    }
                }
            }
        }
        if (msg == 1) {
            int key = ui_msg_get_a();
            if (key == 27) {
                i = 0;
                while (i < 16) {
                    pair[i] = i / 2 + 1;
                    card[i] = 0;
                    i = i + 1;
                }
                seed = 1234 + frames;
                i = 0;
                while (i < 300) {
                    seed = seed + 7919;
                    if (seed > 32767) { seed = seed - 32768; }
                    int a2 = seed / 2048;
                    seed = seed + 7919;
                    if (seed > 32767) { seed = seed - 32768; }
                    int b2 = seed / 2048;
                    int tt2 = pair[a2];
                    pair[a2] = pair[b2];
                    pair[b2] = tt2;
                    i = i + 1;
                }
                first = -1; second = -1; pend = 0; pairs = 0; moves = 0; won = 0;
            }
        }
        if (msg == 9) {
            frames = frames + 1;
            if (pend == 1) {
                tick = tick + 1;
                if (tick >= 6) {
                    pend = 0;
                    tick = 0;
                    card[first] = 0;
                    card[second] = 0;
                    first = -1;
                    second = -1;
                }
            }
        }
    }

    k = ui_timer_kill(tid);
    k = ui_scr_set_keep_on(0);
    k = ui_win_close();
}