rust/app/puzzle15.rs · 7539 字节 · 下载原文
// 十五格拼图(Puzzle 15)—— 用 **Rust** 写的手机小游戏
// Puzzle 15 — a small phone game written in **Rust**
//
// 玩法(与 `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.
//
// ## Rust 这一路的写法约束(见 `Examples/rust/app/snake.rs` 文件头)
// ## Style constraints on this Rust path (see the header of `Examples/rust/app/snake.rs`)
//
// · **全部状态放 `main` 的局部变量**,循环一律 `while`;**循环变量在 `main` 顶部声明一次**;
// · **all state lives in locals of `main`**, loops are always `while`; **loop variables are
// declared once at the top of `main`**;
// · **全程序只养一条 Vec** ⇒ 棋盘 16 格就在这一条里;
// · **exactly one Vec per program** => the 16-cell board lives in it;
// · 数字转字符串走库里的 `int_to_str`(共享静态缓冲,取一次立刻用掉);颜色写负数十进制。
// · numbers go through the library's `int_to_str` (one shared static buffer; take and use it at once);
// colours are negative decimals.
//
// 跑法 —— 手机:`vml run examples/rust/puzzle15.rs`
// How to run — on the phone: `vml run examples/rust/puzzle15.rs`
// 跑法 —— 桌面:`vmlcli Examples/rust/puzzle15.rs --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/rust/puzzle15.rs --screen 480x640 --frames /tmp/fr`
#include <vml_ui.rs>
use vml_ui::*;
fn main() {
let mut lang = ui_scr_get_language();
let mut sw = 360;
let mut sh = 620;
let mut cell = 80;
let mut ox = 20;
let mut oy = 110;
let mut seed = 20264;
let mut moves = 0;
let mut blank = 15;
let mut won = 0;
let mut frames = 0;
let mut i = 0;
let mut j = 0;
let mut n = 0;
let mut t = 0;
let mut k = 0;
let mut bx = 0;
let mut by = 0;
let mut tx = 0;
let mut ty = 0;
let mut cx = 0;
let mut cy = 0;
let mut done = 0;
let mut tid = 0;
let c_bg = -15724520; // 0xFF101018
let c_tile = -14013898; // 0xFF2A2A36
let c_tile2 = -12960184; // 0xFF3A3A48
let c_text = -1513232; // 0xFFE8E8F0
let c_dim = -6643536; // 0xFF9AA0B0
let c_win = -11409298; // 0xFF51E86E
// 一条 Vec:0..15 棋盘(0 = 空格)
// One Vec: 0..15 board (0 = blank)
let mut b: Vec<i32> = vec![0];
i = 0;
while i < 16 {
b.push(0);
i = i + 1;
}
if lang == 0 { ui_win_open("十五格拼图", 0, 0); }
else { ui_win_open("Puzzle 15", 0, 0); }
ui_scr_set_keep_on(1);
sw = ui_scr_get_w();
sh = ui_scr_get_h();
if sw <= 0 { sw = 360; }
if sh <= 0 { sh = 620; }
let mut side = sw - 20;
if sw >= sh - 140 { side = sh - 170; }
cell = side / 4;
if cell < 6 { cell = 6; }
ox = (sw - 4 * cell) / 2;
oy = 110;
// 解决态
// Solved state
i = 0;
while i < 16 {
b[i] = i + 1;
i = i + 1;
}
b[15] = 0;
blank = 15;
// 随机走 200 步打乱(一定可解)
// Shuffle with 200 random legal moves (always solvable)
n = 0;
while n < 200 {
seed = (seed * 1103 + 12345) % 32768;
let r = (seed / 256) % 4;
bx = blank % 4;
by = blank / 4;
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; }
else { j = blank; }
if j != blank {
b[blank] = b[j];
b[j] = 0;
blank = j;
}
n = n + 1;
}
moves = 0;
tid = ui_timer_set(60, 0);
while done == 0 {
if ui_win_is_closed() != 0 { done = 1; }
if done != 0 { break; }
t = ui_msg_fetch_wait(0);
if t == 10 { done = 1; }
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;
}
}
}
}
// 判胜
// Win check
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(c_bg);
i = 0;
while i < 16 {
let px = ox + (i % 4) * cell;
let py = oy + (i / 4) * cell;
if b[i] == 0 {
ui_2d_rect(px + 2, py + 2, cell - 4, cell - 4, -15066588, 1, 0, 4);
} else {
if i == b[i] - 1 { ui_2d_rect(px + 2, py + 2, cell - 4, cell - 4, c_tile2, 1, 0, 4); }
else { ui_2d_rect(px + 2, py + 2, cell - 4, cell - 4, c_tile, 1, 0, 4); }
ui_2d_text(px + cell / 2, py + cell / 2 + cell / 8, int_to_str(b[i]), c_text, cell / 2, 1);
}
i = i + 1;
}
ui_2d_text(10, 24, int_to_str(moves), c_text, 15, 0);
if lang == 0 { ui_2d_text(10, 46, "步数", c_dim, 10, 0); }
else { ui_2d_text(10, 46, "moves", c_dim, 10, 0); }
if won == 1 {
if lang == 0 { ui_2d_text(sw / 2, oy + 4 * cell + 28, "排好了!", c_win, 16, 1); }
else { ui_2d_text(sw / 2, oy + 4 * cell + 28, "Solved!", c_win, 16, 1); }
} else {
if lang == 0 { ui_2d_text(sw / 2, oy + 4 * cell + 28, "点空格旁的数字 / 方向键滑动", c_dim, 11, 1); }
else { ui_2d_text(sw / 2, oy + 4 * cell + 28, "Tap a tile beside the blank / arrows", c_dim, 11, 1); }
}
ui_2d_present();
frames = frames + 1;
if frames >= 20000 { done = 1; }
}
ui_timer_kill(tid);
ui_scr_set_keep_on(0);
ui_win_close();
}