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

c/app/reversi.c · 10851 字节 · 下载原文

▶ 在浏览器里试玩

/* reversi.c —— 黑白棋(Reversi / Othello),跑在任意 VML 宿主上
 * reversi.c -- Reversi / Othello, runs on any VML host
 *
 *     vml run examples/c/app/reversi.c
 *
 * ## 跨平台口径
 * ## Cross-platform notes
 *
 * 只用稳定 `ui_*`;布局每帧按画布尺寸现算并处理窗口尺寸变化;触摸/鼠标都收,
 * 同坐标的重复投递去重(E411);棋盘坐标全部是逻辑格,不依赖像素密度。
 *
 * ## 玩法
 * ## How to play
 *
 * 你执黑先行:点一个小圆点提示的合法格落子,夹住的白子翻成黑子。
 * 双方都无子可下时结束,子多者胜。点「新局」重开。
 */

#include <vml_ui.h>

#define N 8

int W, H;
int g_lang;

int board[N * N];        /* 0 空 / 1 黑(你) / 2 白(电脑) */
                         /* 0 empty / 1 black (you) / 2 white (computer) */
int turn;                /* 1 黑 / 2 白 */
int over;
int lastMove;            /* 上一手位置(-1 无) */
int blackCount, whiteCount;
int tick;
int compact = 0;

int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

char g_buf[32];

char* fmt_int(int v, char* buf)
{
    int n = 0, i = 0;
    if (v == 0) { buf[0] = '0'; buf[1] = 0; return buf; }
    {
        char t[12];
        while (v > 0 && i < 11) { t[i] = (char)('0' + v % 10); i = i + 1; v = v / 10; }
        while (i > 0) { i = i - 1; buf[n] = t[i]; n = n + 1; }
    }
    buf[n] = 0;
    return buf;
}

int rand_n(int n) { return n > 0 ? ui_math_rand(n) : 0; }
int idx(int x, int y) { return y * N + x; }

int opp(int p) { return p == 1 ? 2 : 1; }

/* 在 (x,y) 落 who 能翻几个;不能落返回 0 */
/* How many discs `who` would flip at (x,y); 0 = illegal */
int flips_at(int x, int y, int who)
{
    int d, total = 0;
    if (x < 0 || y < 0 || x >= N || y >= N) return 0;
    if (board[idx(x, y)] != 0) return 0;
    for (d = 0; d < 8; d++) {
        int cx = x + dx[d], cy = y + dy[d], n = 0;
        while (cx >= 0 && cy >= 0 && cx < N && cy < N && board[idx(cx, cy)] == opp(who)) {
            n = n + 1;
            cx = cx + dx[d]; cy = cy + dy[d];
        }
        if (n > 0 && cx >= 0 && cy >= 0 && cx < N && cy < N && board[idx(cx, cy)] == who)
            total = total + n;
    }
    return total;
}

void do_move(int x, int y, int who)
{
    int d;
    board[idx(x, y)] = who;
    for (d = 0; d < 8; d++) {
        int cx = x + dx[d], cy = y + dy[d], n = 0;
        while (cx >= 0 && cy >= 0 && cx < N && cy < N && board[idx(cx, cy)] == opp(who)) {
            n = n + 1;
            cx = cx + dx[d]; cy = cy + dy[d];
        }
        if (n > 0 && cx >= 0 && cy >= 0 && cx < N && cy < N && board[idx(cx, cy)] == who) {
            cx = x + dx[d]; cy = y + dy[d];
            while (board[idx(cx, cy)] == opp(who)) {
                board[idx(cx, cy)] = who;
                cx = cx + dx[d]; cy = cy + dy[d];
            }
        }
    }
    lastMove = idx(x, y);
}

int has_moves(int who)
{
    int i;
    for (i = 0; i < N * N; i++)
        if (flips_at(i % N, i / N, who) > 0) return 1;
    return 0;
}

void count(void)
{
    int i;
    blackCount = 0; whiteCount = 0;
    for (i = 0; i < N * N; i++) {
        if (board[i] == 1) blackCount = blackCount + 1;
        else if (board[i] == 2) whiteCount = whiteCount + 1;
    }
}

void new_game(void)
{
    int i;
    for (i = 0; i < N * N; i++) board[i] = 0;
    board[idx(3, 3)] = 2; board[idx(4, 4)] = 2;
    board[idx(3, 4)] = 1; board[idx(4, 3)] = 1;
    turn = 1;
    over = 0;
    lastMove = -1;
    count();
}

