! 打地鼠 —— 用 **Fortran** 写的手机小游戏
! Whack-a-mole -- a small phone game written in **Fortran**
!
! 玩法（与 Examples/c/app/whack.c / Examples/python/app/whack.py 同一套）：
!   3×3 九个洞，每 14 拍有一个洞冒出地鼠；在它缩回去之前点中 +1 分，
!   超时没点中扣一条命；三条命用完结束；结束后点屏幕重开。
!
! 地鼠的存活时间记在**拍数**上，不是 time() —— 定时器就是心跳。
! 分数与命数**不画数字**（本前端没有数字转字符串路径）：得分用底部一条**能量条**，
! 命数用右上角**三个小方块**（掉一条熄灭一个）。
!
! ◆ 写法要求（见 Examples/fortran/app/sokoban.f90 文件头）
!   · `k = ui_2d_rect(...)` 函数调用表达式；数组 1 基；`!=` 写 `/=`
!   · 没有三元/merge ⇒ 文字用单行 `if` 二选一。
!
! 跑法 —— 手机：vml run examples/fortran/whack.f90
! 跑法 —— 桌面：vmlcli Examples/fortran/whack.f90 --screen 480x640 --frames /tmp/fr

program whack
  implicit none
  integer :: cell
  integer :: ox
  integer :: oy
  integer :: w
  integer :: h
  integer :: mole
  integer :: molett
  integer :: wait
  integer :: score
  integer :: lives
  integer :: over
  integer :: flash
  integer :: frames
  integer :: i
  integer :: k
  integer :: x
  integer :: y
  integer :: cx
  integer :: cy
  integer :: r
  integer :: t
  integer :: tid
  integer :: lang
  integer :: seed
  integer :: tx
  integer :: ty
  integer :: side

  lang = ui_scr_get_language()
  if (lang == 0) k = ui_win_open('打地鼠', 0, 0)
  if (lang /= 0) k = ui_win_open('Whack', 0, 0)

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

  seed = 20267
  mole = -1
  molett = 0
  wait = 6
  score = 0
  lives = 3
  over = 0
  flash = 0
  frames = 0

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

  do while (ui_win_is_closed() == 0)

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

    k = ui_2d_clear(-15724520)
    i = 0
    do while (i < 9)
      cx = ox + mod(i, 3) * cell + cell / 2
      cy = oy + (i / 3) * cell + cell / 2
      r = cell / 3
      k = ui_2d_ellipse(cx, cy + r / 3, r + cell / 12, r / 2 + cell / 20, -15066588, 1, 0)
      if (i == mole) then
        if (flash == 1) then
          k = ui_2d_circle(cx, cy, r, -1, 1, 0)
        else
          k = ui_2d_circle(cx, cy, r, -2509750, 1, 0)
        end if
        k = ui_2d_circle(cx - r / 3, cy - r / 4, r / 5, -15724520, 1, 0)
        k = ui_2d_circle(cx + r / 3, cy - r / 4, r / 5, -15724520, 1, 0)
      end if
      i = i + 1
    end do

    ! 得分条：每条 20 像素宽，最多 8 条
    i = 0
    do while (i < score)
      x = 10 + i * 20
      if (i < 8) k = ui_2d_rect(x, 16, 16, 10, -11409298, 1, 0, 2)
      i = i + 1
    end do
    ! 命：右上角三个小方块，掉一条熄灭一个
    i = 0
    do while (i < 3)
      x = w - 30 - i * 22
      if (i < lives) then
        k = ui_2d_rect(x, 14, 16, 16, -2069424, 1, 0, 3)
      else
        k = ui_2d_rect(x, 14, 16, 16, -14013898, 1, 0, 3)
      end if
      i = i + 1
    end do

    if (over == 1) then
      if (lang == 0) k = ui_2d_text(8, 44, '结束，点一下再来', -1513232, 15, 0)
      if (lang /= 0) k = ui_2d_text(8, 44, 'Over - tap to retry', -1513232, 15, 0)
    else
      if (lang == 0) k = ui_2d_text(8, 44, '冒头就点它', -6643536, 12, 0)
      if (lang /= 0) k = ui_2d_text(8, 44, 'Tap the mole when it pops', -6643536, 12, 0)
    end if
    k = ui_2d_present()

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

    if (t == 9) then
      frames = frames + 1
      if (frames >= 6000) exit
      if (over == 0) then
        flash = 0
        if (mole >= 0) then
          molett = molett - 1
          if (molett <= 0) then
            mole = -1
            lives = lives - 1
            wait = 8
            if (lives <= 0) over = 1
          end if
        else
          wait = wait - 1
          if (wait <= 0) then
            seed = mod(seed * 1103 + 12345, 32768)
            mole = mod(seed / 256, 9)
            molett = 14
          end if
        end if
      end if
    end if

    if (t == 6 .or. t == 4) then
      tx = ui_msg_get_a()
      ty = ui_msg_get_b()
      if (over == 1) then
        score = 0
        lives = 3
        over = 0
        flash = 0
        mole = -1
        wait = 6
        frames = 0
      else
        if (tx >= ox) then
          if (ty >= oy) then
            x = (tx - ox) / cell
            y = (ty - oy) / cell
            if (x < 3) then
              if (y < 3) then
                i = y * 3 + x
                if (i == mole) then
                  score = score + 1
                  mole = -1
                  seed = mod(seed * 1103 + 12345, 32768)
                  wait = 4 + mod(seed / 256, 4)
                  flash = 1
                end if
              end if
            end if
          end if
        end if
      end if
    end if

    if (t == 1) then
      k = ui_msg_get_a()
      if (k == 27) exit
      if (k == 82) then
        score = 0
        lives = 3
        over = 0
        mole = -1
      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 whack
