# 记忆翻牌 —— 用 **Perl** 写的手机小游戏
# Memory matching — a small phone game written in **Perl**
#
# 玩法（与 Examples/c/app/memory.c / Examples/python/app/memory.py 同一套）：
#   4×4 共 8 对；点一张翻开，再点第二张；一样就留在明面，不一样 0.4 秒后扣回去；
#   全部配对完成即通关；通关后点屏幕重开。
#
# ## 本语言的实测约束（写这个文件时量的）
#   · `for (my $i = 0; ...)` 编不过（`期望 RPAREN，实际得到 ASSIGN`）⇒ 循环变量先 `my $i`、
#     再用 `while` 或 `for (; ...)`；
#   · 数字上屏用字符串插值（`"$moves"`）或 `sprintf`，两者都可用；
#   · 数组是 0 基。
#
# 跑法 —— 手机：vml run examples/perl/memory.pl
# 跑法 —— 桌面：vmlcli Examples/perl/memory.pl --screen 480x640 --frames /tmp/fr

use vml_ui;

my $k = 0;
my $lang = 0;
my $w = 360;
my $h = 620;
my $cell = 80;
my $ox = 20;
my $oy = 110;
my $seed = 20265;
my $firstx = -1;
my $firsty = -1;
my $hide = 0;
my $moves = 0;
my $matched = 0;
my $won = 0;
my $frames = 0;
my @pair;
my @up;
my $i = 0;
my $j = 0;
my $t = 0;
my $x = 0;
my $y = 0;
my $tx = 0;
my $ty = 0;
my $idx = 0;
my $tmp = 0;
my $key = 0;
my $tid = 0;
my $hud = "";
my $pad = 0;
my $top = 0;
my $bottom = 0;
my $avail = 0;
my $side = 0;
my $hintSize = 12;

$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 牌面按宽高较小的一边铺满可用区。
$pad = int($w / 40);
if ($pad < 8) { $pad = 8; }
$top = 62;
$bottom = 38;
$avail = $h - $top - $bottom;
if ($avail < 24) { $avail = 24; }
$side = $w - 2 * $pad;
if ($avail < $side) { $side = $avail; }
$cell = int($side / 4);
if ($cell < 6) { $cell = 6; }
$ox = int(($w - 4 * $cell) / 2);
$oy = $top + int(($avail - 4 * $cell) / 2);
$hintSize = 12;
if ($w < 360) { $hintSize = 10; }

# 开一局：0,0,1,1,…,7,7 再洗牌
$i = 0;
while ($i < 16) {
  $pair[$i] = int($i / 2);
  $up[$i] = 0;
  $i = $i + 1;
}
$i = 15;
while ($i > 0) {
  $seed = ($seed * 1103 + 12345) % 32768;
  $j = int($seed / 256) % ($i + 1);
  $tmp = $pair[$i];
  $pair[$i] = $pair[$j];
  $pair[$j] = $tmp;
  $i = $i - 1;
}

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

