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

ruby/app/g2048.rb · 12477 字节 · 下载原文

▶ 在浏览器里试玩

# 2048 —— 用 **Ruby** 写的手机小游戏(合并 / 数字)
# 2048 — a small phone game written in **Ruby** (merge / numbers)
#
# 玩法(与 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.
#
# ## Ruby 这一路的写法约束(见 Examples/ruby/app/minesweeper.rb 文件头)
# ## Style constraints on this Ruby path (see the header of Examples/ruby/app/minesweeper.rb)
#
#   · **所有 def 放在文件最前面,全局变量赋值在它们之后**;
#   · **every def sits at the very top, and global assignments come after them**;
#   · 状态一律 g_ 前缀;数组用 [] + push 填充;数字转字符串用 .to_s;颜色写负数十进制;
#   · state is g_-prefixed; arrays are built with [] + push; numbers to strings use .to_s;
#     colours are negative decimals;
#   · **"只写不读"的名字在函数里写的是该函数自己的局部** ⇒ 用 gt_touch_state 先把状态名各读一遍。
#   · **a name that is only written and never read gets written to the function's own local**
#     ⇒ gt_touch_state reads every state name once first.
#
# 跑法 —— 手机:vml run examples/ruby/g2048.rb
# How to run — on the phone: vml run examples/ruby/g2048.rb
# 跑法 —— 桌面:vmlcli Examples/ruby/g2048.rb --screen 480x640 --frames /tmp/fr
# How to run — on the desktop: vmlcli Examples/ruby/g2048.rb --screen 480x640 --frames /tmp/fr

# UI 库的显式引用(用户 2026-10-01 定的口径),声明文件 Lib/ruby/vml_ui.rb 由 GenLib 生成。
require 'vml_ui'

# ══════════════════════════════════════════════════════════════════════════
# 第一段:全部函数(⚠ 必须排在"给全局赋值"之前,理由见文件头那条)
# ══════════════════════════════════════════════════════════════════════════

def gt_touch_state()
  return g_board.length + g_line.length + g_old.length + g_score + g_won + g_lost + g_seed + g_cell + g_ox + g_oy + g_sw + g_sh + g_lang + g_frames + g_m + g_fs_score + g_fs_lbl + g_fs_hint + g_fs_tile + g_hint_y
end

def gt_relayout()
  g_sw = ui_scr_get_w()
  g_sh = ui_scr_get_h()
  if g_sw <= 0
    g_sw = 360
  end
  if g_sh <= 0
    g_sh = 620
  end
  # 字号随 min(W,H) 缩放;棋盘同时受宽和高约束(240x240 上旧写法只给 70px ⇒ 格子 17px)。
  # Font sizes scale with min(W,H); the board is bounded by both width and height (the
  # old layout left only 70 px on 240x240 => 17 px cells).
  g_m = g_sw
  if g_sh < g_m
    g_m = g_sh
  end
  g_fs_score = g_m / 26
  if g_fs_score < 12
    g_fs_score = 12
  end
  if g_fs_score > 18
    g_fs_score = 18
  end
  g_fs_lbl = g_m / 48
  if g_fs_lbl < 9
    g_fs_lbl = 9
  end
  if g_fs_lbl > 12
    g_fs_lbl = 12
  end
  g_fs_hint = g_m / 30
  if g_fs_hint < 8
    g_fs_hint = 8
  end
  if g_fs_hint > 12
    g_fs_hint = 12
  end
  g_oy = 6 + g_fs_score + 2 + g_fs_lbl + 8
  side = g_sw - 16
  avail = g_sh - g_oy - g_fs_hint - 22
  if avail < side
    side = avail
  end
  g_cell = side / 4
  if g_cell < 6
    g_cell = 6
  end
  g_ox = (g_sw - 4 * g_cell) / 2
  g_fs_tile = g_cell / 3
  if g_fs_tile < 10
    g_fs_tile = 10
  end
  if g_fs_tile > 64
    g_fs_tile = 64
  end
  g_hint_y = g_oy + 4 * g_cell + 16
  if g_hint_y > g_sh - g_fs_hint - 6
    g_hint_y = g_sh - g_fs_hint - 6
  end
end

def gt_spawn()
  n = 0
  i = 0
  while i < 16
    if g_board[i] == 0
      n = n + 1
    end
    i = i + 1
  end
  if n == 0
    return 0
  end
  g_seed = (g_seed * 1103 + 12345) % 32768
  pick = (g_seed / 256) % n
  i = 0
  while i < 16
    if g_board[i] == 0
      if pick == 0
        g_seed = (g_seed * 1103 + 12345) % 32768
        if (g_seed / 256) % 10 == 0
          g_board[i] = 4
        else
          g_board[i] = 2
        end
        i = 16
      else
        pick = pick - 1
      end
    end
    if i < 16
      i = i + 1
    end
  end
  return 0
