c/app/tictactoe.c · 9712 字节 · 下载原文
/* tictactoe.c —— 井字棋(人机对战),跑在任意 VML 宿主上
* tictactoe.c -- Tic-tac-toe (human vs computer), runs on any VML host
*
* 编译运行:
* vml run examples/c/app/tictactoe.c
*
* ## 为什么这份要特别照顾「跨平台」
* ## Why this one is written with cross-platform in mind
*
* 用户会把它在手机上编译成 `.vmb`,再拿到桌面/平板等其他宿主上跑。所以:
* - **只用稳定的 `ui_*` 接口**(窗体 / 绘图 / 消息队列 / 音效),不碰任何平台专有物;
* - **布局每帧按画布尺寸现算**(`ui_scr_get_w/h`),竖屏、横屏、桌面窗口都成立,
* 并处理 `VML_MSG_WINDOWRESIZE`;
* - **触摸与鼠标二选一,重复投递去重**(E411):MAUI 一次点击会同时来
* `TOUCHDOWN` 与 `MOUSEDOWN`,同坐标的两条都当点击处理就会一次落两子;
* - 不依赖外部资源,不读文件,不调用平台专有 syscall。
*
* ## 界面
* ## UI
*
* 上方状态行;中间 3×3 棋盘(点格子落子);下方「新局」按钮。
* A status line on top, a 3x3 board (tap a cell to play), and a New-game button below.
*/
#include <vml_ui.h>
#define N 3
/* ── 配色(0xAARRGGBB)── */
/* -- Colors (0xAARRGGBB) -- */
#define C_BG 0xFF11141C
#define C_CELL 0xFF1B2130
#define C_CELL2 0xFF202737
#define C_LINE 0xFF5A6A86
#define C_X 0xFF5EC8F0
#define C_O 0xFFF3A64B
#define C_TEXT 0xFFE6EBF5
#define C_DIM 0xFF8B94A7
#define C_WIN 0xFF4ADE80
#define C_BTN 0xFF232938
#define C_BTN_HOT 0xFF31405C
int W, H;
int g_lang;
int board[9]; /* 0 空 / 1 人(X) / 2 电脑(O) */
/* 0 empty / 1 human (X) / 2 computer (O) */
int turn; /* 0 人 / 1 电脑 */
/* 0 human / 1 computer */
int over; /* 0 进行中 / 1 人赢 / 2 电脑赢 / 3 平局 */
/* 0 playing / 1 human won / 2 computer won / 3 draw */
int winLine[3]; /* 获胜三格(-1 = 无) */
int tick;
char g_buf[32];
char* fmt_int(int v, char* buf)
{
int n = 0, i = 0;
if (v < 0) { buf[n] = '-'; n = n + 1; v = -v; }
if (v == 0) { buf[n] = '0'; n = n + 1; }
else {
char t[12];
i = 0;
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; }
/* ── 规则 ── */
/* -- Rules -- */
int lines[8][3] = {
{0,1,2}, {3,4,5}, {6,7,8},
{0,3,6}, {1,4,7}, {2,5,8},
{0,4,8}, {2,4,6}
};
int winner(void)
{
int i;
for (i = 0; i < 8; i++) {
int a = lines[i][0], b = lines[i][1], c = lines[i][2];
if (board[a] != 0 && board[a] == board[b] && board[b] == board[c]) {
winLine[0] = a; winLine[1] = b; winLine[2] = c;
return board[a];
}
}
for (i = 0; i < 9; i++) if (board[i] == 0) return 0;
return 3; /* 平局 / draw */
}
/* 找「下一手能连成三」的格子;没有返回 -1 */
/* Returns a cell that completes a line for `who`, or -1 */
int find_win(int who)
{
int i, k;
for (i = 0; i < 8; i++) {
int cnt = 0, empty = -1;
for (k = 0; k < 3; k++) {
int c = lines[i][k];
if (board[c] == who) cnt = cnt + 1;
else if (board[c] == 0) empty = c;
}
if (cnt == 2 && empty >= 0) return empty;
}
return -1;
}
void new_game(void)
{
int i;
for (i = 0; i < 9; i++) board[i] = 0;
turn = 0;
over = 0;
winLine[0] = -1; winLine[1] = -1; winLine[2] = -1;
}
/* 电脑落子:先赢,再堵,再中心/角/边 */
/* Computer move: win, then block, then center/corner/side */
void ai_move(void)
{
int c, i;
int order[9] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
c = find_win(2);
if (c < 0) c = find_win(1);
if (c < 0) {
for (i = 0; i < 9; i++) {
if (board[order[i]] == 0) { c = order[i]; break; }
}
}
if (c >= 0) board[c] = 2;
turn = 0;
over = winner();
}
/* ── 布局(每帧现算,竖屏/横屏/桌面窗口都成立)── */
/* -- Layout (recomputed every frame; works portrait, landscape and desktop windows) -- */
int ui_small(void) { return (H < 420 || W < 360); }
int head_h(void) { return ui_small() ? 56 : 96; }
int foot_h(void) { return ui_small() ? 56 : 96; }
int board_side(void)
{
int side = W - 24;
int avail = H - head_h() - foot_h();
if (avail < 60) avail = 60;
if (side > avail) side = avail;
if (side < 60) side = 60;
return side;
}
int board_x(void) { return (W - board_side()) / 2; }
int board_y(void)
{
int y = head_h() + (H - head_h() - foot_h() - board_side()) / 2;
if (y < head_h()) y = head_h();
return y;
}
int btn_h(void) { return ui_small() ? 26 : 48; }
int btn_y(void) { return H - btn_h() - (ui_small() ? 10 : 28); }
/* ── 绘制 ── */
/* -- Drawing -- */
void draw_button(int x, int y, int w, int h, char* label, int hot)
{
ui_2d_rect(x, y, w, h, hot ? C_BTN_HOT : C_BTN, 1, 0, 10);
ui_2d_text_v(x + w / 2, y + h / 2, label, C_TEXT, h < 34 ? 12 : 20,
VML_ANCHOR_CENTER, VML_VANCHOR_MIDDLE, VML_FONT_BOLD);
}
char* status_text(void)
{
if (over == 1) return g_lang == 0 ? "你赢了!" : "You win!";
if (over == 2) return g_lang == 0 ? "电脑赢了" : "Computer wins";
if (over == 3) return g_lang == 0 ? "平局" : "Draw";
return turn == 0 ? (g_lang == 0 ? "轮到你了(X)" : "Your turn (X)")
: (g_lang == 0 ? "电脑思考中…" : "Computer thinking...");
}
void draw(void)
{
int i, side, bx, by, cell, c, r;
int bw, bh, byy;
ui_2d_clear(C_BG);
/* 状态行 / status line */
ui_2d_text_v(W / 2, ui_small() ? 16 : 32, g_lang == 0 ? "井字棋" : "Tic-tac-toe", C_TEXT,
ui_small() ? 16 : 24,
VML_ANCHOR_CENTER, VML_VANCHOR_MIDDLE, VML_FONT_BOLD);
ui_2d_text_v(W / 2, ui_small() ? 40 : 64, status_text(),
over == 1 ? C_WIN : C_DIM, ui_small() ? 11 : 16,
VML_ANCHOR_CENTER, VML_VANCHOR_MIDDLE, 0);
side = board_side();
cell = side / N;
side = cell * N;
bx = board_x();
by = board_y();
/* 棋盘底 + 格子 */
ui_2d_rect(bx - 2, by - 2, side + 4, side + 4, C_LINE, 1, 0, 8);
for (r = 0; r < N; r++) {
for (c = 0; c < N; c++) {
i = r * N + c;
ui_2d_rect(bx + c * cell + 2, by + r * cell + 2,
cell - 4, cell - 4,
((r + c) % 2 == 0) ? C_CELL : C_CELL2, 1, 0, 4);
if (board[i] != 0) {
int col = board[i] == 1 ? C_X : C_O;
if (over > 0 && over < 3 &&
(winLine[0] == i || winLine[1] == i || winLine[2] == i))
col = C_WIN;
ui_2d_text_v(bx + c * cell + cell / 2, by + r * cell + cell / 2,
board[i] == 1 ? "X" : "O", col,
cell * 2 / 3, VML_ANCHOR_CENTER, VML_VANCHOR_MIDDLE,
VML_FONT_BOLD);
}
}
}
/* 新局按钮 */
bw = W / 3;
if (bw > 220) bw = 220;
bh = btn_h();
byy = btn_y();
draw_button((W - bw) / 2, byy, bw, bh, g_lang == 0 ? "新局" : "New game", 0);
ui_2d_present();
}
/* ── 交互 ── */
/* -- Input -- */
void tap(int x, int y)
{
int side, cell, bx, by, c, r, i;
int bw, bh, byy;
side = board_side();
cell = side / N;
side = cell * N;
bx = board_x();
by = board_y();
/* 新局按钮 */
bw = W / 3;
if (bw > 220) bw = 220;
bh = btn_h();
byy = btn_y();
if (x >= (W - bw) / 2 && x < (W - bw) / 2 + bw && y >= byy && y < byy + bh) {
new_game();
ui_audio_beep(660, 60);
return;
}
if (over != 0 || turn != 0) return;
if (x < bx || x >= bx + side || y < by || y >= by + side) return;
c = (x - bx) / cell;
r = (y - by) / cell;
i = r * N + c;
if (board[i] != 0) return;
board[i] = 1;
ui_audio_beep(880, 50);
over = winner();
if (over == 0) ai_move();
if (over == 1) { ui_audio_beep(1047, 180); }
else if (over == 2) { ui_audio_beep(220, 220); }
else if (over == 3) { ui_audio_beep(523, 120); }
}
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 ? "井字棋" : "Tic-tac-toe", 0, 0,
VML_WIN_ROTATABLE, VML_WIN_NO_GAMEPAD);
W = ui_scr_get_w();
H = ui_scr_get_h();
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();
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(); } /* N / R */
continue;
}
tick = tick + 1;
}
ui_scr_set_keep_on(0);
return 0;
}