Anonymous user
2048: Difference between revisions
→{{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);
Game_Loop: loop
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 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;
when Restart => exit
else▼
▲ when Right => New_Board := VFlip(Move(VFlip(Board)));
Add_Block; -- OK since the board has changed▼
if Blanks = 0▼
when others
▲ end case;
(Board(Row)(Column) /= Board(Row)(Column+1))))▼
▲ 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 ("
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 !");
end if;
end
end loop
end Play_2048;
</lang>
{{out}}
<pre>+----+----+----+----+
|