#include <vml_ui.r>
# 数字华容道 —— 用 **R** 写的手机小游戏（4×4 滑块）
# Puzzle 15 -- a small phone game written in **R** (4x4 sliding)
#
# 玩法：点空位旁边的数字，它就滑进空位；把 1..15 排回顺序即通关。ESC 重排。
#
# 跑法 —— 手机：vml run examples/r/puzzle15.r
# 跑法 —— 桌面：vmlcli Examples/r/puzzle15.r --screen 480x640 --frames /tmp/fr

c_bg <- -15724520
c_txt <- -1513232
c_dim <- -6643536
c_ok <- -11409298
c_tile <- -14013902

e <- 16

k <- ui_win_open("数字华容道", 0, 0)
k <- ui_scr_set_keep_on(1)
w <- ui_scr_get_w()
h <- ui_scr_get_h()
if (w <= 0) { w <- 360 }
if (h <= 0) { h <- 620 }

# 布局按画布现算：字号随 min(W,H) 缩放，棋盘吃掉中间剩余高度。
mn <- w
if (h < mn) { mn <- h }
scn <- as.integer(mn * 100 / 360)
if (scn < 55) { scn <- 55 }
if (scn > 240) { scn <- 240 }
pad <- as.integer(mn / 20)
if (pad < 6) { pad <- 6 }
if (pad > 24) { pad <- 24 }
f_stat <- as.integer(22 * scn / 100)
if (f_stat < 8) { f_stat <- 8 }
f_small <- as.integer(12 * scn / 100)
if (f_small < 8) { f_small <- 8 }
f_hint <- f_small
f_tile <- as.integer(20 * scn / 100)
if (f_tile < 8) { f_tile <- 8 }
stat_y <- pad + as.integer(f_stat / 2)
lab_y <- pad + f_stat + as.integer(f_small / 2)
msg_y <- pad + f_stat + f_small + as.integer(f_small / 2) + as.integer(pad / 2)
gtop <- pad + f_stat + f_small + f_small + pad
avail <- h - gtop - f_hint - pad
cellw <- as.integer((w - 2 * pad) / 4)
ch2 <- as.integer(avail / 4)
if (ch2 < cellw) { cellw <- ch2 }
if (cellw < 16) { cellw <- 16 }
ox <- as.integer((w - cellw * 4) / 2)
oy <- gtop
gap <- as.integer(cellw / 8)
if (gap < 3) { gap <- 3 }
hinty <- h - pad - as.integer(f_hint / 2)
moves <- 0
won <- 0
frames <- 0

tile <- rep(0, 16)
i <- 1
while (i <= 15) { tile[i] <- i; i <- i + 1 }
tile[16] <- 0
seed <- 1234
i <- 0
while (i < 400) {
    seed <- (seed + 7919) %% 32768
    a <- as.integer(seed / 2048) + 1
    seed <- (seed + 7919) %% 32768
    b <- as.integer(seed / 2048) + 1
    tt <- tile[a]
    tile[a] <- tile[b]
    tile[b] <- tt
    i <- i + 1
}

tid <- ui_timer_set(200, 0)

while (frames < 30000) {
    k <- ui_2d_clear(c_bg)
    idx <- 1
    while (idx <= 16) {
        r <- as.integer((idx - 1) / 4)
        c <- idx - 1 - r * 4
        tx <- ox + c * cellw
        ty <- oy + r * cellw
        if (tile[idx] == 0) {
            k <- ui_2d_rect(tx, ty, cellw - gap, cellw - gap, c_bg, 1, 0, 4)
        } else {
            k <- ui_2d_rect(tx, ty, cellw - gap, cellw - gap, c_tile, 1, 0, 4)
            nd <- 1
            if (tile[idx] >= 10) { nd <- 2 }
            dg <- as.integer(f_tile * 6 / 10)
            if (dg < 4) { dg <- 4 }
            ttx <- tx + as.integer((cellw - gap - dg * nd) / 2)
            tty <- ty + as.integer((cellw - gap) / 2)
            k <- ui_2d_text(ttx, tty, sprintf("%d", tile[idx]), c_txt, f_tile, 0)
        }
        idx <- idx + 1
    }
    k <- ui_2d_text(ox, stat_y, sprintf("%d", moves), c_txt, f_stat, 0)
    k <- ui_2d_text(ox, lab_y, "步数", c_dim, f_small, 0)
    if (won == 1) {
        k <- ui_2d_text(ox, msg_y, "排好了！ESC 再来", c_ok, f_small, 0)
    } else {
        k <- ui_2d_text(ox, msg_y, "点空位旁边的数字", c_dim, f_small, 0)
    }
    k <- ui_2d_text(pad, hinty, "ESC 重排", c_dim, f_hint, 0)
    k <- ui_2d_present()

    msg <- ui_msg_fetch_wait(0)
    if (msg == 10) { frames <- 30000 }
    if (msg == 6) {
        ax <- ui_msg_get_a()
        ay <- ui_msg_get_b()
        if (ax >= ox) {
            if (ay >= oy) {
                c <- as.integer((ax - ox) / cellw)
                r <- as.integer((ay - oy) / cellw)
                if (c < 4) {
                    if (r < 4) {
                        if (won == 0) {
                            idx <- r * 4 + c + 1
                            doit <- 0
                            if (idx - 4 == e) { doit <- 1 }
                            if (idx + 4 == e) { doit <- 1 }
                            if (idx - 1 == e) { if (c > 0) { doit <- 1 } }
                            if (idx + 1 == e) { if (c < 3) { doit <- 1 } }
                            if (doit == 1) {
                                tt <- tile[idx]
                                tile[idx] <- tile[e]
                                tile[e] <- tt
                                e <- idx
                                moves <- moves + 1
                                won <- 1
                                j <- 1
                                while (j <= 15) {
                                    if (tile[j] != j) { won <- 0 }
                                    j <- j + 1
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (msg == 1) {
        key <- ui_msg_get_a()
        if (key == 27) {
            i <- 1
            while (i <= 15) { tile[i] <- i; i <- i + 1 }
            tile[16] <- 0
            seed <- 1234 + frames
            i <- 0
            while (i < 400) {
                seed <- (seed + 7919) %% 32768
                a <- as.integer(seed / 2048) + 1
                seed <- (seed + 7919) %% 32768
                b <- as.integer(seed / 2048) + 1
                tt <- tile[a]
                tile[a] <- tile[b]
                tile[b] <- tt
                i <- i + 1
            }
            moves <- 0
            won <- 0
            e <- 16
        }
    }
    if (msg == 9) { frames <- frames + 1 }
}

k <- ui_timer_kill(tid)
k <- ui_scr_set_keep_on(0)
k <- ui_win_close()
