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

c/app/minesweeper.c · 7469 字节 · 下载原文

▶ 在浏览器里试玩

/* minesweeper.c —— 扫雷(网格推理:翻开格子、标雷、别踩雷)
 * minesweeper.c -- Minesweeper (grid deduction: reveal cells, flag mines, do not step on one)
 *
 * 玩法:点格子翻开;数字是周围 8 格的雷数;翻到 0 会**连锁展开**;
 * Rules: tap a cell to reveal it; the number counts mines in the surrounding 8 cells; revealing a 0
 * **floods** outward;
 * 顶部按钮切到「插旗」模式再点就是标雷;把所有非雷格子翻开即通关,踩到雷就结束。
 * the top button switches to "flag" mode and taps then mark mines; revealing every non-mine cell wins,
 * stepping on a mine ends it.
 *
 * 画法与手感都刻意保持朴素 —— 这一份要讲清的是**网格 + 状态数组 + 连锁展开**,
 * The drawing and feel are deliberately plain -- what this sample is meant to show is
 * **a grid + state arrays + flood fill**,
 * 而不是美术。
 * not artwork.
 */

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

#define MAX_FRAMES 4000
#define GW 9
#define GH 9
#define MINES 10
#define QUEUE_MAX (GW * GH)

#define C_BG     0xFF101018
#define C_HIDDEN 0xFF2A2A36
#define C_OPEN   0xFF1A1A24
#define C_FLAG   0xFFE06C50
#define C_TEXT   0xFFE8E8F0
#define C_DIM    0xFF9AA0B0
#define C_MINE   0xFFE06C50

static int SW, SH, g_lang, g_frames;
static int cell, ox, oy;
static int mine[GW][GH], open_[GW][GH], flag[GW][GH];
static int opened, dead, won, flag_mode;
static int queue_x[QUEUE_MAX], queue_y[QUEUE_MAX];

static unsigned int seed = 20261117;
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;
    side = SW < SH - 120 ? SW - 16 : SH - 130;
    cell = side / GW;
    if (cell < 6) cell = 6;
    ox = (SW - GW * cell) / 2;
    oy = 96;
}

static int neighbors(int x, int y)
{
    int dx, dy, n = 0;
    for (dy = -1; dy <= 1; dy++)
        for (dx = -1; dx <= 1; dx++) {
            int nx = x + dx, ny = y + dy;
            if (dx == 0 && dy == 0) continue;
            if (nx < 0 || ny < 0 || nx >= GW || ny >= GH) continue;
            if (mine[nx][ny]) n++;
        }
    return n;
}

static void new_game(void)
{
    int i, x, y;
    for (x = 0; x < GW; x++)
        for (y = 0; y < GH; y++) { mine[x][y] = 0; open_[x][y] = 0; flag[x][y] = 0; }
    for (i = 0; i < MINES; i++) {
        do { x = rnd(GW); y = rnd(GH); } while (mine[x][y]);
        mine[x][y] = 1;
    }
    opened = 0;
    dead = 0;
    won = 0;
    flag_mode = 0;
}

