← 都来码 / Dolaima← 例程索引ada/app/g2048.adb▶ 在浏览器里试玩 原文

ada/app/g2048.adb · 10482 字节 · 下载原文

▶ 在浏览器里试玩

-- 2048 —— 用 **Ada** 写的手机小游戏
-- 2048 -- a small phone game written in **Ada**
--
-- 玩法(与 Examples/c/app/game2048.c / Examples/python/app/game2048.py 同一套):
--   4×4 棋盘,方向键 / 点屏幕四边往一个方向滑;相同数字相撞合成两倍、得分加上合成值;
--   每滑一次冒一个新数字(10% 是 4);到 2048 算赢(还能继续),没空格且四邻无相等算输。
--
-- ## 合并的先后容易写错:一次滑动里每个块只许合并一次(`4 4 4 4` → `8 8`),从远端往回收。
--
-- ## 本语言的实测约束(见 Examples/ada/app/stopwatch.adb 文件头)
--
-- 跑法 —— 手机:vml run examples/ada/g2048.adb
-- 跑法 —— 桌面:vmlcli Examples/ada/g2048.adb --screen 480x640 --frames /tmp/fr

with VML_UI;

procedure G2048 is
   type Board_Arr is array (1 .. 16) of Integer;
   type Line_Arr is array (1 .. 4) of Integer;
   B : Board_Arr;
   Line : Line_Arr;
   Old : Line_Arr;
   K : Integer := 0;
   W : Integer := 360;
   H : Integer := 620;
   Cell : Integer := 80;
   CellW : Integer := 0;
   CellH : Integer := 0;
   Top : Integer := 56;
   Bot : Integer := 22;
   Ox : Integer := 20;
   Oy : Integer := 120;
   Hfs : Integer := 11;
   Lfs : Integer := 14;
   Score : Integer := 0;
   Won : Integer := 0;
   Lost : Integer := 0;
   Frames : Integer := 0;
   Seed : Integer := 20261;
   I : Integer := 0;
   J : Integer := 0;
   N : Integer := 0;
   Outc : Integer := 0;
   Pick : Integer := 0;
   Changed : Integer := 0;
   D : Integer := -1;
   T : Integer := 0;
   Tid : Integer := 0;
   Tx : Integer := 0;
   Ty : Integer := 0;
   Dl : Integer := 0;
   Dr : Integer := 0;
   Dt : Integer := 0;
   Db : Integer := 0;
   Kk : Integer := 0;
   V : Integer := 0;
   Col : 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;
   Hfs := H / 48;
   if Hfs < 9 then
      Hfs := 9;
   end if;
   if Hfs > 13 then
      Hfs := 13;
   end if;
   Lfs := H / 40;
   if W < 400 then
      Lfs := 11;
   end if;
   if Lfs > 18 then
      Lfs := 18;
   end if;

   I := 1;
   while I <= 16 loop
      B(I) := 0;
      I := I + 1;
   end loop;
   Seed := 20261;
   J := 0;
   while J < 2 loop
      Seed := (Seed * 1103 + 12345) mod 32768;
      Pick := (Seed / 256) mod 16 + 1;
      while B(Pick) /= 0 loop
         Seed := (Seed * 1103 + 12345) mod 32768;
         Pick := (Seed / 256) mod 16 + 1;
      end loop;
      B(Pick) := 2;
      J := J + 1;
   end loop;

   Tid := ui_timer_set(50, 0);

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

      D := -1;
      if T = 1 then
         I := ui_msg_get_a();
         if I = 37 then
            D := 0;
         elsif I = 39 then
            D := 1;
         elsif I = 38 then
            D := 2;
         elsif I = 40 then
            D := 3;
         elsif I = 27 or I = 82 then
            J := 1;
            while J <= 16 loop
               B(J) := 0;
               J := J + 1;
            end loop;
            B(1) := 2;
            B(6) := 2;
            Score := 0;
            Won := 0;
            Lost := 0;
         end if;
      end if;

      if T = 6 or T = 4 then
         Tx := ui_msg_get_a();
         Ty := ui_msg_get_b();
         Dl := Tx;
         Dr := W - Tx;
         Dt := Ty;
         Db := H - Ty;
         if Dl <= Dr and Dl <= Dt and Dl <= Db then
            D := 0;
         elsif Dr <= Dt and Dr <= Db then
            D := 1;
         elsif Dt <= Db then
            D := 2;
         else
            D := 3;
         end if;
      end if;

      if D >= 0 and Lost = 0 then
         Changed := 0;
         Kk := 0;
         while Kk < 4 loop
            I := 0;
            while I < 4 loop
               if D = 0 then
                  Line(I + 1) := B(Kk * 4 + I + 1);
               elsif D = 1 then
                  Line(I + 1) := B(Kk * 4 + (3 - I) + 1);
               elsif D = 2 then
                  Line(I + 1) := B(I * 4 + Kk + 1);
               else
                  Line(I + 1) := B((3 - I) * 4 + Kk + 1);
               end if;
               Old(I + 1) := Line(I + 1);
               I := I + 1;
            end loop;
            N := 0;
            I := 0;
            while I < 4 loop
               if Line(I + 1) /= 0 then
                  Line(N + 1) := Line(I + 1);
                  N := N + 1;
               end if;
               I := I + 1;
            end loop;
            I := N;
            while I < 4 loop
               Line(I + 1) := 0;
               I := I + 1;
            end loop;
            Outc := 0;
            I := 0;
            while I < N loop
               if I + 1 < N and Line(I + 1) = Line(I + 2) then
                  Line(Outc + 1) := Line(I + 1) * 2;
                  Score := Score + Line(I + 1) * 2;
                  if Line(I + 1) * 2 >= 2048 then
                     Won := 1;
                  end if;
                  I := I + 2;
               else
                  Line(Outc + 1) := Line(I + 1);
                  I := I + 1;
               end if;
               Outc := Outc + 1;
            end loop;
            I := Outc;
            while I < 4 loop
               Line(I + 1) := 0;
               I := I + 1;
            end loop;
            I := 0;
            while I < 4 loop
               if Line(I + 1) /= Old(I + 1) then
                  Changed := 1;
               end if;
               I := I + 1;
            end loop;
            I := 0;
            while I < 4 loop
               if D = 0 then
                  B(Kk * 4 + I + 1) := Line(I + 1);
               elsif D = 1 then
                  B(Kk * 4 + (3 - I) + 1) := Line(I + 1);
               elsif D = 2 then
                  B(I * 4 + Kk + 1) := Line(I + 1);
               else
                  B((3 - I) * 4 + Kk + 1) := Line(I + 1);
               end if;
               I := I + 1;
            end loop;
            Kk := Kk + 1;
         end loop;
         if Changed /= 0 then
            N := 0;
            I := 0;
            while I < 16 loop
               if B(I + 1) = 0 then
                  N := N + 1;
               end if;
               I := I + 1;
            end loop;
            if N > 0 then
               Seed := (Seed * 1103 + 12345) mod 32768;
               Pick := (Seed / 256) mod N;
               I := 0;
               while I < 16 loop
                  if B(I + 1) = 0 then
                     if Pick = 0 then
                        Seed := (Seed * 1103 + 12345) mod 32768;
                        if (Seed / 256) mod 10 = 0 then
                           B(I + 1) := 4;
                        else
                           B(I + 1) := 2;
                        end if;
                        I := 16;
                     else
                        Pick := Pick - 1;
                     end if;
                  end if;
                  if I < 16 then
                     I := I + 1;
                  end if;
               end loop;
            end if;
            N := 0;
            I := 0;
            while I < 16 loop
               if B(I + 1) = 0 then
                  N := 1;
               end if;
               I := I + 1;
            end loop;
            I := 0;
            while I < 15 loop
               if I mod 4 /= 3 and B(I + 1) = B(I + 2) then
                  N := 1;
               end if;
               if I + 4 < 16 and B(I + 1) = B(I + 5) then
                  N := 1;
               end if;
               I := I + 1;
            end loop;
            if N = 0 then
               Lost := 1;
            end if;
         end if;
      end if;

      K := ui_2d_clear(-15724520);
      I := 0;
      while I < 16 loop
         V := B(I + 1);
         if V = 0 then
            K := ui_2d_rect(Ox + (I mod 4) * Cell + 2, Oy + (I / 4) * Cell + 2, Cell - 4, Cell - 4, -15066588, 1, 0, 6);
         else
            Col := -14013898;
            if V = 4 then
               Col := -12960184;
            elsif V = 8 then
               Col := -11890471;
            elsif V = 16 then
               Col := -11409298;
            elsif V = 32 then
               Col := -2509750;
            elsif V = 64 then
               Col := -2069424;
            elsif V = 128 then
               Col := -2069336;
            elsif V = 256 then
               Col := -6591248;
            elsif V = 512 then
               Col := -11871802;
            elsif V >= 1024 then
               Col := -4145088;
            end if;
            K := ui_2d_rect(Ox + (I mod 4) * Cell + 2, Oy + (I / 4) * Cell + 2, Cell - 4, Cell - 4, Col, 1, 0, 6);
            K := ui_2d_text(Ox + (I mod 4) * Cell + Cell / 2, Oy + (I / 4) * Cell + Cell / 2, Integer'Image(V), -1513232, Cell / 3, 1);
         end if;
         I := I + 1;
      end loop;

      K := ui_2d_text(10, 20, Integer'Image(Score), -1513232, 18, 0);
      if Lang = 0 then
         K := ui_2d_text(10, 48, "得分", -6643536, 10, 0);
      else
         K := ui_2d_text(10, 48, "score", -6643536, 10, 0);
      end if;

      if Lost /= 0 then
         if Lang = 0 then
            K := ui_2d_text(W / 2, H - Bot + 2, "没路可走了,ESC / R 重开", -1513232, Lfs, 1);
         else
            K := ui_2d_text(W / 2, H - Bot + 2, "No moves left - ESC / R to restart", -1513232, Lfs, 1);
         end if;
      else
         K := ui_2d_text(W / 2, H - Bot + 2, "方向键 / 点屏幕四边滑动", -6643536, Hfs, 1);
      end if;
      K := ui_2d_present();

      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 G2048;