/* 电脑:翻子最多 + 角加权;同分随机 */
/* Computer: most flips + corner weight; random tie-break */
void ai_move(void)
{
    int i, best = -1, bestScore = -1, cnt = 0;
    for (i = 0; i < N * N; i++) {
        int x = i % N, y = i / N;
        int f = flips_at(x, y, 2);
        int s, corner;
        if (f <= 0) continue;
        corner = (x == 0 || x == N - 1) && (y == 0 || y == N - 1);
        s = f + (corner ? 12 : 0);
        if (s > bestScore) { bestScore = s; best = i; cnt = 1; }
        else if (s == bestScore) {
            cnt = cnt + 1;
            if (rand_n(cnt) == 0) best = i;
        }
    }
    if (best >= 0) do_move(best % N, best / N, 2);
}

/* 轮到谁就检查:没子可下就跳过;两边都没子结束 */
/* Advance the turn, skipping a player with no legal move */
void advance_turn(void)
{
    count();
    if (!has_moves(1) && !has_moves(2)) { over = 1; return; }
    if (turn == 1) {
        turn = 2;
        if (!has_moves(2)) {
            turn = 1;
            if (!has_moves(1)) over = 1;
        }
    } else {
        turn = 1;
        if (!has_moves(1)) {
            turn = 2;
            if (!has_moves(2)) over = 1;
        }
    }
}

/* ── 布局 ── */
/* -- Layout -- */

int side(void)
{
    int s = W - (compact ? 16 : 24);
    int avail = H - (compact ? 58 : 108) - (compact ? 36 : 96);
    if (!compact && avail < 160) avail = 160;
    if (compact && avail < 96) avail = 96;
    if (s > avail) s = avail;
    if (s < 96) s = 96;
    return s;
}
int bx(void) { return (W - side()) / 2; }
int by(void)
{
    int top = compact ? 58 : 108;
    int y = top + (H - top - (compact ? 36 : 96) - side()) / 2;
    if (y < top) y = top;
    return y;
}

/* ── 绘制 ── */
/* -- Drawing -- */

void draw(void)
{
    int i, x, y, s, cell, ox, oy, i2, cx, cy, r;
    char* st;

    ui_2d_clear(0xFF0F1B14);

    s = side();
    cell = s / N;
    s = cell * N;
    ox = bx();
    oy = by();

    /* 棋盘 */
    ui_2d_rect(ox - 3, oy - 3, s + 6, s + 6, 0xFF3A5A46, 1, 0, 6);
    for (y = 0; y < N; y++) {
        for (x = 0; x < N; x++) {
            int cx0 = ox + x * cell, cy0 = oy + y * cell;
            ui_2d_rect(cx0 + 1, cy0 + 1, cell - 2, cell - 2, 0xFF2E7D4F, 1, 0, 2);
        }
    }

    /* 合法落点提示(只在轮到你时) */
    if (turn == 1 && !over) {
        for (i = 0; i < N * N; i++) {
            if (flips_at(i % N, i / N, 1) > 0) {
                int px = ox + (i % N) * cell + cell / 2;
                int py = oy + (i / N) * cell + cell / 2;
                ui_2d_circle(px, py, cell / 8 + 1, 0xCCB9C4D6, 1, 0);
            }
        }
    }

    /* 棋子 */
    for (i = 0; i < N * N; i++) {
        if (board[i] == 0) continue;
        cx = ox + (i % N) * cell + cell / 2;
        cy = oy + (i / N) * cell + cell / 2;
        r = cell / 2 - 3;
        ui_2d_circle(cx, cy, r, board[i] == 1 ? 0xFF20242B : 0xFFF2F4F7, 1, 0);
        if (i == lastMove)
            ui_2d_circle(cx, cy, r + 3, 0xCCE6EBF5, 0, 3);
    }

    /* 记分 */
    ui_2d_text_v(8, compact ? 20 : 30, g_lang == 0 ? "黑白棋" : "Reversi", 0xFFE6EBF5, compact ? 15 : 22,
                 VML_ANCHOR_LEFT, VML_VANCHOR_MIDDLE, VML_FONT_BOLD);
    fmt_int(blackCount, g_buf);
    ui_2d_text_v(W - (compact ? 84 : 110), compact ? 20 : 30, g_buf, 0xFFE6EBF5, compact ? 14 : 20,
                 VML_ANCHOR_RIGHT, VML_VANCHOR_MIDDLE, VML_FONT_BOLD);
    ui_2d_circle(W - (compact ? 72 : 96), compact ? 20 : 30, compact ? 6 : 8, 0xFF20242B, 1, 0);
    fmt_int(whiteCount, g_buf);
    ui_2d_text_v(W - (compact ? 34 : 50), compact ? 20 : 30, g_buf, 0xFFE6EBF5, compact ? 14 : 20,
                 VML_ANCHOR_RIGHT, VML_VANCHOR_MIDDLE, VML_FONT_BOLD);
    ui_2d_circle(W - (compact ? 22 : 36), compact ? 20 : 30, compact ? 6 : 8, 0xFFF2F4F7, 1, 0);

    if (over) {
        if (blackCount > whiteCount) st = g_lang == 0 ? "你赢了!" : "You win!";
        else if (blackCount < whiteCount) st = g_lang == 0 ? "电脑赢了" : "Computer wins";
        else st = g_lang == 0 ? "平局" : "Draw";
        ui_2d_text_v(W / 2, compact ? 44 : 66, st, 0xFF4ADE80, compact ? 11 : 18,
                     VML_ANCHOR_CENTER, VML_VANCHOR_MIDDLE, VML_FONT_BOLD);
    } else {
        st = turn == 1 ? (g_lang == 0 ? "轮到你了(黑)" : "Your turn (black)")
                       : (g_lang == 0 ? "电脑思考中…" : "Computer thinking...");
        ui_2d_text_v(W / 2, compact ? 44 : 66, st, 0xFF9AB0A0, compact ? 10 : 14,
                     VML_ANCHOR_CENTER, VML_VANCHOR_MIDDLE, 0);
    }

    /* 新局 */
    {
        int bw = W / (compact ? 2 : 3), bh = compact ? 24 : 48, bx2, by2;
        if (bw > 220) bw = 220;
        bx2 = (W - bw) / 2;
        by2 = H - bh - (compact ? 6 : 28);
        ui_2d_rect(bx2, by2, bw, bh, 0xFF232938, 1, 0, 10);
        ui_2d_text_v(W / 2, by2 + bh / 2, g_lang == 0 ? "新局" : "New game", 0xFFE6EBF5, compact ? 11 : 18,
                     VML_ANCHOR_CENTER, VML_VANCHOR_MIDDLE, VML_FONT_BOLD);
    }

    ui_2d_present();
}

