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

dart/app/puzzle15.dart · 6419 字节 · 下载原文

▶ 在浏览器里试玩

// 十五格拼图(Puzzle 15)—— Dart 版小游戏
// Puzzle 15 — a small game in the Dart edition
//
// 玩法(与 `Examples/c/app/puzzle15.c` / `Examples/python/app/puzzle15.py` 同一套):
// Gameplay (same rules as the C / Python versions):
//   · 4×4 共 15 个数字加一个空格;点空格旁边的格子(或按方向键)把格子滑进空格;
//   · 4x4 with fifteen numbered tiles and one blank; tap a tile next to the blank (or press an arrow)
//     to slide it into the blank;
//   · 把 1..15 按顺序排好、空格落在右下角即通关;
//   · put 1..15 in order with the blank in the bottom-right corner to win;
//   · 打乱靠**随机走步**(从解决态出发随机走 200 步),所以一定可解。
//   · shuffling is done by **random legal moves** (200 steps from the solved state), so it is always solvable.
//
// ## Dart 这一路的写法约束(见 `Examples/dart/app/snake.dart` 文件头)
// ## Style constraints on this Dart path (see the header of `Examples/dart/app/snake.dart`)
//
//   · **全部状态放 `main` 的局部变量**;颜色写十六进制;整除写 `~/`;数字用字符串插值;
//   · **all state lives in locals of `main`**; hex colours; `~/` for integer division; string interpolation;
//   · ⚠ 双循环里不要把循环内赋值的变量作实参 ⇒ 绘制内联表达式。
//   · ⚠ never pass a variable assigned inside a nested loop as an argument => inline when drawing.
//
// 跑法 —— 手机:`vml run examples/dart/puzzle15.dart`
// How to run — on the phone: `vml run examples/dart/puzzle15.dart`
// 跑法 —— 桌面:`vmlcli Examples/dart/puzzle15.dart --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/dart/puzzle15.dart --screen 480x640 --frames /tmp/fr`

import 'vml_ui.dart';

