2048: Difference between revisions

156 bytes added ,  2 years ago
→‎{{header|Ada}}: Fix Restart by using two nested loops
m (Added missing closing lang tag to Julia solution)
(→‎{{header|Ada}}: Fix Restart by using two nested loops)
Line 1,146:
when others => Invalid),
when others => Invalid);
 
-- ----- Game data
function Random_Int is new System.Random_Numbers.Random_Discrete(Integer);
Line 1,157:
Score : Natural;
Generator : System.Random_Numbers.Generator;
 
-- ----- Displaying the board
procedure Display_Board is
Line 1,176:
Put_Line("Score =" & Score'Img);
end Display_Board;
 
-- ----- Game mechanics
procedure Add_Block is
Line 1,195:
end loop;
end Add_Block;
 
procedure Reset_Game is
begin
Line 1,204:
Add_Block;
end Reset_Game;
 
-- Moving and merging will always be performed leftward, hence the following transforms
function HFlip (What : in t_Row) return t_Row is
Line 1,220:
end return;
end Transpose;
 
-- For moving/merging, recursive expression functions will be used, but they
-- can't contain statements, hence the following sub-function used by Merge
Line 1,229:
return (1 => 0);
end Add_Blank;
 
function Move_Row (What : in t_List) return t_List is
(if What'Length = 1 then What
Line 1,235:
then Move_Row(What(What'First+1..What'Last)) & (1 => 0)
else (1 => What(What'First)) & Move_Row(What(What'First+1..What'Last)));
 
function Merge (What : in t_List) return t_List is
(if What'Length <= 1 or else What(What'First) = 0 then What
Line 1,241:
then (1 => 2*What(What'First)) & Merge(What(What'First+2..What'Last)) & Add_Blank(What(What'First))
else (1 => What(What'First)) & Merge(What(What'First+1..What'Last)));
 
function Move (What : in t_Board) return t_Board is
(Merge(Move_Row(What(1))),Merge(Move_Row(What(2))),Merge(Move_Row(What(3))),Merge(Move_Row(What(4))));
 
begin
System.Random_Numbers.Reset(Generator);
Reset_Game;
Main_Game_LoopMain_Loop: loop
Display_BoardReset_Game;
Game_Loop: loop
case Get_Keystroke is
when Restart => Reset_Game;
when Quit => exit Main_Game_Loop;
when Left => New_Board := Move(Board);
when Right => New_Board := VFlip(Move(VFlip(Board)));
when Up => New_Board := Transpose(Move(Transpose(Board)));
when Down => New_Board := Transpose(VFlip(Move(VFlip(Transpose(Board)))));
when others => null;
end case;
 
if New_Board = Board then
Put_Line ("Invalid move...");
elsif (for some Row of New_Board => (for some Cell of Row => Cell = 2048)) then
Display_Board;
Put_Linecase ("WinGet_Keystroke !");is
when Restart => exit Main_Game_LoopGame_Loop;
when RestartQuit => Reset_Gameexit Main_Loop;
else
Board : when Left => New_Board := Move(Board);
when Right => New_Board := VFlip(Move(VFlip(Board)));
Add_Block; -- OK since the board has changed
when LeftUp => New_Board := Transpose(Move(Transpose(Board)));
if Blanks = 0
andwhen thenDown (for all Row in=> 1..4New_Board :=> Transpose(VFlip(Move(VFlip(Transpose(Board)))));
when others (for all Column in 1..3 => null;
end case;
(Board(Row)(Column) /= Board(Row)(Column+1))))
and then (for all Row in 1..3 =>
if New_Board = Board then
(for all Column in 1..4 =>
Put_Line ("Invalid move...");
(Board(Row)(Column) /= Board(Row+1)(Column)))) then
elsif (for some Row of New_Board => (for some Cell of Row => Cell = 2048)) then
Display_Board;
Put_Line ("LostWin !");
exit Main_Game_Loop;
else
Board := New_Board;
Add_Block; -- OK since the board has changed
if Blanks = 0
and then (for all Row in 1..4 =>
(for all Column in 1..3 =>
(Board(Row)(Column) /= Board(Row)(Column+1))))
and then (for all Row in 1..3 =>
(for all Column in 1..4 =>
(Board(Row)(Column) /= Board(Row+1)(Column)))) then
Display_Board;
Put_Line ("Lost !");
when Quit => exit Main_Game_LoopMain_Loop;
when others =>end nullif;
end if;
end ifloop Game_Loop;
end loop Main_Game_LoopMain_Loop;
end Play_2048;</lang>
</lang>
{{out}}
<pre>+----+----+----+----+
Anonymous user