/* ── 交互 ── */
/* -- Input -- */

void tap(int x, int yscore)
{
    int s, cell, ox, oy, bw, bh, bx2, by2;
    int gx, gy;

    s = side();
    cell = s / N;
    s = cell * N;
    ox = bx();
    oy = by();

    bw = W / (compact ? 2 : 3); bh = compact ? 24 : 48;
    if (bw > 220) bw = 220;
    bx2 = (W - bw) / 2;
    by2 = H - bh - (compact ? 6 : 28);
    if (x >= bx2 && x < bx2 + bw && yscore >= by2 && yscore < by2 + bh) {
        new_game();
        ui_audio_beep(660, 60);
        return;
    }

    if (over || turn != 1) return;
    if (x < ox || x >= ox + s || yscore < oy || yscore >= oy + s) return;
    gx = (x - ox) / cell;
    gy = (yscore - oy) / cell;
    if (flips_at(gx, gy, 1) <= 0) return;

    do_move(gx, gy, 1);
    ui_audio_beep(880, 45);
    advance_turn();
    if (!over && turn == 2) {
        ai_move();
        ui_audio_beep(520, 45);
        advance_turn();
    }
    count();
}

int main(void)
{
    int m[4];
    int t;
    int lastX = -1, lastY = -1, sawTouch = 0;

    g_lang = ui_scr_get_language();
    ui_win_open_ex(g_lang == 0 ? "黑白棋" : "Reversi", 0, 0,
                   VML_WIN_ROTATABLE, VML_WIN_NO_GAMEPAD);
    W = ui_scr_get_w();
    H = ui_scr_get_h();
    compact = (H < 420 || W < 360) ? 1 : 0;
    ui_scr_set_keep_on(1);

    new_game();
    draw();

    while (ui_win_is_closed() == 0) {
        t = ui_msg_wait(m, 100);
        if (t == VML_MSG_WINDOWCLOSE) break;
        if (t == VML_MSG_WINDOWRESIZE) {
            W = ui_scr_get_w();
            H = ui_scr_get_h();
            compact = (H < 420 || W < 360) ? 1 : 0;
            draw();
            continue;
        }
        if (t == VML_MSG_TOUCHDOWN) {
            lastX = m[1]; lastY = m[2]; sawTouch = 1;
            tap(m[1], m[2]);
            draw();
            continue;
        }
        if (t == VML_MSG_MOUSEDOWN) {
            if (sawTouch && m[1] == lastX && m[2] == lastY) { sawTouch = 0; continue; }
            tap(m[1], m[2]);
            draw();
            continue;
        }
        if (t == VML_MSG_KEYDOWN) {
            if (m[1] == 78 || m[1] == 82) { new_game(); draw(); }
            continue;
        }
        tick = tick + 1;
    }

    ui_scr_set_keep_on(0);
    return 0;
}