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

pascal/app/puzzle15.pas · 4898 字节 · 下载原文

▶ 在浏览器里试玩

(* 数字华容道 —— 用 **Pascal** 写的手机小游戏(4×4 滑块)
   Puzzle 15 -- a small phone game written in **Pascal** (4x4 sliding)

   玩法:点一下空位旁边的数字,它就滑进空位;把 1..15 排回顺序即通关
   (右下角留空)。ESC 重排。

   跑法 —— 手机:vml run examples/pascal/puzzle15.pas
   跑法 —— 桌面:vmlcli Examples/pascal/puzzle15.pas --screen 480x640 --frames /tmp/fr
*)

program Puzzle15;

uses vml_ui;

var
  tile: array[0..15] of integer;
  w, h, cell, gap, ox, oy, i, j, idx, e, frames: integer;
  hdrH, footH, hdrY, fontTile, fontBig, fontSmall: integer;
  tx, ty, c, r, tmp, tmp2, seed, moves, won, msg, ax, ay, key, tid: integer;
  s, t: string;

procedure Shuffle;
var
  k, cc, rr, tt, sd: integer;
begin
  for k := 0 to 14 do
    tile[k] := k + 1;
  tile[15] := 0;
  sd := 1234 + moves;
  for k := 0 to 399 do
  begin
    sd := (sd + 7919) mod 32768;
    cc := sd div 2048;
    sd := (sd + 7919) mod 32768;
    rr := sd div 2048;
    tt := tile[cc];
    tile[cc] := tile[rr];
    tile[rr] := tt;
  end;
  e := 15;
  for k := 0 to 15 do
    if tile[k] = 0 then
      e := k;
  moves := 0;
  won := 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 现算:顶部信息区 + 4×4 滑块 + 底部提示 }
  hdrH := h div 5;
  if hdrH < 64 then
    hdrH := 64;
  if hdrH > 130 then
    hdrH := 130;
  footH := 30;
  cell := (w - 24) div 4;
  if (h - hdrH - footH) div 4 < cell then
    cell := (h - hdrH - footH) div 4;
  if cell < 16 then
    cell := 16;
  if cell > 230 then
    cell := 230;
  ox := (w - cell * 4) div 2;
  oy := hdrH;
  gap := cell div 30;
  if gap < 4 then
    gap := 4;
  if gap > 8 then
    gap := 8;
  hdrY := hdrH div 3;
  fontTile := cell div 4;
  if fontTile < 10 then
    fontTile := 10;
  if fontTile > 44 then
    fontTile := 44;
  fontBig := cell div 8;
  if fontBig < 16 then
    fontBig := 16;
  if fontBig > 30 then
    fontBig := 30;
  fontSmall := hdrH div 6;
  if fontSmall < 10 then
    fontSmall := 10;
  if fontSmall > 16 then
    fontSmall := 16;
  moves := 0;
  won := 0;
  frames := 0;
  Shuffle;
  tid := ui_timer_set(200, 0);

  while frames < 30000 do
  begin
    ui_2d_clear($FF101018);
    for idx := 0 to 15 do
    begin
      r := idx div 4;
      c := idx - r * 4;
      tx := ox + c * cell;
      ty := oy + r * cell;
      if tile[idx] = 0 then
        ui_2d_rect(tx, ty, cell - gap, cell - gap, $FF101018, 1, 0, 4)
      else
      begin
        ui_2d_rect(tx, ty, cell - gap, cell - gap, $FF2A2A32, 1, 0, 4);
        Str(tile[idx], s);
        ui_2d_text(tx + (cell - gap) div 2, ty + (cell - gap) div 2 + fontTile div 3,
                   s, $FFFFFFFF, fontTile, 1);
      end;
    end;

    Str(moves, s);
    ui_2d_text(ox, hdrY, s, $FFFFFFFF, fontBig, 0);
    ui_2d_text(ox, hdrY + fontBig, '步数', $FF8A9AA8, fontSmall, 0);
    if won = 1 then
      ui_2d_text(w div 2, hdrH - 8, '排好了!ESC 再来', $FF66BB6A, fontSmall + 2, 1)
    else
      ui_2d_text(w div 2, hdrH - 8, '点空位旁边的数字', $FF8A9AA8, fontSmall, 1);
    ui_2d_text(w div 2, h - 8, 'ESC 重排', $FF8A9AA8, fontSmall, 1);
    ui_2d_present();

    msg := ui_msg_fetch_wait(0);
    if msg = 10 then
      frames := 30000;

    if msg = 6 then
    begin
      ax := ui_msg_get_a();
      ay := ui_msg_get_b();
      if (ax >= ox) and (ay >= oy) then
      begin
        c := (ax - ox) div cell;
        r := (ay - oy) div cell;
        if (c < 4) and (r < 4) and (won = 0) then
        begin
          idx := r * 4 + c;
          if (idx - 4 = e) and (idx >= 4) then
          begin
            tmp := tile[idx];
            tile[idx] := tile[e];
            tile[e] := tmp;
            e := idx;
            moves := moves + 1;
          end
          else if (idx + 4 = e) and (idx <= 11) then
          begin
            tmp := tile[idx];
            tile[idx] := tile[e];
            tile[e] := tmp;
            e := idx;
            moves := moves + 1;
          end
          else if (idx - 1 = e) and (c > 0) then
          begin
            tmp := tile[idx];
            tile[idx] := tile[e];
            tile[e] := tmp;
            e := idx;
            moves := moves + 1;
          end
          else if (idx + 1 = e) and (c < 3) then
          begin
            tmp := tile[idx];
            tile[idx] := tile[e];
            tile[e] := tmp;
            e := idx;
            moves := moves + 1;
          end;
        end;
      end;
    end;

    if msg = 1 then
    begin
      key := ui_msg_get_a();
      if key = 27 then
        Shuffle;
    end;

    if msg = 9 then
      frames := frames + 1;
  end;

  ui_timer_kill(tid);
  ui_scr_set_keep_on(0);
  ui_win_close();
end.