end

def gt_new_game()
  i = 0
  while i < 16
    g_board[i] = 0
    i = i + 1
  end
  g_score = 0
  g_won = 0
  g_lost = 0
  g_frames = 0
  gt_spawn()
  gt_spawn()
end

def gt_read_line(d, k)
  i = 0
  while i < 4
    if d == 0
      g_line[i] = g_board[k * 4 + i]
    end
    if d == 1
      g_line[i] = g_board[k * 4 + (3 - i)]
    end
    if d == 2
      g_line[i] = g_board[i * 4 + k]
    end
    if d == 3
      g_line[i] = g_board[(3 - i) * 4 + k]
    end
    i = i + 1
  end
end

def gt_write_line(d, k)
  i = 0
  while i < 4
    if d == 0
      g_board[k * 4 + i] = g_line[i]
    end
    if d == 1
      g_board[k * 4 + (3 - i)] = g_line[i]
    end
    if d == 2
      g_board[i * 4 + k] = g_line[i]
    end
    if d == 3
      g_board[(3 - i) * 4 + k] = g_line[i]
    end
    i = i + 1
  end
end

# 滑一次:从远端收拢 + 每个块只合并一次;有变化返回 1
def gt_slide(d)
  changed = 0
  k = 0
  while k < 4
    gt_read_line(d, k)
    i = 0
    while i < 4
      g_old[i] = g_line[i]
      i = i + 1
    end
    n = 0
    i = 0
    while i < 4
      if g_line[i] != 0
        g_line[n] = g_line[i]
        n = n + 1
      end
      i = i + 1
    end
    i = n
    while i < 4
      g_line[i] = 0
      i = i + 1
    end
    out = 0
    i = 0
    while i < n
      if i + 1 < n && g_line[i] == g_line[i + 1]
        g_line[out] = g_line[i] * 2
        g_score = g_score + g_line[i] * 2
        if g_line[i] * 2 >= 2048
          g_won = 1
        end
        i = i + 2
      else
        g_line[out] = g_line[i]
        i = i + 1
      end
      out = out + 1
    end
    i = out
    while i < 4
      g_line[i] = 0
      i = i + 1
    end
    i = 0
    while i < 4
      if g_line[i] != g_old[i]
        changed = 1
      end
      i = i + 1
    end
    gt_write_line(d, k)
    k = k + 1
  end
  return changed
end

def gt_can_move()
  i = 0
  while i < 16
    if g_board[i] == 0
      return 1
    end
    i = i + 1
  end
  x = 0
  while x < 4
    y = 0
    while y < 4
      if x + 1 < 4 && g_board[x * 4 + y] == g_board[(x + 1) * 4 + y]
        return 1
      end
      if y + 1 < 4 && g_board[x * 4 + y] == g_board[x * 4 + y + 1]
        return 1
      end
      y = y + 1
    end
    x = x + 1
  end
  return 0
end

def gt_draw()
  ui_2d_clear(g_c_bg)
  x = 0
  while x < 4
    y = 0
    while y < 4
      v = g_board[x * 4 + y]
      px = g_ox + x * g_cell
      py = g_oy + y * g_cell
      if v == 0
        ui_2d_rect(px + 2, py + 2, g_cell - 4, g_cell - 4, g_c_empty, 1, 0, 6)
      else
        if v == 2
          ui_2d_rect(px + 2, py + 2, g_cell - 4, g_cell - 4, g_c_t2, 1, 0, 6)
        end
        if v == 4
          ui_2d_rect(px + 2, py + 2, g_cell - 4, g_cell - 4, g_c_t4, 1, 0, 6)
        end
        if v == 8
          ui_2d_rect(px + 2, py + 2, g_cell - 4, g_cell - 4, g_c_t8, 1, 0, 6)
        end
        if v == 16
          ui_2d_rect(px + 2, py + 2, g_cell - 4, g_cell - 4, g_c_t16, 1, 0, 6)
        end
        if v == 32
          ui_2d_rect(px + 2, py + 2, g_cell - 4, g_cell - 4, g_c_t32, 1, 0, 6)
        end
        if v == 64
          ui_2d_rect(px + 2, py + 2, g_cell - 4, g_cell - 4, g_c_t64, 1, 0, 6)
        end
        if v == 128
          ui_2d_rect(px + 2, py + 2, g_cell - 4, g_cell - 4, g_c_t128, 1, 0, 6)
        end
        if v == 256
          ui_2d_rect(px + 2, py + 2, g_cell - 4, g_cell - 4, g_c_t256, 1, 0, 6)
        end
        if v == 512
          ui_2d_rect(px + 2, py + 2, g_cell - 4, g_cell - 4, g_c_t512, 1, 0, 6)
        end
        if v >= 1024
          ui_2d_rect(px + 2, py + 2, g_cell - 4, g_cell - 4, g_c_t1024, 1, 0, 6)
        end
        ui_2d_text(px + g_cell / 2, py + (g_cell + g_fs_tile) / 2, v.to_s, g_c_text, g_fs_tile, VML_ANCHOR_CENTER)
      end
      y = y + 1
    end
    x = x + 1
  end

  ui_2d_text(10, 6 + g_fs_score, g_score.to_s, g_c_text, g_fs_score, VML_ANCHOR_LEFT)
  if g_lang == 0
    ui_2d_text(10, 6 + g_fs_score + 2 + g_fs_lbl, "得分", g_c_dim, g_fs_lbl, VML_ANCHOR_LEFT)
  else
    ui_2d_text(10, 6 + g_fs_score + 2 + g_fs_lbl, "score", g_c_dim, g_fs_lbl, VML_ANCHOR_LEFT)
  end

  if g_lost != 0
    if g_lang == 0
      ui_2d_text(g_sw / 2, g_hint_y, "没路可走了,ESC / R 重开", g_c_text, g_fs_score, VML_ANCHOR_CENTER)
    else
      ui_2d_text(g_sw / 2, g_hint_y, "No moves left - ESC / R to restart", g_c_text, g_fs_score, VML_ANCHOR_CENTER)
    end
  else
    if g_lang == 0
      ui_2d_text(g_sw / 2, g_hint_y, "方向键 / 点屏幕四边滑动", g_c_dim, g_fs_hint, VML_ANCHOR_CENTER)
    else
      ui_2d_text(g_sw / 2, g_hint_y, "Arrows / tap a screen edge to slide", g_c_dim, g_fs_hint, VML_ANCHOR_CENTER)
    end
  end
