d/app/puzzle15.d · 6773 字节 · 下载原文
// 数字华容道 —— 用 **D** 写的手机小游戏(4×4 滑块)
// Puzzle 15 -- a small phone game written in **D** (4x4 sliding)
//
// 玩法:点空位旁边的数字,它就滑进空位;把 1..15 排回顺序即通关。ESC 重排。
//
// 跑法 —— 手机:vml run examples/d/puzzle15.d
// 跑法 —— 桌面:vmlcli Examples/d/puzzle15.d --screen 480x640 --frames /tmp/fr
#include <vml_ui.d>
char[] digit(int d) {
if (d == 0) { return "0"; }
if (d == 1) { return "1"; }
if (d == 2) { return "2"; }
if (d == 3) { return "3"; }
if (d == 4) { return "4"; }
if (d == 5) { return "5"; }
if (d == 6) { return "6"; }
if (d == 7) { return "7"; }
if (d == 8) { return "8"; }
return "9";
}
void drawNum(int x, int y, int v, int col, int sz) {
int p1000 = v / 1000;
int p100 = (v / 100) % 10;
int p10 = (v / 10) % 10;
int p1 = v % 10;
int cx = x;
if (p1000 > 0) {
ui_2d_text(cx, y, digit(p1000), col, sz, 0);
cx = cx + sz / 2;
ui_2d_text(cx, y, digit(p100), col, sz, 0);
cx = cx + sz / 2;
} else if (p100 > 0) {
ui_2d_text(cx, y, digit(p100), col, sz, 0);
cx = cx + sz / 2;
}
if (p1000 > 0 || p100 > 0 || p10 > 0) {
ui_2d_text(cx, y, digit(p10), col, sz, 0);
cx = cx + sz / 2;
}
ui_2d_text(cx, y, digit(p1), col, sz, 0);
}
void main() {
int k = ui_win_open("数字华容道", 0, 0);
k = ui_scr_set_keep_on(1);
int w = ui_scr_get_w();
int h = ui_scr_get_h();
if (w <= 0) { w = 360; }
if (h <= 0) { h = 620; }
int small = w;
if (h < small) { small = h; }
// 版面按画布现算:顶部步数 + 中间 4×4 棋盘 + 底部提示。
int stat_fs = small / 16;
if (stat_fs < 16) { stat_fs = 16; }
if (stat_fs > 40) { stat_fs = 40; }
int label_fs = small / 30;
if (label_fs < 9) { label_fs = 9; }
if (label_fs > 16) { label_fs = 16; }
int hint_fs = small / 26;
if (hint_fs < 9) { hint_fs = 9; }
if (hint_fs > 16) { hint_fs = 16; }
int stat_y = stat_fs + 4;
int label_y = stat_y + label_fs + 4;
int hint_y = label_y + hint_fs + 8;
int top = hint_y + 8;
int pad = w / 40;
if (pad < 8) { pad = 8; }
int bottom_y = h - 8;
int bottom_top = h - (hint_fs + 12);
int avail_h = bottom_top - top;
if (avail_h < 24) { avail_h = 24; }
int cellw = w - 2 * pad;
if (avail_h < cellw) { cellw = avail_h; }
cellw = cellw / 4;
if (cellw < 6) { cellw = 6; }
int ox = (w - cellw * 4) / 2;
int oy = top + (avail_h - cellw * 4) / 2;
if (oy < top) { oy = top; }
int gap = cellw / 8;
if (gap < 3) { gap = 3; }
if (gap > 12) { gap = 12; }
int moves = 0;
int won = 0;
int frames = 0;
int[16] tile;
int i = 0;
while (i < 15) { tile[i] = i + 1; i = i + 1; }
tile[15] = 0;
int seed = 1234;
i = 0;
while (i < 400) {
seed = seed + 7919;
if (seed > 32767) { seed = seed - 32768; }
int a = seed / 2048;
seed = seed + 7919;
if (seed > 32767) { seed = seed - 32768; }
int b = seed / 2048;
int tt = tile[a];
tile[a] = tile[b];
tile[b] = tt;
i = i + 1;
}
int e = 15;
int tid = ui_timer_set(200, 0);
while (frames < 30000) {
k = ui_2d_clear(-15724520);
int idx = 0;
while (idx < 16) {
int r = idx / 4;
int c = idx - r * 4;
int tx = ox + c * cellw + gap / 2;
int ty = oy + r * cellw + gap / 2;
if (tile[idx] == 0) {
k = ui_2d_rect(tx, ty, cellw - gap, cellw - gap, -15724520, 1, 0, 4);
} else {
k = ui_2d_rect(tx, ty, cellw - gap, cellw - gap, -14013902, 1, 0, 4);
int nf = cellw / 2;
if (nf < 9) { nf = 9; }
int nd = 1;
if (tile[idx] >= 10) { nd = 2; }
drawNum(tx + cellw / 2 - nd * nf / 4, ty + cellw / 2 + nf * 35 / 100, tile[idx], -1513232, nf);
}
idx = idx + 1;
}
drawNum(pad, stat_y, moves, -1513232, stat_fs);
k = ui_2d_text(pad, label_y, "步数", -6643536, label_fs, 0);
if (won == 1) {
k = ui_2d_text(w / 2, hint_y, "排好了!ESC 再来", -10044566, hint_fs, 1);
} else {
k = ui_2d_text(w / 2, hint_y, "点空位旁边的数字", -6643536, hint_fs, 1);
}
k = ui_2d_text(w / 2, bottom_y, "ESC 重排", -6643536, hint_fs, 1);
k = ui_2d_present();
int msg = ui_msg_fetch_wait(0);
if (msg == 10) { frames = 30000; }
if (msg == 6) {
int ax = ui_msg_get_a();
int ay = ui_msg_get_b();
if (ax >= ox && ay >= oy) {
int c = (ax - ox) / cellw;
int r = (ay - oy) / cellw;
if (c < 4 && r < 4 && won == 0) {
int idx2 = r * 4 + c;
int doit = 0;
if (idx2 - 4 == e) { doit = 1; }
if (idx2 + 4 == e) { doit = 1; }
if (idx2 - 1 == e && c > 0) { doit = 1; }
if (idx2 + 1 == e && c < 3) { doit = 1; }
if (doit == 1) {
int tt2 = tile[idx2];
tile[idx2] = tile[e];
tile[e] = tt2;
e = idx2;
moves = moves + 1;
won = 1;
int j = 0;
while (j < 15) {
if (tile[j] != j + 1) { won = 0; }
j = j + 1;
}
}
}
}
}
if (msg == 1) {
int key = ui_msg_get_a();
if (key == 27) {
i = 0;
while (i < 15) { tile[i] = i + 1; i = i + 1; }
tile[15] = 0;
seed = 1234 + frames;
i = 0;
while (i < 400) {
seed = seed + 7919;
if (seed > 32767) { seed = seed - 32768; }
int a2 = seed / 2048;
seed = seed + 7919;
if (seed > 32767) { seed = seed - 32768; }
int b2 = seed / 2048;
int tt3 = tile[a2];
tile[a2] = tile[b2];
tile[b2] = tt3;
i = i + 1;
}
moves = 0; won = 0; e = 15;
}
}
if (msg == 9) { frames = frames + 1; }
}
k = ui_timer_kill(tid);
k = ui_scr_set_keep_on(0);
k = ui_win_close();
}