void main() {
  int sw = 360;
  int sh = 620;
  int cell = 80;
  int ox = 20;
  int oy = 110;
  int seed = 20264;
  int moves = 0;
  int blank = 15;
  int won = 0;
  int frames = 0;
  int i = 0;
  int j = 0;
  int n = 0;
  int t = 0;
  int k = 0;
  int bx = 0;
  int by = 0;
  int tx = 0;
  int ty = 0;
  int cx = 0;
  int cy = 0;
  int done = 0;
  int r = 0;
  String hud = "";

  int cBg = 0xFF101018;
  int cTile = 0xFF2A2A36;
  int cTile2 = 0xFF3A3A48;
  int cText = 0xFFE8E8F0;
  int cDim = 0xFF9AA0B0;
  int cWin = 0xFF51E86E;

  List<int> b = [];
  i = 0;
  while (i < 16) { b.add(0); i = i + 1; }

  int lang = ui_scr_get_language();
  ui_win_open("十五格拼图", 0, 0);

  sw = ui_scr_get_w();
  sh = ui_scr_get_h();
  if (sw <= 0) { sw = 360; }
  if (sh <= 0) { sh = 620; }
  // 宽高两头夹:小屏把顶部空白压掉,大屏不让棋盘顶出底部提示
  // Clamp on both axes: small screens drop the top gap, big screens keep the board above the hint.
  int side = sw - 20;
  int sideH = sh - 64 - 34;
  if (sideH < side) { side = sideH; }
  cell = side ~/ 4;
  if (cell < 12) { cell = 12; }
  ox = (sw - 4 * cell) ~/ 2;
  oy = 64;

  // 解决态
  i = 0;
  while (i < 16) { b[i] = i + 1; i = i + 1; }
  b[15] = 0;
  blank = 15;

  // 随机走 200 步打乱(一定可解)
  n = 0;
  while (n < 200) {
    seed = (seed * 1103 + 12345) % 32768;
    r = (seed ~/ 256) % 4;
    bx = blank % 4;
    by = blank ~/ 4;
    j = blank;
    if (r == 0 && bx > 0) { j = blank - 1; }
    else if (r == 1 && bx < 3) { j = blank + 1; }
    else if (r == 2 && by > 0) { j = blank - 4; }
    else if (r == 3 && by < 3) { j = blank + 4; }
    if (j != blank) {
      b[blank] = b[j];
      b[j] = 0;
      blank = j;
    }
    n = n + 1;
  }
  moves = 0;

  int tid = ui_timer_set(60, 1);
  ui_2d_present();

  while (done == 0 && ui_win_is_closed() == 0) {
    t = ui_msg_fetch_wait(60);
    if (t == 10) { break; }

    if (t == 1) {
      k = ui_msg_get_a();
      if (k == 27) { done = 1; }
      bx = blank % 4;
      by = blank ~/ 4;
      j = blank;
      if (k == 37 && bx < 3) { j = blank + 1; }
      if (k == 39 && bx > 0) { j = blank - 1; }
      if (k == 38 && by < 3) { j = blank + 4; }
      if (k == 40 && by > 0) { j = blank - 4; }
      if (j != blank) {
        b[blank] = b[j];
        b[j] = 0;
        blank = j;
        moves = moves + 1;
      }
    }

    if (t == 6 || t == 4) {
      tx = ui_msg_get_a();
      ty = ui_msg_get_b();
      if (tx >= ox && ty >= oy) {
        cx = (tx - ox) ~/ cell;
        cy = (ty - oy) ~/ cell;
        if (cx >= 0 && cy >= 0 && cx < 4 && cy < 4) {
          j = cy * 4 + cx;
          bx = blank % 4;
          by = blank ~/ 4;
          if ((cx == bx && (cy == by + 1 || cy == by - 1)) ||
              (cy == by && (cx == bx + 1 || cx == bx - 1))) {
            b[blank] = b[j];
            b[j] = 0;
            blank = j;
            moves = moves + 1;
          }
        }
      }
    }

    // 判胜
    won = 1;
    i = 0;
    while (i < 15) {
      if (b[i] != i + 1) { won = 0; }
      i = i + 1;
    }
    if (b[15] != 0) { won = 0; }

    ui_2d_clear(cBg);
    i = 0;
    while (i < 16) {
      if (b[i] == 0) {
        ui_2d_rect(ox + (i % 4) * cell + 2, oy + (i ~/ 4) * cell + 2, cell - 4, cell - 4, 0xFF1A1A24, 1, 0, 4);
      } else {
        if (i == b[i] - 1) {
          ui_2d_rect(ox + (i % 4) * cell + 2, oy + (i ~/ 4) * cell + 2, cell - 4, cell - 4, cTile2, 1, 0, 4);
        } else {
          ui_2d_rect(ox + (i % 4) * cell + 2, oy + (i ~/ 4) * cell + 2, cell - 4, cell - 4, cTile, 1, 0, 4);
        }
        ui_2d_text(ox + (i % 4) * cell + cell ~/ 2, oy + (i ~/ 4) * cell + cell ~/ 2 + cell ~/ 8,
            "${b[i]}", cText, cell ~/ 2, 1);
      }
      i = i + 1;
    }

    hud = "$moves";
    ui_2d_text(10, 24, hud, cText, 15, 0);
    if (lang == 0) { ui_2d_text(10, 46, "步数", cDim, 10, 0); }
    else { ui_2d_text(10, 46, "moves", cDim, 10, 0); }

    int msgY = oy + 4 * cell + 24;
    if (msgY > sh - 16) { msgY = sh - 16; }
    if (won == 1) {
      if (lang == 0) { ui_2d_text(sw ~/ 2, msgY, "排好了!", cWin, 16, 1); }
      else { ui_2d_text(sw ~/ 2, msgY, "Solved!", cWin, 16, 1); }
    } else {
      if (lang == 0) { ui_2d_text(sw ~/ 2, msgY, "点空格旁的数字 / 方向键滑动", cDim, 11, 1); }
      else { ui_2d_text(sw ~/ 2, msgY, "Tap a tile beside the blank / arrows", cDim, 11, 1); }
    }
    ui_2d_present();

    frames = frames + 1;
    if (frames >= 20000) { done = 1; }
  }

  ui_timer_kill(tid);
  ui_win_close();
}