-- 十五格拼图 —— 用 **Ada** 写的手机小游戏
-- Puzzle 15 -- a small phone game written in **Ada**
--
-- 玩法（与 Examples/c/app/puzzle15.c / Examples/python/app/puzzle15.py 同一套）：
--   4×4 共 15 个数字加一个空格；点空格旁边的格子（或按方向键）把格子滑进空格；
--   把 1..15 按顺序排好、空格落在右下角即通关；打乱靠随机走步（一定可解）。
--
-- ## 本语言的实测约束（见 Examples/ada/app/stopwatch.adb 文件头）
--   · 库函数 `K := ui_...(…)`；数组 1 基；`Integer'Image` 带一个前导空格；
--   · `and/or/not`，不等 `/=`；`while … loop … end loop;`、`if … then … elsif … else … end if;`
--
-- 跑法 —— 手机：vml run examples/ada/puzzle15.adb
-- 跑法 —— 桌面：vmlcli Examples/ada/puzzle15.adb --screen 480x640 --frames /tmp/fr

with VML_UI;

procedure Puzzle15 is
   type Board_Arr is array (1 .. 16) of Integer;
   Board : Board_Arr;
   K : Integer := 0;
   W : Integer := 0;
   H : Integer := 0;
   Cell : Integer := 80;
   CellW : Integer := 0;
   CellH : Integer := 0;
   Top : Integer := 56;
   Bot : Integer := 22;
   Ox : Integer := 20;
   Oy : Integer := 110;
   Dfs : Integer := 20;
   Hfs : Integer := 11;
   Wfs : Integer := 16;
   Blank : Integer := 16;
   Won : Integer := 0;
   Moves : Integer := 0;
   Seed : Integer := 20264;
   Frames : Integer := 0;
   I : Integer := 0;
   J : Integer := 0;
   N : Integer := 0;
   R : Integer := 0;
   Bx : Integer := 0;
   By : Integer := 0;
   T : Integer := 0;
   Tid : Integer := 0;
   Tx : Integer := 0;
   Ty : Integer := 0;
   Cx : Integer := 0;
   Cy : Integer := 0;
   Lang : Integer := 0;
