! 十五格拼图 —— 用 **Fortran** 写的手机小游戏
! Puzzle 15 -- a small phone game written in **Fortran**
!
! 玩法（与 Examples/c/app/puzzle15.c / Examples/python/app/puzzle15.py 同一套的目标）：
!   4×4 共 15 块加一个空格；点空格旁边的格子把它滑进空格；
!   把 1..15 按顺序排好、空格落在右下角即通关。
!
! 本前端没有数字转字符串 ⇒ **块的颜色 = 它应该在的位置**（位置 i 固定颜色），
! 排好时每一格的颜色都等于所在位置的颜色（同色对齐即通关）。
!
! ◆ 写法要求（见 Examples/fortran/app/sokoban.f90 文件头）
!   · `k = ui_2d_rect(...)` 函数调用表达式；`!=` 写 `/=`
!
! 跑法 —— 手机：vml run examples/fortran/puzzle15.f90
! 跑法 —— 桌面：vmlcli Examples/fortran/puzzle15.f90 --screen 480x640 --frames /tmp/fr

program puzzle15
  implicit none
  integer :: board(16)
  integer :: w
  integer :: h
  integer :: cell
  integer :: ox
  integer :: oy
  integer :: blank
  integer :: won
  integer :: moves
  integer :: seed
  integer :: i
  integer :: j
  integer :: k
  integer :: r
  integer :: bx
  integer :: by
  integer :: cx
  integer :: cy
  integer :: n
  integer :: t
  integer :: tid
  integer :: lang
  integer :: key
  integer :: tx
  integer :: ty
  integer :: col
  integer :: gx
  integer :: gy
  integer :: colors(16)

  colors(1) = -2069424
  colors(2) = -2509750
  colors(3) = -11409298
  colors(4) = -11890471
  colors(5) = -6591248
  colors(6) = -2069336
  colors(7) = -11871802
  colors(8) = -4145088
  colors(9) = -11409298
  colors(10) = -11890471
  colors(11) = -2069424
  colors(12) = -2509750
  colors(13) = -6591248
  colors(14) = -2069336
  colors(15) = -11871802
  colors(16) = -4145088

  lang = ui_scr_get_language()
  if (lang == 0) k = ui_win_open('十五格拼图', 0, 0)
  if (lang /= 0) k = ui_win_open('Puzzle 15', 0, 0)

  w = ui_scr_get_w()
  h = ui_scr_get_h()
  if (w <= 0) w = 360
  if (h <= 0) h = 620
  ! 4×4 拼图：窄屏提示折成两行，棋盘从提示下方开始
  oy = 70
  if (w < 420) oy = 80
  cell = (w - 20) / 4
  k = (h - oy - 8) / 4
  if (k < cell) cell = k
  if (cell < 6) cell = 6
  ox = (w - cell * 4) / 2

  ! 解决态 + 随机走步打乱（一定可解）
  do i = 1, 16
    board(i) = i
  end do
  board(16) = 0
  blank = 16
  seed = 20264
  n = 0
  do while (n < 200)
    seed = mod(seed * 1103 + 12345, 32768)
    r = mod(seed / 256, 4)
    bx = mod(blank - 1, 4)
    by = (blank - 1) / 4
    j = blank
    if (r == 0) then
      if (bx > 0) j = blank - 1
    end if
    if (r == 1) then
      if (bx < 3) j = blank + 1
    end if
    if (r == 2) then
      if (by > 0) j = blank - 4
    end if
    if (r == 3) then
      if (by < 3) j = blank + 4
    end if
    if (j /= blank) then
      board(blank) = board(j)
      board(j) = 0
      blank = j
    end if
    n = n + 1
  end do
  won = 0
  moves = 0

  k = ui_scr_set_keep_on(1)
  tid = ui_timer_set(60, 0)

  do while (ui_win_is_closed() == 0)

    ! 判胜：每格色号 == 位置号
    won = 1
    i = 1
    do while (i < 16)
      if (board(i) /= i) won = 0
      i = i + 1
    end do
    if (board(16) /= 0) won = 0

    ! ── draw ──
    ! 布局随画布现算（含 resize）
    oy = 70
    if (w < 420) oy = 80
    cell = (w - 20) / 4
    k = (h - oy - 8) / 4
    if (k < cell) cell = k
    if (cell < 6) cell = 6
    ox = (w - cell * 4) / 2

    k = ui_2d_clear(-15724520)
    i = 0
    do while (i < 16)
      gx = ox + mod(i, 4) * cell
      gy = oy + (i / 4) * cell
      if (board(i + 1) == 0) then
        k = ui_2d_rect(gx + 2, gy + 2, cell - 4, cell - 4, -15066588, 1, 0, 4)
      else
        k = ui_2d_rect(gx + 2, gy + 2, cell - 4, cell - 4, colors(board(i + 1)), 1, 0, 4)
        if (board(i + 1) == i + 1) then
          k = ui_2d_rect(gx + cell / 4, gy + cell / 4, cell / 2, cell / 2, -15724520, 1, 0, 0)
        end if
      end if
      i = i + 1
    end do

    if (won == 1) then
      if (lang == 0) k = ui_2d_text(8, 44, '排好了！点一下重开', -11409298, 15, 0)
      if (lang /= 0) k = ui_2d_text(8, 44, 'Solved! Tap to restart', -11409298, 15, 0)
    else
      if (w < 420) then
        if (lang == 0) k = ui_2d_text(8, 44, '点空格旁的数字 / 方向键滑动', -6643536, 11, 0)
        if (lang /= 0) k = ui_2d_text(8, 44, 'Tap a tile beside the blank / arrows', -6643536, 11, 0)
        if (lang == 0) k = ui_2d_text(8, 60, '同色对齐即通关', -6643536, 11, 0)
        if (lang /= 0) k = ui_2d_text(8, 60, 'Match each tile to its spot', -6643536, 11, 0)
      else
        if (lang == 0) k = ui_2d_text(8, 44, '点空格旁的数字 / 方向键滑动（同色对齐即通关）', -6643536, 11, 0)
        if (lang /= 0) k = ui_2d_text(8, 44, 'Tap a tile beside the blank / arrows', -6643536, 11, 0)
      end if
    end if
    k = ui_2d_present()

    t = ui_msg_fetch_wait(0)
    if (t == 10) exit

    if (t == 1) then
      key = ui_msg_get_a()
      if (key == 27) exit
      bx = mod(blank - 1, 4)
      by = (blank - 1) / 4
      j = blank
      if (key == 37) then
        if (bx < 3) j = blank + 1
      end if
      if (key == 39) then
        if (bx > 0) j = blank - 1
      end if
      if (key == 38) then
        if (by < 3) j = blank + 4
      end if
      if (key == 40) then
        if (by > 0) j = blank - 4
      end if
      if (j /= blank) then
        board(blank) = board(j)
        board(j) = 0
        blank = j
        moves = moves + 1
      end if
    end if

    if (t == 6 .or. t == 4) then
      tx = ui_msg_get_a()
      ty = ui_msg_get_b()
      if (tx >= ox) then
        if (ty >= oy) then
          cx = (tx - ox) / cell
          cy = (ty - oy) / cell
          if (cx < 4) then
            if (cy < 4) then
              j = cy * 4 + cx + 1
              bx = mod(blank - 1, 4)
              by = (blank - 1) / 4
              if (cx == bx .and. (cy == by + 1 .or. cy == by - 1)) then
                board(blank) = board(j)
                board(j) = 0
                blank = j
                moves = moves + 1
              end if
              if (cy == by .and. (cx == bx + 1 .or. cx == bx - 1)) then
                board(blank) = board(j)
                board(j) = 0
                blank = j
                moves = moves + 1
              end if
            end if
          end if
        end if
      end if
    end if

    if (t == 11) then
      w = ui_scr_get_w()
      h = ui_scr_get_h()
      if (w <= 0) w = 360
      if (h <= 0) h = 620
    end if

    if (t == 9) then
      i = i + 1
      if (i >= 20000) exit
    end if
  end do

  k = ui_timer_kill(tid)
  k = ui_scr_set_keep_on(0)
  k = ui_win_close()
end program puzzle15
