ruby/app/memory.rb · 12147 字节 · 下载原文
# 记忆翻牌 —— 用 **Ruby** 写的手机小游戏
# Memory matching — a small phone game written in **Ruby**
#
# 玩法(与 Examples/c/app/memory.c / Examples/python/app/memory.py 同一套):
# Gameplay (same rules as the C / Python versions):
# · 4×4 共 8 对;点一张翻开,再点第二张;
# · 4x4, eight pairs; tap one card to flip it, then tap a second;
# · 一样就留在明面,不一样 **0.4 秒**后扣回去;全部配对完成即通关;
# · an equal pair stays revealed, a different pair flips back after **0.4 s**; clear all pairs to win;
# · 通关后点屏幕(或按 ESC / R)重开。
# · after a win, tap the screen (or press ESC / R) for a new game.
#
# ## 这一份演示「延时动作」怎么写
# ## This sample shows **how to write a delayed action**
#
# 翻错的两张不能立刻扣回去(玩家会看不到第二张是什么),所以记一个**倒计时拍数**,
# 主循环每拍减一,减到 0 才扣 —— 不另起线程、不睡眠。
# A wrong pair must not flip back immediately (the player would never see the second card), so we
# keep a countdown in **ticks** and decrement it each frame -- no extra thread, no sleep.
#
# ## 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** (the other order
# makes the function read garbage);
# · 状态一律 g_ 前缀;数组用 [] + push 填充;
# · state is g_-prefixed; arrays are built with [] + push;
# · 数字转字符串用 .to_s,拼接用 +;
# · numbers to strings use .to_s; concatenation uses +;
# · 颜色写负数十进制。
# · colours are negative decimals.
#
# 跑法 —— 手机:vml run examples/ruby/memory.rb
# How to run — on the phone: vml run examples/ruby/memory.rb
# 跑法 —— 桌面:vmlcli Examples/ruby/memory.rb --screen 480x640 --frames /tmp/fr
# How to run — on the desktop: vmlcli Examples/ruby/memory.rb --screen 480x640 --frames /tmp/fr
# UI 库的显式引用(用户 2026-10-01 定的口径),声明文件 Lib/ruby/vml_ui.rb 由 GenLib 生成。
# The explicit reference to the UI library (user convention of 2026-10-01); Lib/ruby/vml_ui.rb is generated by GenLib.
require 'vml_ui'
# ══════════════════════════════════════════════════════════════════════════
# 第一段:全部函数(⚠ 必须排在"给全局赋值"之前,理由见文件头那条)
# Section 1: all the functions (⚠ must come before any assignment to a global; see the header)
# ══════════════════════════════════════════════════════════════════════════
# ⚠ 编译期保险(见 minesweeper.rb 同名函数):让各状态的全局槽在任何函数生成之前就存在。
# ⚠ 实测(2026-11-14):缺这个函数时 `mem_relayout` 写出的 g_cell / g_ox 落进了它自己的局部,
# `mem_tap` 读到的仍是初始值(80 / 20)⇒ 点击坐标全被判「越界」,一张牌都翻不开。
# ⚠ Compile-time safeguard (see the same function in minesweeper.rb): make every state slot exist
# before any function is generated. Measured on 2026-11-14: without it, the g_cell / g_ox written by
# `mem_relayout` landed in that function's own local and `mem_tap` still saw the initial 80 / 20,
# so every tap was judged out of bounds and no card could ever flip.
def mem_touch_state()
return g_sw + g_sh + g_cell + g_ox + g_oy + g_side + g_first_x + g_first_y + g_hide + g_moves + g_matched + g_won + g_seed + g_frames + g_alive + g_lang + g_pair.length + g_up.length + g_m + g_fs_score + g_fs_lbl + g_fs_hint + g_fs_face + g_hint_y
end
# 确定性伪随机(验收要能复现同一局)
# Deterministic PRNG (acceptance needs to reproduce the same run)
def mem_rnd(n)
g_seed = (g_seed * 1103 + 12345) % 32768
return (g_seed / 256) % n
end
# 一对牌的颜色 —— 8 种固定色,按配对号取
# The colour of a pair — eight fixed colours indexed by pair number
def mem_pair_color(pair_id)
if pair_id == 0
return g_c_p0
end
if pair_id == 1
return g_c_p1
end
if pair_id == 2
return g_c_p2
end
if pair_id == 3
return g_c_p3
end
if pair_id == 4
return g_c_p4
end
if pair_id == 5
return g_c_p5
end
if pair_id == 6
return g_c_p6
end
return g_c_p7
end
def mem_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
g_side = g_sw - 20
avail = g_sh - g_oy - g_fs_hint - 22
if avail < g_side
g_side = avail
end
g_cell = g_side / 4
if g_cell < 6
g_cell = 6
end
g_ox = (g_sw - 4 * g_cell) / 2
g_fs_face = g_cell / 2
if g_fs_face < 10
g_fs_face = 10
end
if g_fs_face > 48
g_fs_face = 48
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 mem_new_game()
g_seed = 20265
i = 0
while i < 16
# 与其它语言版同一初始排列(手算成字面量)
# Same initial layout as the other language versions (computed by hand)
g_pair[0] = 0
g_pair[1] = 2
g_pair[2] = 4
g_pair[3] = 6
g_pair[4] = 0
g_pair[5] = 2
g_pair[6] = 4
g_pair[7] = 6
g_pair[8] = 1
g_pair[9] = 3
g_pair[10] = 5
g_pair[11] = 7
g_pair[12] = 1
g_pair[13] = 3
g_pair[14] = 5
g_pair[15] = 7
g_up[i] = 0
i = i + 1
end
i = 15
while i > 0
j = mem_rnd(i + 1)
t = g_pair[i]
g_pair[i] = g_pair[j]
g_pair[j] = t
i = i - 1
end
g_first_x = -1
g_first_y = -1
g_hide = 0
g_moves = 0
g_matched = 0
g_won = 0
end
def mem_draw()
ui_2d_clear(g_c_bg)
x = 0
while x < 4
y = 0
while y < 4
px = g_ox + x * g_cell
py = g_oy + y * g_cell
pair_id = g_pair[x * 4 + y]
if pair_id < 0
# 已配对:淡淡地留个印子
ui_2d_rect(px + 3, py + 3, g_cell - 6, g_cell - 6, g_c_matched, 1, 0, 6)
else
if g_up[x * 4 + y] == 1
ui_2d_rect(px + 3, py + 3, g_cell - 6, g_cell - 6, mem_pair_color(pair_id), 1, 0, 8)
# 牌面用数字(本前端 .to_s 可用)
face = pair_id + 1
ui_2d_text(px + g_cell / 2, py + (g_cell + g_fs_face) / 2, face.to_s, g_c_face, g_fs_face, VML_ANCHOR_CENTER)
else
ui_2d_rect(px + 3, py + 3, g_cell - 6, g_cell - 6, g_c_back, 1, 0, 8)
end
end
y = y + 1
end
x = x + 1
end
ui_2d_text(10, 6 + g_fs_score, g_moves.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, "moves", g_c_dim, g_fs_lbl, VML_ANCHOR_LEFT)
end
if g_won == 1
if g_lang == 0
ui_2d_text(g_sw / 2, g_hint_y, "全配好了!点一下再来", g_c_text, g_fs_score, VML_ANCHOR_CENTER)
else
ui_2d_text(g_sw / 2, g_hint_y, "All matched! Tap for a new one", 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, "Flip two that match", g_c_dim, g_fs_hint, VML_ANCHOR_CENTER)
end
end
end
def mem_tap(tx, ty)
# ⚠ "通关后点一下重开" 的判断放在主循环里 —— 本前端在**函数内的 if 块里调用另一个函数**
# 会让解析器把配对的 end 当多余(实测 End 错误);顶层 if 里调用则正常(见 minesweeper)。
# ⚠ The "restart on tap after winning" check lives in the main loop -- on this frontend a function
# call inside an `if` block **within a function** makes the parser treat the matching `end` as
# extra (measured); calls inside a top-level `if` are fine (see minesweeper).
x = (tx - g_ox) / g_cell
y = (ty - g_oy) / g_cell
if x < 0 || y < 0 || x >= 4 || y >= 4
return 0
end
i = x * 4 + y
if g_pair[i] < 0 || g_up[i] == 1 || g_hide > 0
return 0
end
g_up[i] = 1
if g_first_x < 0
g_first_x = x
g_first_y = y
return 0
end
g_moves = g_moves + 1
fi = g_first_x * 4 + g_first_y
if g_pair[fi] == g_pair[i]
g_pair[fi] = -1
g_pair[i] = -1
g_matched = g_matched + 2
if g_matched >= 16
g_won = 1
end
g_first_x = -1
else
# 记个倒计时,主循环到点扣回
g_hide = 8
end
end
# ══════════════════════════════════════════════════════════════════════════
# 第二段:全局布局与状态(⚠ 必须在所有 def 之后;文件头那条)
# Section 2: layout and state (⚠ must come after every def; see the header)
# ══════════════════════════════════════════════════════════════════════════
g_c_bg = -15724520 # 0xFF101018
g_c_back = -14013898 # 0xFF2A2A36
g_c_matched = -15329762 # 0xFF16161E
g_c_text = -1513232 # 0xFFE8E8F0
g_c_dim = -6643536 # 0xFF9AA0B0
g_c_face = -15724520 # 0xFF101018
g_c_p0 = -2069424 # 0xFFE06C50
g_c_p1 = -2509750 # 0xFFD9B44A
g_c_p2 = -11409298 # 0xFF51E86E
g_c_p3 = -11890471 # 0xFF4A90D9
g_c_p4 = -6591248 # 0xFF9B6CF0
g_c_p5 = -2069336 # 0xFFE06CA8
g_c_p6 = -11871802 # 0xFF4AD9C6
g_c_p7 = -4145088 # 0xFFC0C040
g_lang = ui_scr_get_language()
g_sw = 360
g_sh = 620
g_cell = 80
g_ox = 20
g_oy = 110
g_side = 0
g_m = 360
g_fs_score = 15
g_fs_lbl = 10
g_fs_hint = 12
g_fs_face = 40
g_hint_y = 0
g_pair = []
g_up = []
i = 0
while i < 16
g_pair.push(0)
g_up.push(0)
i = i + 1
end
g_first_x = -1
g_first_y = -1
g_hide = 0
g_moves = 0
g_matched = 0
g_won = 0
g_seed = 20265
g_frames = 0
g_alive = 1
ui_win_open("记忆翻牌", 0, 0)
mem_relayout()
mem_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 == 6 || t == 4
if g_won == 1
mem_new_game()
else
mem_tap(ui_msg_get_a(), ui_msg_get_b())
end
end
if t == 1
k = ui_msg_get_a()
if k == 27 || k == 82
mem_new_game()
end
end
if t == 11
mem_relayout()
end
if t == 9
g_frames = g_frames + 1
if g_frames >= 8000
g_alive = 0
end
if g_hide > 0
g_hide = g_hide - 1
if g_hide == 0
x = 0
while x < 4
y = 0
while y < 4
g_up[x * 4 + y] = 0
y = y + 1
end
x = x + 1
end
g_first_x = -1
end
end
end
mem_draw()
ui_2d_present()
end
ui_timer_kill(g_tid)
ui_win_close()