(* 扫雷 —— 用 **Pascal** 写的手机小游戏(网格推理) Minesweeper -- a small phone game written in **Pascal** 玩法:9×9 共 10 颗雷;点格子翻开,数字是周围 8 格的雷数;翻到 0 会 **连锁展开**;顶部按钮切到「插旗」模式再点就是标雷;把所有非雷格子 翻开即通关,踩到雷就结束;ESC 重开。 ⚠ 本前端 `and` / `or` **不短路**(两边都会求值)⇒ 边界判断必须写成嵌套 if,不能写 `if (nx >= 0) and (grid[nx] = 1)` —— 那样会先越界读。 跑法 —— 手机:vml run examples/pascal/minesweeper.pas 跑法 —— 桌面:vmlcli Examples/pascal/minesweeper.pas --screen 480x640 --frames /tmp/fr *) program Minesweeper; uses vml_ui; var mine, opn, flag, nbr: array[0..80] of integer; qx, qy: array[0..80] of integer; w, h, cell, ox, oy, i, x, y, idx, nb: integer; topH, btnX, btnY, btnW, btnH, statusY, fontBtn, fontStatus, fontNum: integer; head, tail, cx, cy, nx, ny, dx, dy, px, py: integer; opened, dead, won, flagm, seed, frames, msg, ax, ay, key, tid: integer; s: string; function CountAj(cxx, cyy: integer): integer; var ddx, ddy, vx, vy, n: integer; begin n := 0; ddx := -1; while ddx <= 1 do begin ddy := -1; while ddy <= 1 do begin if (ddx <> 0) or (ddy <> 0) then begin vx := cxx + ddx; vy := cyy + ddy; if vx >= 0 then begin if vy >= 0 then begin if vx < 9 then begin if vy < 9 then begin if mine[vx * 9 + vy] = 1 then n := n + 1; end; end; end; end; end; ddy := ddy + 1; end; ddx := ddx + 1; end; CountAj := n; end; procedure NewGame; var k, mx, my: integer; begin for k := 0 to 80 do begin mine[k] := 0; opn[k] := 0; flag[k] := 0; nbr[k] := 0; end; seed := 1234 + frames; k := 0; while k < 10 do begin seed := (seed + 7919) mod 32768; mx := seed div 3641; seed := (seed + 7919) mod 32768; my := seed div 3641; if mine[mx * 9 + my] = 0 then begin mine[mx * 9 + my] := 1; k := k + 1; end; end; opened := 0; dead := 0; won := 0; flagm := 0; frames := 0; end; begin ui_win_open('扫雷', 0, 0); ui_scr_set_keep_on(1); w := ui_scr_get_w(); h := ui_scr_get_h(); if w <= 0 then w := 360; if h <= 0 then h := 620; { 版面全部由 scr_w / scr_h 现算:顶部状态行 + 模式按钮 + 9×9 棋盘 } fontStatus := h div 40; if fontStatus < 10 then fontStatus := 10; if fontStatus > 16 then fontStatus := 16; statusY := 8 + fontStatus; btnH := h div 18; if btnH < 24 then btnH := 24; if btnH > 44 then btnH := 44; fontBtn := btnH div 2; if fontBtn < 11 then fontBtn := 11; if fontBtn > 18 then fontBtn := 18; btnX := 8; btnY := statusY + 6; btnW := fontBtn * 4 + 20; topH := btnY + btnH + 8; cell := (w - 16) div 9; if (h - topH - 4) div 9 < cell then cell := (h - topH - 4) div 9; if cell < 12 then cell := 12; if cell > 120 then cell := 120; ox := (w - cell * 9) div 2; oy := topH; fontNum := cell div 2; if fontNum < 10 then fontNum := 10; if fontNum > 30 then fontNum := 30; frames := 0; NewGame; tid := ui_timer_set(80, 0); while frames < 4000 do begin ui_2d_clear($FF101018); if flagm = 0 then begin ui_2d_rect(btnX, btnY, btnW, btnH, $FF2A2A32, 1, 0, 8); ui_2d_text(btnX + 10, btnY + btnH div 2 + fontBtn div 3, '翻开模式', $FFFFFFFF, fontBtn, 0); end else begin ui_2d_rect(btnX, btnY, btnW, btnH, $FFD99A26, 1, 0, 8); ui_2d_text(btnX + 10, btnY + btnH div 2 + fontBtn div 3, '插旗模式', $FF101018, fontBtn, 0); end; Str(opened, s); ui_2d_text(w - 10, btnY + btnH div 2 + fontBtn div 3, s, $FF8A9AA8, fontBtn + 2, 2); x := 0; while x < 9 do begin y := 0; while y < 9 do begin idx := x * 9 + y; px := ox + x * cell; py := oy + y * cell; if opn[idx] = 1 then begin if mine[idx] = 1 then ui_2d_rect(px + 1, py + 1, cell - 2, cell - 2, $FFE05A30, 1, 0, 2) else begin ui_2d_rect(px + 1, py + 1, cell - 2, cell - 2, $FF1A1A22, 1, 0, 2); if nbr[idx] > 0 then begin Str(nbr[idx], s); ui_2d_text(px + cell div 2, py + cell div 2 + fontNum div 3, s, $FFFFFFFF, fontNum, 1); end; end; end else begin ui_2d_rect(px + 1, py + 1, cell - 2, cell - 2, $FF2A2A32, 1, 0, 2); if flag[idx] = 1 then ui_2d_circle(px + cell div 2, py + cell div 2, cell div 4, $FFD99A26, 1, 0); end; y := y + 1; end; x := x + 1; end; if dead = 1 then ui_2d_text(8, statusY, '踩雷了,ESC 重来', $FFE05A30, fontStatus + 2, 0) else if won = 1 then ui_2d_text(8, statusY, '扫清了!ESC 再来', $FF66BB6A, fontStatus + 2, 0) else ui_2d_text(8, statusY, '点格子翻开 / 点顶部按钮插旗', $FF8A9AA8, fontStatus, 0); ui_2d_present(); msg := ui_msg_fetch_wait(0); if msg = 10 then frames := 4000; if msg = 6 then begin ax := ui_msg_get_a(); ay := ui_msg_get_b(); if (ax >= btnX) and (ax <= btnX + btnW) and (ay >= btnY) and (ay <= btnY + btnH) then begin if flagm = 0 then flagm := 1 else flagm := 0; end else if ax >= ox then begin if ay >= oy then begin x := (ax - ox) div cell; y := (ay - oy) div cell; if x < 9 then begin if y < 9 then begin idx := x * 9 + y; if (dead = 0) and (won = 0) then begin if flagm = 1 then begin if opn[idx] = 0 then begin if flag[idx] = 1 then flag[idx] := 0 else flag[idx] := 1; end; end else if (opn[idx] = 0) and (flag[idx] = 0) then begin qx[1] := x; qy[1] := y; head := 1; tail := 1; while head <= tail do begin cx := qx[head]; cy := qy[head]; head := head + 1; idx := cx * 9 + cy; if opn[idx] = 1 then idx := -1; if idx >= 0 then begin opn[idx] := 1; opened := opened + 1; if mine[idx] = 1 then begin dead := 1; head := tail + 1; end else begin nb := CountAj(cx, cy); nbr[idx] := nb; if nb = 0 then begin dx := -1; while dx <= 1 do begin dy := -1; while dy <= 1 do begin if (dx <> 0) or (dy <> 0) then begin nx := cx + dx; ny := cy + dy; if nx >= 0 then begin if ny >= 0 then begin if nx < 9 then begin if ny < 9 then begin if tail < 80 then begin tail := tail + 1; qx[tail] := nx; qy[tail] := ny; end; end; end; end; end; end; dy := dy + 1; end; dx := dx + 1; end; end; end; end; end; if opened >= 71 then won := 1; end; end; end; end; end; end; end; if msg = 1 then begin key := ui_msg_get_a(); if key = 27 then NewGame; end; if msg = 9 then frames := frames + 1; end; ui_timer_kill(tid); ui_scr_set_keep_on(0); ui_win_close(); end.