/* 连锁展开(用显式队列,不用递归):翻开一个 0,把周围都排队 */
static void reveal(int x, int y)
{
    int head = 0, tail = 0;
    if (open_[x][y] || flag[x][y]) return;

    queue_x[tail] = x; queue_y[tail] = y; tail++;
    while (head < tail) {
        int cx = queue_x[head], cy = queue_y[head], dx, dy;
        head++;
        if (open_[cx][cy] || flag[cx][cy]) continue;
        open_[cx][cy] = 1;
        opened = opened + 1;
        if (mine[cx][cy]) { dead = 1; return; }
        if (neighbors(cx, cy) != 0) continue;          /* 有数字就不展开 */
        for (dy = -1; dy <= 1; dy++)
            for (dx = -1; dx <= 1; dx++) {
                int nx = cx + dx, ny = cy + dy;
                if ((dx == 0 && dy == 0) || nx < 0 || ny < 0 || nx >= GW || ny >= GH) continue;
                if (open_[nx][ny] || flag[nx][ny]) continue;
                if (tail < QUEUE_MAX) { queue_x[tail] = nx; queue_y[tail] = ny; tail++; }
            }
    }
    if (opened >= GW * GH - MINES) won = 1;
}

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

    ui_2d_clear(C_BG);

    /* 顶栏:模式按钮 + 剩余雷数 */
    ui_2d_rect(8, 44, SW / 3, 36, flag_mode ? C_FLAG : C_HIDDEN, 1, 0, 8);
    ui_2d_text(8 + SW / 6, 62, flag_mode ? (g_lang == 0 ? "插旗模式" : "Flag mode")
                                        : (g_lang == 0 ? "翻开模式" : "Reveal mode"),
               flag_mode ? 0xFF101018 : C_TEXT, 13, VML_ANCHOR_CENTER);

    i = 0;
    for (x = 0; x < GW; x++)
        for (y = 0; y < GH; y++)
            if (flag[x][y]) i++;
    itoa(MINES - i, buf);
    ui_2d_text(SW - 10, 52, buf, C_FLAG, 16, VML_ANCHOR_RIGHT);
    ui_2d_text(SW - 10, 72, g_lang == 0 ? "剩余雷数" : "mines left", C_DIM, 10, VML_ANCHOR_RIGHT);

    for (x = 0; x < GW; x++) {
        for (y = 0; y < GH; y++) {
            int px = ox + x * cell, py = oy + y * cell;
            if (open_[x][y]) {
                int n = neighbors(x, y);
                ui_2d_rect(px + 1, py + 1, cell - 2, cell - 2, mine[x][y] ? C_MINE : C_OPEN, 1, 0, 2);
                if (!mine[x][y] && n > 0) {
                    itoa(n, buf);
                    ui_2d_text(px + cell / 2, py + cell / 2 + 5, buf, C_TEXT, cell / 2, VML_ANCHOR_CENTER);
                }
            } else {
                ui_2d_rect(px + 1, py + 1, cell - 2, cell - 2, C_HIDDEN, 1, 0, 3);
                if (flag[x][y])
                    ui_2d_circle(px + cell / 2, py + cell / 2, cell / 4, C_FLAG, 1, 0);
            }
        }
    }

    if (dead)
        ui_2d_text(SW / 2, oy + GH * cell + 26, g_lang == 0 ? "踩雷了,ESC 重来" : "Boom - ESC to restart",
                   C_MINE, 14, VML_ANCHOR_CENTER);
    else if (won)
        ui_2d_text(SW / 2, oy + GH * cell + 26, g_lang == 0 ? "扫清了!ESC 再来" : "Cleared! ESC for a new board",
                   C_TEXT, 14, VML_ANCHOR_CENTER);
    else
        ui_2d_text(SW / 2, oy + GH * cell + 26, g_lang == 0 ? "点格子翻开 / 点顶部按钮插旗"
                                                           : "Tap a cell / tap the top button to flag",
                   C_DIM, 11, VML_ANCHOR_CENTER);
    ui_2d_present();
}

static void tap(int tx, int ty)
{
    int x, y;
    if (tx >= 8 && tx <= 8 + SW / 3 && ty >= 44 && ty <= 80) {
        flag_mode = flag_mode ? 0 : 1;             /* 点顶栏切模式 */
        return;
    }
    x = (tx - ox) / cell;
    y = (ty - oy) / cell;
    if (x < 0 || y < 0 || x >= GW || y >= GH) return;
    if (dead || won) return;
    if (flag_mode) { if (!open_[x][y]) flag[x][y] = flag[x][y] ? 0 : 1; return; }
    reveal(x, y);
}

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("minesweeper.c", 0, 0, VML_WIN_ROTATABLE, VML_WIN_NO_GAMEPAD);
    relayout();
    new_game();
    draw();

    tid = ui_timer_set(80, 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;
            draw();
            continue;
        }
        if (t == VML_MSG_WINDOWRESIZE) { relayout(); draw(); continue; }
        if (t == VML_MSG_WINDOWCLOSE) break;
    }

    ui_timer_kill(tid);
    ui_win_close();
    itoa(opened, buf);
    printf("minesweeper: opened=%s dead=%d won=%d done\n", buf, dead, won);
    return 0;
}