-- 扫雷 —— 用 **Ada** 写的手机小游戏（网格推理）
-- Minesweeper -- a small phone game written in **Ada** (grid deduction)
--
-- 玩法（与 Examples/c/app/minesweeper.c / Examples/python/app/minesweeper.py 同一套）：
--   9×9 共 10 颗雷；点格子翻开，数字是周围 8 格的雷数；翻到 0 会**连锁展开**；
--   顶部按钮切到「插旗」模式再点就是标雷；把所有非雷格子翻开即通关，踩到雷就结束。
--
-- ## 本语言的实测约束（见 Examples/ada/app/stopwatch.adb 文件头）
--
-- 跑法 —— 手机：vml run examples/ada/minesweeper.adb
-- 跑法 —— 桌面：vmlcli Examples/ada/minesweeper.adb --screen 480x640 --frames /tmp/fr

with VML_UI;

procedure Minesweeper is
   type Cell_Arr is array (1 .. 81) of Integer;
   Mine : Cell_Arr;
   Openc : Cell_Arr;
   Flag : Cell_Arr;
   Qx : Cell_Arr;
   Qy : Cell_Arr;
   K : Integer := 0;
   W : Integer := 360;
   H : Integer := 620;
   Cell : Integer := 30;
   CellW : Integer := 0;
   CellH : Integer := 0;
   Top : Integer := 74;
   Bot : Integer := 18;
   BtnY : Integer := 38;
   BtnH : Integer := 24;
   Bfs : Integer := 12;
   Hfs : Integer := 10;
   Ox : Integer := 20;
   Oy : Integer := 96;
   Opened : Integer := 0;
   Dead : Integer := 0;
   Won : Integer := 0;
   Flagmode : Integer := 0;
   Frames : Integer := 0;
   Seed : Integer := 20261117;
   I : Integer := 0;
   X : Integer := 0;
   Y : Integer := 0;
   N : Integer := 0;
   Nb : Integer := 0;
   Dx : Integer := 0;
   Dy : Integer := 0;
   Nx : Integer := 0;
   Ny : Integer := 0;
   Head : Integer := 0;
   Tail : Integer := 0;
   Cx : Integer := 0;
   Cy : Integer := 0;
   T : Integer := 0;
   Tid : Integer := 0;
   Tx : Integer := 0;
   Ty : Integer := 0;
   Idx : Integer := 0;
   Px : Integer := 0;
   Py : Integer := 0;
   J : 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;
   BtnY := H / 8;
   if BtnY < 38 then
      BtnY := 38;
   end if;
   if BtnY > 64 then
      BtnY := 64;
   end if;
   BtnH := H / 10;
   if BtnH < 24 then
      BtnH := 24;
   end if;
   if BtnH > 40 then
      BtnH := 40;
   end if;
   Bfs := H / 40;
   if Bfs < 11 then
      Bfs := 11;
   end if;
   if Bfs > 20 then
      Bfs := 20;
   end if;
   Hfs := H / 48;
   if Hfs < 10 then
      Hfs := 10;
   end if;
   if Hfs > 13 then
      Hfs := 13;
   end if;
   Top := BtnY + BtnH + 12;
   Bot := H / 20;
   if Bot < 18 then
      Bot := 18;
   end if;
   if Bot > 48 then
      Bot := 48;
   end if;
   CellW := (W - 16) / 9;
   CellH := (H - Top - Bot - 8) / 9;
   Cell := CellW;
   if CellH < Cell then
      Cell := CellH;
   end if;
   if Cell < 6 then
      Cell := 6;
   end if;
   Ox := (W - 9 * Cell) / 2;
   Oy := Top + (H - Top - Bot - 9 * Cell) / 2;

   I := 1;
   while I <= 81 loop
      Mine(I) := 0;
      Openc(I) := 0;
      Flag(I) := 0;
      I := I + 1;
   end loop;
   I := 0;
   while I < 10 loop
      Seed := (Seed * 1103 + 12345) mod 32768;
      X := (Seed / 256) mod 9 + 1;
      Seed := (Seed * 1103 + 12345) mod 32768;
      Y := (Seed / 256) mod 9 + 1;
      Idx := (X - 1) * 9 + Y;
      if Mine(Idx) = 0 then
         Mine(Idx) := 1;
         I := I + 1;
      end if;
   end loop;

   Tid := ui_timer_set(80, 0);

   while ui_win_is_closed() = 0 loop
      K := ui_2d_clear(-15724520);

      if Flagmode = 0 then
         K := ui_2d_rect(8, BtnY, W / 3, BtnH, -14013898, 1, 0, 8);
         if Lang = 0 then
            K := ui_2d_text(12, BtnY + (BtnH - Bfs) / 2, "翻开模式", -1513232, Bfs, 0);
         else
            K := ui_2d_text(12, BtnY + (BtnH - Bfs) / 2, "Reveal mode", -1513232, Bfs, 0);
         end if;
      else
         K := ui_2d_rect(8, BtnY, W / 3, BtnH, -2069424, 1, 0, 8);
         if Lang = 0 then
            K := ui_2d_text(12, BtnY + (BtnH - Bfs) / 2, "插旗模式", -15724520, Bfs, 0);
         else
            K := ui_2d_text(12, BtnY + (BtnH - Bfs) / 2, "Flag mode", -15724520, Bfs, 0);
         end if;
      end if;

      X := 1;
      while X <= 9 loop
         Y := 1;
         while Y <= 9 loop
            Idx := (X - 1) * 9 + Y;
            Px := Ox + (X - 1) * Cell;
            Py := Oy + (Y - 1) * Cell;
            if Openc(Idx) /= 0 then
               if Mine(Idx) = 1 then
                  K := ui_2d_rect(Px + 1, Py + 1, Cell - 2, Cell - 2, -2069424, 1, 0, 2);
               else
                  K := ui_2d_rect(Px + 1, Py + 1, Cell - 2, Cell - 2, -15066588, 1, 0, 2);
                  Nb := 0;
                  Dx := -1;
                  while Dx <= 1 loop
                     Dy := -1;
                     while Dy <= 1 loop
                        if not (Dx = 0 and Dy = 0) then
                           Nx := X + Dx;
                           Ny := Y + Dy;
                           if Nx >= 1 and Ny >= 1 and Nx <= 9 and Ny <= 9 then
                              if Mine((Nx - 1) * 9 + Ny) = 1 then
                                 Nb := Nb + 1;
                              end if;
                           end if;
                        end if;
                        Dy := Dy + 1;
                     end loop;
                     Dx := Dx + 1;
                  end loop;
                  N := 0;
                  while N < Nb loop
                     K := ui_2d_circle(Px + Cell / 4 + (N mod 4) * (Cell / 5), Py + Cell / 3 + (N / 4) * (Cell / 5), Cell / 12, -1513232, 1, 0);
                     N := N + 1;
                  end loop;
               end if;
            else
               K := ui_2d_rect(Px + 1, Py + 1, Cell - 2, Cell - 2, -14013898, 1, 0, 3);
               if Flag(Idx) /= 0 then
                  K := ui_2d_circle(Px + Cell / 2, Py + Cell / 2, Cell / 4, -2069424, 1, 0);
               end if;
            end if;
            Y := Y + 1;
         end loop;
         X := X + 1;
      end loop;

      if Dead /= 0 then
         K := ui_2d_text(8, 8, "踩雷了，ESC 重来", -2069424, Hfs + 3, 0);
      elsif Won /= 0 then
         K := ui_2d_text(8, 8, "扫清了！ESC 再来", -11409298, Hfs + 3, 0);
      else
         K := ui_2d_text(8, 8, "点格子翻开 / 点顶部按钮插旗", -6643536, Hfs, 0);
      end if;
      K := ui_2d_present();

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

      if T = 6 or T = 4 then
         Tx := ui_msg_get_a();
         Ty := ui_msg_get_b();
         if Tx >= 8 and Tx <= 8 + W / 3 and Ty >= BtnY and Ty <= BtnY + BtnH then
            if Flagmode = 0 then
               Flagmode := 1;
            else
               Flagmode := 0;
            end if;
         elsif Tx >= Ox and Ty >= Oy then
            X := (Tx - Ox) / Cell + 1;
            Y := (Ty - Oy) / Cell + 1;
            if X <= 9 and Y <= 9 then
               if Dead = 0 and Won = 0 then
                  Idx := (X - 1) * 9 + Y;
                  if Flagmode = 1 then
                     if Openc(Idx) = 0 then
                        if Flag(Idx) = 0 then
                           Flag(Idx) := 1;
                        else
                           Flag(Idx) := 0;
                        end if;
                     end if;
                  elsif Openc(Idx) = 0 and Flag(Idx) = 0 then
                     Qx(1) := X;
                     Qy(1) := Y;
                     Head := 1;
                     Tail := 1;
                     while Head <= Tail loop
                        Cx := Qx(Head);
                        Cy := Qy(Head);
                        Head := Head + 1;
                        Idx := (Cx - 1) * 9 + Cy;
                        if Openc(Idx) = 0 and Flag(Idx) = 0 then
                           Openc(Idx) := 1;
                           Opened := Opened + 1;
                           if Mine(Idx) = 1 then
                              Dead := 1;
                              Head := Tail + 1;
                           else
                              Nb := 0;
                              Dx := -1;
                              while Dx <= 1 loop
                                 Dy := -1;
                                 while Dy <= 1 loop
                                    if not (Dx = 0 and Dy = 0) then
                                       Nx := Cx + Dx;
                                       Ny := Cy + Dy;
                                       if Nx >= 1 and Ny >= 1 and Nx <= 9 and Ny <= 9 then
                                          if Mine((Nx - 1) * 9 + Ny) = 1 then
                                             Nb := Nb + 1;
                                          end if;
                                       end if;
                                    end if;
                                    Dy := Dy + 1;
                                 end loop;
                                 Dx := Dx + 1;
                              end loop;
                              if Nb = 0 and Dead = 0 then
                                 Dx := -1;
                                 while Dx <= 1 loop
                                    Dy := -1;
                                    while Dy <= 1 loop
                                       if not (Dx = 0 and Dy = 0) then
                                          Nx := Cx + Dx;
                                          Ny := Cy + Dy;
                                          if Nx >= 1 and Ny >= 1 and Nx <= 9 and Ny <= 9 then
                                             if Tail < 81 then
                                                Tail := Tail + 1;
                                                Qx(Tail) := Nx;
                                                Qy(Tail) := Ny;
                                             end if;
                                          end if;
                                       end if;
                                       Dy := Dy + 1;
                                    end loop;
                                    Dx := Dx + 1;
                                 end loop;
                              end if;
                           end if;
                        end if;
                     end loop;
                     if Dead = 0 and Opened >= 81 - 10 then
                        Won := 1;
                     end if;
                  end if;
               end if;
            end if;
         end if;
      end if;

      if T = 1 then
         I := ui_msg_get_a();
         if I = 27 or I = 82 then
            J := 1;
            while J <= 81 loop
               Mine(J) := 0;
               Openc(J) := 0;
               Flag(J) := 0;
               J := J + 1;
            end loop;
            Seed := 20261117;
            I := 0;
            while I < 10 loop
               Seed := (Seed * 1103 + 12345) mod 32768;
               X := (Seed / 256) mod 9 + 1;
               Seed := (Seed * 1103 + 12345) mod 32768;
               Y := (Seed / 256) mod 9 + 1;
               Idx := (X - 1) * 9 + Y;
               if Mine(Idx) = 0 then
                  Mine(Idx) := 1;
                  I := I + 1;
               end if;
            end loop;
            Opened := 0;
            Dead := 0;
            Won := 0;
            Flagmode := 0;
            Frames := 0;
         end if;
      end if;

      if T = 9 then
         Frames := Frames + 1;
         if Frames >= 4000 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 Minesweeper;
