! 迷宫 —— 用 **Fortran** 写的手机小游戏（探索）
! Maze -- a small phone game written in **Fortran** (exploration)
!
! 玩法（与 Examples/c/app/maze.c / Examples/python/app/maze.py 同一套的目标）：
!   方向键 / 点屏幕四边移动，走到右下角红格即通关；ESC / 通关后点一下重开。
!
! 与 DFS 版不同：这一份用**公式化蛇形迷宫**（偶数行整行通道、奇数行交替在左右端开口），
! 一定连通、一定有路，也不需要 441 格的表 —— Fortran 的数组下标是 1 基，
! 用公式算「某格是不是墙」比维护一张大表更短。
!
! ◆ 写法要求（见 Examples/fortran/app/sokoban.f90 文件头）
!   · `k = ui_2d_rect(...)` 函数调用表达式；`!=` 写 `/=`
!
! 跑法 —— 手机：vml run examples/fortran/maze.f90
! 跑法 —— 桌面：vmlcli Examples/fortran/maze.f90 --screen 480x640 --frames /tmp/fr

program maze
  implicit none
  integer :: w
  integer :: h
  integer :: cell
  integer :: ox
  integer :: oy
  integer :: px
  integer :: py
  integer :: won
  integer :: steps
  integer :: x
  integer :: y
  integer :: k
  integer :: t
  integer :: tid
  integer :: lang
  integer :: key
  integer :: tx
  integer :: ty
  integer :: dl
  integer :: dr
  integer :: dt
  integer :: db
  integer :: nx
  integer :: ny
  integer :: band
  integer :: wall
  integer :: side
  integer :: barw
  integer :: fs

  lang = ui_scr_get_language()
  if (lang == 0) k = ui_win_open('迷宫', 0, 0)
  if (lang /= 0) k = ui_win_open('Maze', 0, 0)

  w = ui_scr_get_w()
  h = ui_scr_get_h()
  if (w <= 0) w = 360
  if (h <= 0) h = 620
  ! 21×21 迷宫：提示下方可用区域取正方形（小屏也尽量占满）
  oy = 68
  if (h > 620) oy = h / 8
  side = w - 16
  k = h - oy - 8
  if (k < side) side = k
  cell = side / 21
  if (cell < 3) cell = 3
  ox = (w - 21 * cell) / 2

  px = 1
  py = 0
  won = 0
  steps = 0

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

  do while (ui_win_is_closed() == 0)

    ! ── draw ──
    ! 布局随画布现算（含 resize）
    oy = 68
    if (h > 620) oy = h / 8
    side = w - 16
    k = h - oy - 8
    if (k < side) side = k
    cell = side / 21
    if (cell < 3) cell = 3
    ox = (w - 21 * cell) / 2

    k = ui_2d_clear(-15724520)
    x = 0
    do while (x < 21)
      y = 0
      do while (y < 21)
        ! 公式：偶数行整行通道；奇数行的开口在左右两端交替
        wall = 1
        if (mod(y, 2) == 0) then
          wall = 0
        else
          band = y / 2
          if (mod(band, 2) == 0) then
            if (x == 20) wall = 0
          else
            if (x == 0) wall = 0
          end if
        end if
        if (wall == 1) then
          k = ui_2d_rect(ox + x * cell, oy + y * cell, cell, cell, -14013898, 1, 0, 0)
        else
          k = ui_2d_rect(ox + x * cell, oy + y * cell, cell, cell, -15066588, 1, 0, 0)
        end if
        y = y + 1
      end do
      x = x + 1
    end do
    k = ui_2d_rect(ox + 19 * cell + 1, oy + 20 * cell + 1, cell - 2, cell - 2, -2069424, 1, 0, 2)
    k = ui_2d_rect(ox + px * cell + 1, oy + py * cell + 1, cell - 2, cell - 2, -11409298, 1, 0, 2)

    ! 步数用一条进度条（每步 3 像素；宽度封顶到画布内）
    if (steps > 0) then
      barw = steps * 3
      if (barw > w - 20) barw = w - 20
      k = ui_2d_rect(10, 14, barw, 8, -11890471, 1, 0, 2)
    end if

    fs = 11
    if (w < 360) fs = 10
    if (won == 1) then
      if (lang == 0) k = ui_2d_text(8, 44, '走出来了！点一下 / ESC 换一张', -11409298, 15, 0)
      if (lang /= 0) k = ui_2d_text(8, 44, 'Out! Tap / ESC for a new maze', -11409298, 15, 0)
    else
      if (lang == 0) k = ui_2d_text(8, 44, '方向键 / 点屏幕四边，走到红格', -6643536, fs, 0)
      if (lang /= 0) k = ui_2d_text(8, 44, 'Arrows / tap edges; reach the red cell', -6643536, fs, 0)
    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) then
        px = 1
        py = 0
        won = 0
        steps = 0
      end if
      nx = px
      ny = py
      if (key == 37) nx = px - 1
      if (key == 39) nx = px + 1
      if (key == 38) ny = py - 1
      if (key == 40) ny = py + 1
      if (won == 0 .and. (nx /= px .or. ny /= py)) then
        wall = 1
        if (nx >= 0 .and. ny >= 0 .and. nx < 21 .and. ny < 21) then
          if (mod(ny, 2) == 0) then
            wall = 0
          else
            band = ny / 2
            if (mod(band, 2) == 0) then
              if (nx == 20) wall = 0
            else
              if (nx == 0) wall = 0
            end if
          end if
        end if
        if (wall == 0) then
          px = nx
          py = ny
          steps = steps + 1
          if (px == 19 .and. py == 20) won = 1
        end if
      end if
    end if

    if (t == 6 .or. t == 4) then
      tx = ui_msg_get_a()
      ty = ui_msg_get_b()
      if (won == 1) then
        px = 1
        py = 0
        won = 0
        steps = 0
      else
        dl = tx
        dr = w - tx
        dt = ty
        db = h - ty
        nx = px
        ny = py
        if (dl <= dr .and. dl <= dt .and. dl <= db) nx = px - 1
        if (dl > dr .and. dr <= dt .and. dr <= db) nx = px + 1
        if (dl > dr .and. dr > dt .and. dt <= db) ny = py - 1
        if (dl > dr .and. dr > dt .and. dt > db) ny = py + 1
        wall = 1
        if (nx >= 0 .and. ny >= 0 .and. nx < 21 .and. ny < 21) then
          if (mod(ny, 2) == 0) then
            wall = 0
          else
            band = ny / 2
            if (mod(band, 2) == 0) then
              if (nx == 20) wall = 0
            else
              if (nx == 0) wall = 0
            end if
          end if
        end if
        if (wall == 0) then
          px = nx
          py = ny
          steps = steps + 1
          if (px == 19 .and. py == 20) won = 1
        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
  end do

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