while (ui_win_is_closed() == 0) {

  # ── draw ──
  $k = ui_2d_clear(-15724520);
  $x = 0;
  while ($x < 4) {
    $y = 0;
    while ($y < 4) {
      $idx = $x * 4 + $y;
      if ($pair[$idx] < 0) {
        $k = ui_2d_rect($ox + $x * $cell + 3, $oy + $y * $cell + 3, $cell - 6, $cell - 6, -15329762, 1, 0, 6);
      } elsif ($up[$idx] == 1) {
        if ($pair[$idx] == 0) { $k = ui_2d_rect($ox + $x * $cell + 3, $oy + $y * $cell + 3, $cell - 6, $cell - 6, -2069424, 1, 0, 8); }
        if ($pair[$idx] == 1) { $k = ui_2d_rect($ox + $x * $cell + 3, $oy + $y * $cell + 3, $cell - 6, $cell - 6, -2509750, 1, 0, 8); }
        if ($pair[$idx] == 2) { $k = ui_2d_rect($ox + $x * $cell + 3, $oy + $y * $cell + 3, $cell - 6, $cell - 6, -11409298, 1, 0, 8); }
        if ($pair[$idx] == 3) { $k = ui_2d_rect($ox + $x * $cell + 3, $oy + $y * $cell + 3, $cell - 6, $cell - 6, -11890471, 1, 0, 8); }
        if ($pair[$idx] == 4) { $k = ui_2d_rect($ox + $x * $cell + 3, $oy + $y * $cell + 3, $cell - 6, $cell - 6, -6591248, 1, 0, 8); }
        if ($pair[$idx] == 5) { $k = ui_2d_rect($ox + $x * $cell + 3, $oy + $y * $cell + 3, $cell - 6, $cell - 6, -2069336, 1, 0, 8); }
        if ($pair[$idx] == 6) { $k = ui_2d_rect($ox + $x * $cell + 3, $oy + $y * $cell + 3, $cell - 6, $cell - 6, -11871802, 1, 0, 8); }
        if ($pair[$idx] == 7) { $k = ui_2d_rect($ox + $x * $cell + 3, $oy + $y * $cell + 3, $cell - 6, $cell - 6, -4145088, 1, 0, 8); }
        $k = ui_2d_text($ox + $x * $cell + int($cell / 2), $oy + $y * $cell + int($cell / 2) + int($cell / 8),
          sprintf("%d", $pair[$idx] + 1), -15724520, int($cell / 2), 1);
      } else {
        $k = ui_2d_rect($ox + $x * $cell + 3, $oy + $y * $cell + 3, $cell - 6, $cell - 6, -14013898, 1, 0, 8);
      }
      $y = $y + 1;
    }
    $x = $x + 1;
  }

  $hud = sprintf("%d", $moves);
  $k = ui_2d_text(10, 20, $hud, -1513232, 15, 0);
  if ($lang == 0) { $k = ui_2d_text(10, 42, "步数", -6643536, 10, 0); }
  if ($lang != 0) { $k = ui_2d_text(10, 42, "moves", -6643536, 10, 0); }

  if ($won == 1) {
    if ($lang == 0) { $k = ui_2d_text(int($w / 2), $h - 30, "全配好了！点一下再来", -1513232, $hintSize + 3, 1); }
    if ($lang != 0) { $k = ui_2d_text(int($w / 2), $h - 30, "All matched! Tap for a new one", -1513232, $hintSize + 3, 1); }
  } else {
    if ($lang == 0) { $k = ui_2d_text(int($w / 2), $h - 30, "翻两张一样的", -6643536, $hintSize, 1); }
    if ($lang != 0) { $k = ui_2d_text(int($w / 2), $h - 30, "Flip two that match", -6643536, $hintSize, 1); }
  }
  $k = ui_2d_present();

  $t = ui_msg_fetch_wait(0);
  if ($t == 10) { last; }

  if ($t == 6 || $t == 4) {
    $tx = ui_msg_get_a();
    $ty = ui_msg_get_b();
    if ($won == 1) {
      # 重开
      $seed = 20265;
      $i = 0;
      while ($i < 16) {
        $pair[$i] = int($i / 2);
        $up[$i] = 0;
        $i = $i + 1;
      }
      $i = 15;
      while ($i > 0) {
        $seed = ($seed * 1103 + 12345) % 32768;
        $j = int($seed / 256) % ($i + 1);
        $tmp = $pair[$i];
        $pair[$i] = $pair[$j];
        $pair[$j] = $tmp;
        $i = $i - 1;
      }
      $firstx = -1;
      $firsty = -1;
      $hide = 0;
      $moves = 0;
      $matched = 0;
      $won = 0;
    } else {
      if ($tx >= $ox && $ty >= $oy) {
        $x = int(($tx - $ox) / $cell);
        $y = int(($ty - $oy) / $cell);
        if ($x >= 0 && $y >= 0 && $x < 4 && $y < 4) {
          $idx = $x * 4 + $y;
          if ($pair[$idx] >= 0 && $up[$idx] == 0 && $hide == 0) {
            $up[$idx] = 1;
            if ($firstx < 0) {
              $firstx = $x;
              $firsty = $y;
            } else {
              $moves = $moves + 1;
              $j = $firstx * 4 + $firsty;
              if ($pair[$j] == $pair[$idx]) {
                $pair[$j] = -1;
                $pair[$idx] = -1;
                $matched = $matched + 2;
                if ($matched >= 16) { $won = 1; }
                $firstx = -1;
              } else {
                $hide = 8;
              }
            }
          }
        }
      }
    }
  }

  if ($t == 1) {
    $key = ui_msg_get_a();
    if ($key == 27) { last; }
  }

  if ($t == 9) {
    $frames = $frames + 1;
    if ($frames >= 8000) { last; }
    if ($hide > 0) {
      $hide = $hide - 1;
      if ($hide == 0) {
        $i = 0;
        while ($i < 16) {
          $up[$i] = 0;
          $i = $i + 1;
        }
        $firstx = -1;
      }
    }
  }
}

$k = ui_timer_kill($tid);
$k = ui_scr_set_keep_on(0);
$k = ui_win_close();