end

def gt_move(d)
  if g_lost != 0
    return 0
  end
  if gt_slide(d) != 0
    gt_spawn()
    if gt_can_move() == 0
      g_lost = 1
    end
  end
  return 0
end

# ══════════════════════════════════════════════════════════════════════════
# 第二段:全局布局与状态(⚠ 必须在所有 def 之后;文件头那条)
# ══════════════════════════════════════════════════════════════════════════

g_c_bg = -15724520        # 0xFF101018
g_c_empty = -15066588     # 0xFF1A1A24
g_c_text = -1513232       # 0xFFE8E8F0
g_c_dim = -6643536        # 0xFF9AA0B0
g_c_t2 = -14013898        # 0xFF2A2A36
g_c_t4 = -12960184        # 0xFF3A3A48
g_c_t8 = -11890471        # 0xFF4A90D9
g_c_t16 = -11409298       # 0xFF51E86E
g_c_t32 = -2509750        # 0xFFD9B44A
g_c_t64 = -2069424        # 0xFFE06C50
g_c_t128 = -2069336       # 0xFFE06CA8
g_c_t256 = -6591248       # 0xFF9B6CF0
g_c_t512 = -11871802      # 0xFF4AD9C6
g_c_t1024 = -4145088      # 0xFFC0C040

g_lang = ui_scr_get_language()
g_sw = 360
g_sh = 620
g_cell = 80
g_ox = 20
g_oy = 120
g_m = 360
g_fs_score = 18
g_fs_lbl = 10
g_fs_hint = 11
g_fs_tile = 26
g_hint_y = 0
g_score = 0
g_won = 0
g_lost = 0
g_seed = 20261
g_frames = 0
g_alive = 1

g_board = []
g_line = []
g_old = []
i = 0
while i < 16
  g_board.push(0)
  i = i + 1
end
i = 0
while i < 4
  g_line.push(0)
  g_old.push(0)
  i = i + 1
end

ui_win_open("2048", 0, 0)
gt_relayout()
gt_new_game()

g_tid = ui_timer_set(50, 1)

while g_alive == 1
  t = ui_msg_fetch_wait(50)
  if t == 10
    g_alive = 0
  end

  if t == 1
    k = ui_msg_get_a()
    if k == 27 || k == 82
      gt_new_game()
    end
    if k == 37
      gt_move(0)
    end
    if k == 39
      gt_move(1)
    end
    if k == 38
      gt_move(2)
    end
    if k == 40
      gt_move(3)
    end
  end

  if t == 6 || t == 4
    tx = ui_msg_get_a()
    ty = ui_msg_get_b()
    dl = tx
    dr = g_sw - tx
    dt = ty
    db = g_sh - ty
    if dl <= dr && dl <= dt && dl <= db
      gt_move(0)
    else
      if dr <= dt && dr <= db
        gt_move(1)
      else
        if dt <= db
          gt_move(2)
        else
          gt_move(3)
        end
      end
    end
  end

  if t == 11
    gt_relayout()
  end

  if t == 9
    g_frames = g_frames + 1
    if g_frames >= 20000
      g_alive = 0
    end
  end

  gt_draw()
  ui_2d_present()
end

ui_timer_kill(g_tid)
ui_win_close()