! 记忆翻牌 —— 用 **Fortran** 写的手机小游戏
! Memory matching -- a small phone game written in **Fortran**
!
! 玩法（与 Examples/c/app/memory.c / Examples/python/app/memory.py 同一套）：
!   4×4 共 8 对；点一张翻开，再点第二张；一样就留在明面，不一样 0.4 秒后扣回去；
!   全部配对完成即通关；通关后点屏幕重开。
!
! 与 sokoban 刻意不同：这一份是**配对状态表 + 延时扣回**，一帧里没有移动。
! 也刻意不画数字（本前端没有数字转字符串路径）：牌面用**八种颜色**表达，
! 配对成功的一对画成**深色留印**。
!
! ◆ 写法要求（沿用 corpus/fortran/skel.f90 / sokoban.f90 的几条）
!   · `call ui_2d_rect(...)` 会被编成 `CALL sub_ui_rect`，链接器剥不到 ⇒ 一律写
!     **函数调用表达式** `k = ui_2d_rect(...)`（编成 func_ui_rect）；
!   · **数组 1 基**：扁平下标 = 行*4 + 列 + 1；
!   · 没有三元/merge ⇒ 界面文字用单行 `if` 二选一；`!=` 写 `/=`；
!   · `if` 条件里别写「紧跟括号的除法」（`Unexpected token: Div`）⇒ 先算进变量。
!
! 跑法 —— 手机：vml run examples/fortran/memory.f90
! 跑法 —— 桌面：vmlcli Examples/fortran/memory.f90 --screen 480x640 --frames /tmp/fr

program memory
  implicit none
  integer :: pair(16)
  integer :: up(16)
  integer :: i
  integer :: j
  integer :: k
  integer :: x
  integer :: y
  integer :: w
  integer :: h
  integer :: cell
  integer :: ox
  integer :: oy
  integer :: firstx
  integer :: firsty
  integer :: hide
  integer :: matched
  integer :: won
  integer :: tx
  integer :: ty
  integer :: t
  integer :: tid
  integer :: lang
  integer :: seed
  integer :: r
  integer :: tmp
  integer :: key

  lang = ui_scr_get_language()
  if (lang == 0) k = ui_win_open('记忆翻牌', 0, 0)
  if (lang /= 0) k = ui_win_open('Memory', 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 = 56
  cell = (w - 20) / 4
  k = (h - oy - 8) / 4
  if (k < cell) cell = k
  if (cell < 8) cell = 8
  ox = (w - cell * 4) / 2

  ! 开一局：0,0,1,1,…,7,7 再洗牌
  seed = 20265
  do i = 1, 16
    pair(i) = (i - 1) / 2
    up(i) = 0
  end do
  i = 16
  do while (i > 1)
    seed = mod(seed * 1103 + 12345, 32768)
    j = mod(seed / 256, i) + 1
    tmp = pair(i)
    pair(i) = pair(j)
    pair(j) = tmp
    i = i - 1
  end do
  firstx = -1
  firsty = -1
  hide = 0
  matched = 0
  won = 0

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

  do while (ui_win_is_closed() == 0)

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

    k = ui_2d_clear(-15724520)
    x = 0
    do while (x < 4)
      y = 0
      do while (y < 4)
        i = x * 4 + y + 1
        if (pair(i) < 0) then
          k = ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, -15329762, 1, 0, 6)
        else
          if (up(i) == 1) then
            if (pair(i) == 0) k = ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, -2069424, 1, 0, 8)
            if (pair(i) == 1) k = ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, -2509750, 1, 0, 8)
            if (pair(i) == 2) k = ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, -11409298, 1, 0, 8)
            if (pair(i) == 3) k = ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, -11890471, 1, 0, 8)
            if (pair(i) == 4) k = ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, -6591248, 1, 0, 8)
            if (pair(i) == 5) k = ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, -2069336, 1, 0, 8)
            if (pair(i) == 6) k = ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, -11871802, 1, 0, 8)
            if (pair(i) == 7) k = ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, -4145088, 1, 0, 8)
          else
            k = ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, -14013898, 1, 0, 8)
          end if
        end if
        y = y + 1
      end do
      x = x + 1
    end do

    if (won == 0) then
      if (lang == 0) k = ui_2d_text(10, 10, '点两张一样的', -6643536, 14, 0)
      if (lang /= 0) k = ui_2d_text(10, 10, 'Flip two that match', -6643536, 14, 0)
    else
      if (lang == 0) k = ui_2d_text(10, 10, '全配好了！点一下再来', -11409298, 14, 0)
      if (lang /= 0) k = ui_2d_text(10, 10, 'All matched! Tap for a new one', -11409298, 14, 0)
    end if
    k = ui_2d_present()

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

    if (t == 6 .or. t == 4) then
      tx = ui_msg_get_a()
      ty = ui_msg_get_b()
      if (won == 1) then
        ! 重开
        seed = 20265
        do i = 1, 16
          pair(i) = (i - 1) / 2
          up(i) = 0
        end do
        i = 16
        do while (i > 1)
          seed = mod(seed * 1103 + 12345, 32768)
          j = mod(seed / 256, i) + 1
          tmp = pair(i)
          pair(i) = pair(j)
          pair(j) = tmp
          i = i - 1
        end do
        firstx = -1
        firsty = -1
        hide = 0
        matched = 0
        won = 0
      else
        if (tx >= ox) then
          if (ty >= oy) then
            x = (tx - ox) / cell
            y = (ty - oy) / cell
            if (x < 4) then
              if (y < 4) then
                i = x * 4 + y + 1
                if (pair(i) >= 0 .and. up(i) == 0 .and. hide == 0) then
                  up(i) = 1
                  if (firstx < 0) then
                    firstx = x
                    firsty = y
                  else
                    j = firstx * 4 + firsty + 1
                    if (pair(j) == pair(i)) then
                      pair(j) = -1
                      pair(i) = -1
                      matched = matched + 2
                      if (matched >= 16) won = 1
                      firstx = -1
                    else
                      hide = 8
                    end if
                  end if
                end if
              end if
            end if
          end if
        end if
      end if
    end if

    if (t == 1) then
      key = ui_msg_get_a()
      if (key == 27) exit
    end if

    if (t == 9) then
      if (hide > 0) then
        hide = hide - 1
        if (hide == 0) then
          do i = 1, 16
            up(i) = 0
          end do
          firstx = -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 memory