begin
   Lang := ui_scr_get_language();
   K := ui_win_open(0, 0, 0);
   K := ui_scr_set_keep_on(1);
   W := ui_scr_get_w();
   H := ui_scr_get_h();
   if W <= 0 then
      W := 360;
   end if;
   if H <= 0 then
      H := 620;
   end if;
   Top := H / 12;
   if Top < 56 then
      Top := 56;
   end if;
   if Top > 96 then
      Top := 96;
   end if;
   Bot := H / 14;
   if Bot < 22 then
      Bot := 22;
   end if;
   if Bot > 64 then
      Bot := 64;
   end if;
   CellW := (W - 16) / 4;
   CellH := (H - Top - Bot - 8) / 4;
   Cell := CellW;
   if CellH < Cell then
      Cell := CellH;
   end if;
   if Cell < 6 then
      Cell := 6;
   end if;
   Ox := (W - 4 * Cell) / 2;
   Oy := Top + (H - Top - Bot - 4 * Cell) / 2;
   Dfs := Cell / 2;
   if Dfs < 8 then
      Dfs := 8;
   end if;
   if Dfs > 42 then
      Dfs := 42;
   end if;
   Hfs := H / 48;
   if Hfs < 9 then
      Hfs := 9;
   end if;
   if Hfs > 13 then
      Hfs := 13;
   end if;
   Wfs := H / 40;
   if Wfs < 12 then
      Wfs := 12;
   end if;
   if Wfs > 18 then
      Wfs := 18;
   end if;

   I := 1;
   while I <= 16 loop
      Board(I) := I;
      I := I + 1;
   end loop;
   Board(16) := 0;
   Blank := 16;
   N := 0;
   while N < 200 loop
      Seed := (Seed * 1103 + 12345) mod 32768;
      R := (Seed / 256) mod 4;
      Bx := (Blank - 1) mod 4;
      By := (Blank - 1) / 4;
      J := Blank;
      if R = 0 and Bx > 0 then
         J := Blank - 1;
      end if;
      if R = 1 and Bx < 3 then
         J := Blank + 1;
      end if;
      if R = 2 and By > 0 then
         J := Blank - 4;
      end if;
      if R = 3 and By < 3 then
         J := Blank + 4;
      end if;
      if J /= Blank then
         Board(Blank) := Board(J);
         Board(J) := 0;
         Blank := J;
      end if;
      N := N + 1;
   end loop;

   Tid := ui_timer_set(60, 0);

   while ui_win_is_closed() = 0 loop
      Won := 1;
      I := 1;
      while I < 16 loop
         if Board(I) /= I then
            Won := 0;
         end if;
         I := I + 1;
      end loop;
      if Board(16) /= 0 then
         Won := 0;
      end if;

      K := ui_2d_clear(-15724520);
      I := 0;
      while I < 16 loop
         if Board(I + 1) = 0 then
            K := ui_2d_rect(Ox + (I mod 4) * Cell + 2, Oy + (I / 4) * Cell + 2, Cell - 4, Cell - 4, -15066588, 1, 0, 4);
         else
            if Board(I + 1) = I + 1 then
               K := ui_2d_rect(Ox + (I mod 4) * Cell + 2, Oy + (I / 4) * Cell + 2, Cell - 4, Cell - 4, -12960184, 1, 0, 4);
            else
               K := ui_2d_rect(Ox + (I mod 4) * Cell + 2, Oy + (I / 4) * Cell + 2, Cell - 4, Cell - 4, -14013898, 1, 0, 4);
            end if;
            K := ui_2d_text(Ox + (I mod 4) * Cell + Cell / 2, Oy + (I / 4) * Cell + (Cell - Dfs) / 2,
               Integer'Image(Board(I + 1)), -1513232, Dfs, 1);
         end if;
         I := I + 1;
      end loop;

      K := ui_2d_text(10, 20, Integer'Image(Moves), -1513232, 15, 0);
      if Lang = 0 then
         K := ui_2d_text(10, 46, "步数", -6643536, 10, 0);
      else
         K := ui_2d_text(10, 46, "moves", -6643536, 10, 0);
      end if;

      if Won = 1 then
         if Lang = 0 then
            K := ui_2d_text(W / 2, H - Bot + 2, "排好了！", -11409298, Wfs, 1);
         else
            K := ui_2d_text(W / 2, H - Bot + 2, "Solved!", -11409298, Wfs, 1);
         end if;
      else
         K := ui_2d_text(W / 2, H - Bot + 2, "点空格旁的数字 / 方向键滑动", -6643536, Hfs, 1);
      end if;
      K := ui_2d_present();

      T := ui_msg_fetch_wait(0);
      if T = 10 then
         exit;
      end if;

      if T = 1 then
         I := ui_msg_get_a();
         if I = 27 then
            exit;
         end if;
         Bx := (Blank - 1) mod 4;
         By := (Blank - 1) / 4;
         J := Blank;
         if I = 37 and Bx < 3 then
            J := Blank + 1;
         end if;
         if I = 39 and Bx > 0 then
            J := Blank - 1;
         end if;
         if I = 38 and By < 3 then
            J := Blank + 4;
         end if;
         if I = 40 and By > 0 then
            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 and Ty >= Oy then
            Cx := (Tx - Ox) / Cell;
            Cy := (Ty - Oy) / Cell;
            if Cx >= 0 and Cy >= 0 and Cx < 4 and Cy < 4 then
               J := Cy * 4 + Cx + 1;
               Bx := (Blank - 1) mod 4;
               By := (Blank - 1) / 4;
               if (Cx = Bx and (Cy = By + 1 or Cy = By - 1)) or (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;

      if T = 9 then
         Frames := Frames + 1;
         if Frames >= 20000 then
            exit;
         end if;
      end if;
   end loop;

   K := ui_timer_kill(Tid);
   K := ui_scr_set_keep_on(0);
   K := ui_win_close();
end Puzzle15;
