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

dart/app/g2048.dart · 10089 字节 · 下载原文

▶ 在浏览器里试玩

// 2048 —— Dart 版小游戏
// 2048 — a small game in the Dart edition
//
// 玩法(与 `Examples/c/app/game2048.c` / `Examples/python/app/game2048.py` 同一套):
// Gameplay (same rules as the C / Python versions):
//   · 4×4 棋盘,方向键 / 点屏幕四边往一个方向滑;
//   · a 4x4 board; arrow keys / tapping one of the four screen edges slide everything that way;
//   · 相同数字相撞合成两倍、得分加上合成值,每滑一次冒一个新数字(10% 是 4);
//   · equal numbers merge and score the merged value; each slide spawns a new tile (4 with 10% odds);
//   · 到 2048 算赢(还能继续),没空格且四邻无相等算输;ESC / R 重开。
//   · reaching 2048 wins (you may keep playing); no empty cell and no adjacent equal pair loses; ESC / R restarts.
//
// ## 合并的**先后**容易写错
// ## The **merge order** is easy to get wrong
//
// 一次滑动里每个块只许合并一次(`4 4 4 4` 滑一次是 `8 8`,不是 `16`),所以从滑动方向的**最远端**往回收。
// Each tile may merge only once per slide (`4 4 4 4` becomes `8 8`, not `16`), so collapse from the far end.
//
// ## 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;
//   · ⚠ 双循环里**不要把循环内赋值的变量作实参**(实测会错位,见 `memory.dart` / `whack.dart`)
//     ⇒ 绘制一律内联表达式。
//   · ⚠ **never pass a variable assigned inside a nested loop as an argument** (measured to go wrong;
//     see `memory.dart` / `whack.dart`) => inline the expressions when drawing.
//
// 跑法 —— 手机:`vml run examples/dart/g2048.dart`
// How to run — on the phone: `vml run examples/dart/g2048.dart`
// 跑法 —— 桌面:`vmlcli Examples/dart/g2048.dart --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/dart/g2048.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 = 120;
  int score = 0;
  int won = 0;
  int lost = 0;
  int seed = 20261;
  int frames = 0;
  int i = 0;
  int k = 0;
  int d = 0;
  int j = 0;
  int n = 0;
  int out = 0;
  int pick = 0;
  int changed = 0;
  int t = 0;
  int tx = 0;
  int ty = 0;
  int done = 0;
  int dl = 0;
  int dr = 0;
  int dt = 0;
  int db = 0;
  String hud = "";

  int cBg = 0xFF101018;
  int cEmpty = 0xFF1A1A24;
  int cText = 0xFFE8E8F0;
  int cDim = 0xFF9AA0B0;

  // 棋盘 16 格 + 合并缓冲 4 + 原值快照 4
  // Board: 16 slots + a 4-slot merge buffer + a 4-slot snapshot
  List<int> b = [];
  i = 0;
  while (i < 24) { b.add(0); i = i + 1; }

  int lang = ui_scr_get_language();
  ui_win_open("2048", 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;

  // 开局:两粒
  // Opening: two tiles
  seed = 20261;
  i = 0;
  while (i < 16) { b[i] = 0; i = i + 1; }
  j = 0;
  while (j < 2) {
    seed = (seed * 1103 + 12345) % 32768;
    pick = (seed ~/ 256) % 16;
    while (b[pick] != 0) {
      seed = (seed * 1103 + 12345) % 32768;
      pick = (seed ~/ 256) % 16;
    }
    b[pick] = 2;
    j = j + 1;
  }

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

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

    d = -1;
    if (t == 1) {
      k = ui_msg_get_a();
      if (k == 37) { d = 0; }
      if (k == 39) { d = 1; }
      if (k == 38) { d = 2; }
      if (k == 40) { d = 3; }
      if (k == 27 || k == 82) {
        i = 0;
        while (i < 16) { b[i] = 0; i = i + 1; }
        score = 0;
        won = 0;
        lost = 0;
        seed = 20261;
        b[0] = 2;
        b[5] = 2;
      }
    }

    if (t == 6 || t == 4) {
      tx = ui_msg_get_a();
      ty = ui_msg_get_b();
      dl = tx;
      dr = sw - tx;
      dt = ty;
      db = sh - ty;
      if (dl <= dr && dl <= dt && dl <= db) { d = 0; }
      else if (dr <= dt && dr <= db) { d = 1; }
      else if (dt <= db) { d = 2; }
      else { d = 3; }
    }

    if (d >= 0 && lost == 0) {
      changed = 0;
      k = 0;
      while (k < 4) {
        i = 0;
        while (i < 4) {
          if (d == 0) { b[16 + i] = b[k * 4 + i]; }
          if (d == 1) { b[16 + i] = b[k * 4 + (3 - i)]; }
          if (d == 2) { b[16 + i] = b[i * 4 + k]; }
          if (d == 3) { b[16 + i] = b[(3 - i) * 4 + k]; }
          b[20 + i] = b[16 + i];
          i = i + 1;
        }
        // 收拢
        n = 0;
        i = 0;
        while (i < 4) {
          if (b[16 + i] != 0) { b[16 + n] = b[16 + i]; n = n + 1; }
          i = i + 1;
        }
        i = n;
        while (i < 4) { b[16 + i] = 0; i = i + 1; }
        // 合并
        out = 0;
        i = 0;
        while (i < n) {
          if (i + 1 < n && b[16 + i] == b[16 + i + 1]) {
            b[16 + out] = b[16 + i] * 2;
            score = score + b[16 + i] * 2;
            if (b[16 + i] * 2 >= 2048) { won = 1; }
            i = i + 2;
          } else {
            b[16 + out] = b[16 + i];
            i = i + 1;
          }
          out = out + 1;
        }
        i = out;
        while (i < 4) { b[16 + i] = 0; i = i + 1; }
        i = 0;
        while (i < 4) {
          if (b[16 + i] != b[20 + i]) { changed = 1; }
          i = i + 1;
        }
        // 写回
        i = 0;
        while (i < 4) {
          if (d == 0) { b[k * 4 + i] = b[16 + i]; }
          if (d == 1) { b[k * 4 + (3 - i)] = b[16 + i]; }
          if (d == 2) { b[i * 4 + k] = b[16 + i]; }
          if (d == 3) { b[(3 - i) * 4 + k] = b[16 + i]; }
          i = i + 1;
        }
        k = k + 1;
      }
      if (changed != 0) {
        // 冒一个新数字
        n = 0;
        i = 0;
        while (i < 16) {
          if (b[i] == 0) { n = n + 1; }
          i = i + 1;
        }
        if (n > 0) {
          seed = (seed * 1103 + 12345) % 32768;
          pick = (seed ~/ 256) % n;
          i = 0;
          while (i < 16) {
            if (b[i] == 0) {
              if (pick == 0) {
                seed = (seed * 1103 + 12345) % 32768;
                if ((seed ~/ 256) % 10 == 0) { b[i] = 4; } else { b[i] = 2; }
                i = 16;
              } else {
                pick = pick - 1;
              }
            }
            if (i < 16) { i = i + 1; }
          }
        }
        // 还能不能走
        n = 0;
        i = 0;
        while (i < 16) {
          if (b[i] == 0) { n = 1; }
          i = i + 1;
        }
        i = 0;
        while (i < 15) {
          if (i % 4 != 3 && b[i] == b[i + 1]) { n = 1; }
          if (i + 4 < 16 && b[i] == b[i + 4]) { n = 1; }
          i = i + 1;
        }
        if (n == 0) { lost = 1; }
      }
    }

    // ── 绘制(内联表达式,见文件头)──
    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, cEmpty, 1, 0, 6);
      } else {
        if (b[i] == 2) { ui_2d_rect(ox + (i % 4) * cell + 2, oy + (i ~/ 4) * cell + 2, cell - 4, cell - 4, 0xFF2A2A36, 1, 0, 6); }
        else if (b[i] == 4) { ui_2d_rect(ox + (i % 4) * cell + 2, oy + (i ~/ 4) * cell + 2, cell - 4, cell - 4, 0xFF3A3A48, 1, 0, 6); }
        else if (b[i] == 8) { ui_2d_rect(ox + (i % 4) * cell + 2, oy + (i ~/ 4) * cell + 2, cell - 4, cell - 4, 0xFF4A90D9, 1, 0, 6); }
        else if (b[i] == 16) { ui_2d_rect(ox + (i % 4) * cell + 2, oy + (i ~/ 4) * cell + 2, cell - 4, cell - 4, 0xFF51E86E, 1, 0, 6); }
        else if (b[i] == 32) { ui_2d_rect(ox + (i % 4) * cell + 2, oy + (i ~/ 4) * cell + 2, cell - 4, cell - 4, 0xFFD9B44A, 1, 0, 6); }
        else if (b[i] == 64) { ui_2d_rect(ox + (i % 4) * cell + 2, oy + (i ~/ 4) * cell + 2, cell - 4, cell - 4, 0xFFE06C50, 1, 0, 6); }
        else if (b[i] == 128) { ui_2d_rect(ox + (i % 4) * cell + 2, oy + (i ~/ 4) * cell + 2, cell - 4, cell - 4, 0xFFE06CA8, 1, 0, 6); }
        else if (b[i] == 256) { ui_2d_rect(ox + (i % 4) * cell + 2, oy + (i ~/ 4) * cell + 2, cell - 4, cell - 4, 0xFF9B6CF0, 1, 0, 6); }
        else if (b[i] == 512) { ui_2d_rect(ox + (i % 4) * cell + 2, oy + (i ~/ 4) * cell + 2, cell - 4, cell - 4, 0xFF4AD9C6, 1, 0, 6); }
        else { ui_2d_rect(ox + (i % 4) * cell + 2, oy + (i ~/ 4) * cell + 2, cell - 4, cell - 4, 0xFFC0C040, 1, 0, 6); }
        ui_2d_text(ox + (i % 4) * cell + cell ~/ 2, oy + (i ~/ 4) * cell + cell ~/ 2 + cell ~/ 6,
            "${b[i]}", cText, cell ~/ 3, 1);
      }
      i = i + 1;
    }

    hud = "$score";
    ui_2d_text(10, 24, hud, cText, 18, 0);
    if (lang == 0) { ui_2d_text(10, 48, "得分", cDim, 10, 0); }
    else { ui_2d_text(10, 48, "score", cDim, 10, 0); }

    int msgY = oy + 4 * cell + 24;
    if (msgY > sh - 16) { msgY = sh - 16; }
    if (lost != 0) {
      if (lang == 0) { ui_2d_text(sw ~/ 2, msgY, "没路可走了,ESC / R 重开", cText, 14, 1); }
      else { ui_2d_text(sw ~/ 2, msgY, "No moves left - ESC / R to restart", cText, 14, 1); }
    } else {
      if (lang == 0) { ui_2d_text(sw ~/ 2, msgY, "方向键 / 点屏幕四边滑动", cDim, 11, 1); }
      else { ui_2d_text(sw ~/ 2, msgY, "Arrows / tap a screen edge to slide", cDim, 11, 1); }
    }
    ui_2d_present();

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

  ui_timer_kill(tid);
  ui_win_close();
}