aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJedidiah Barber <contact@jedbarber.id.au>2025-04-16 15:51:58 +1200
committerJedidiah Barber <contact@jedbarber.id.au>2025-04-16 15:51:58 +1200
commitbaa3a346cde2c2c965243e554a15ed3bd6f5c7fe (patch)
treef8ec8376a6a42053e15cb9607b0fef17bb4f321d
parentb3455e502f4491af22e190a14aba9565f534bb59 (diff)
Code style improvements, constants marked as constant
-rw-r--r--src/displays.adb46
-rw-r--r--src/displays.ads5
-rw-r--r--src/grids.adb11
-rw-r--r--src/grids.ads3
-rw-r--r--src/main.adb3
-rw-r--r--src/misc.adb1
-rw-r--r--src/moves.adb1
-rw-r--r--src/moves.ads3
-rw-r--r--src/pathfinding.adb9
-rw-r--r--src/pathfinding.ads3
-rw-r--r--src/sokoban.adb91
-rw-r--r--src/sokoban.ads4
-rw-r--r--src/squares.adb1
-rw-r--r--src/squares.ads5
-rw-r--r--src/things.adb1
-rw-r--r--src/things.ads5
16 files changed, 101 insertions, 91 deletions
diff --git a/src/displays.adb b/src/displays.adb
index 468ca70..6c33171 100644
--- a/src/displays.adb
+++ b/src/displays.adb
@@ -3,15 +3,14 @@
with
FLTK.Screen,
- FLTK.Events,
- Misc;
+ FLTK.Events;
package body Displays is
package WD renames FLTK.Widgets.Groups.Windows.Double;
- package B renames FLTK.Widgets.Boxes;
+ package B renames FLTK.Widgets.Boxes;
@@ -21,25 +20,26 @@ package body Displays is
Text : in String)
return Display
is
- My_Width : Integer := Misc.Max (Message_Box_Width, W);
- My_Height : Integer := Misc.Max (Message_Box_Height + Stat_Box_Height, H);
+ My_Width : constant Integer := Misc.Max (Message_Box_Width, W);
+ My_Height : constant Integer := Misc.Max (Message_Box_Height + Stat_Box_Height, H);
begin
return This : Display :=
- (WD.Double_Window'(WD.Forge.Create (X, Y, My_Width, My_Height, Text)) with
-
- Message_Box => B.Box'(B.Forge.Create
- (0, 0, Message_Box_Width, Message_Box_Height, "")),
- Level_Box => B.Box'(B.Forge.Create
- (0, Message_Box_Height, Stat_Box_Width, Stat_Box_Height, "")),
- Move_Box => B.Box'(B.Forge.Create
- (Stat_Box_Width, Message_Box_Height, Stat_Box_Width, Stat_Box_Height, "")),
- Current_Grid => null,
- Key_Func => null,
- Mouse_Func => null) do
-
+ (WD.Forge.Create (X, Y, My_Width, My_Height, Text)
+ with
+ Message_Box => B.Forge.Create
+ (0, 0, Message_Box_Width, Message_Box_Height, ""),
+ Level_Box => B.Forge.Create
+ (0, Message_Box_Height, Stat_Box_Width, Stat_Box_Height, ""),
+ Move_Box => B.Forge.Create
+ (Stat_Box_Width, Message_Box_Height, Stat_Box_Width, Stat_Box_Height, ""),
+ Current_Grid => null,
+ Key_Func => null,
+ Mouse_Func => null)
+ do
This.Add (This.Message_Box);
This.Add (This.Level_Box);
This.Add (This.Move_Box);
+
This.Message_Box.Set_Label_Size (Text_Size);
This.Level_Box.Set_Label_Size (Text_Size);
This.Move_Box.Set_Label_Size (Text_Size);
@@ -92,11 +92,11 @@ package body Displays is
procedure Ensure_Correct_Size
(This : in out Display)
is
- New_Width : Integer :=
+ New_Width : constant Integer :=
Misc.Max (Message_Box_Width, This.Current_Grid.Get_W);
- New_Height : Integer :=
+ New_Height : constant Integer :=
Message_Box_Height + This.Current_Grid.Get_H + Stat_Box_Height;
- Grid_X : Integer :=
+ Grid_X : constant Integer :=
Misc.Max (0, (Message_Box_Width - This.Current_Grid.Get_W) / 2);
begin
This.Current_Grid.Reposition (Grid_X, 0);
@@ -178,12 +178,11 @@ package body Displays is
This.Key_Func (FLTK.Press (FLTK.Events.Last_Key)) = FLTK.Handled
then
return FLTK.Handled;
-
- elsif This.Mouse_Func /= null and then Event = FLTK.Release and then
+ elsif
+ This.Mouse_Func /= null and then Event = FLTK.Release and then
This.Mouse_Func (FLTK.Events.Mouse_X, FLTK.Events.Mouse_Y) = FLTK.Handled
then
return FLTK.Handled;
-
else
return WD.Double_Window (This).Handle (Event);
end if;
@@ -192,3 +191,4 @@ package body Displays is
end Displays;
+
diff --git a/src/displays.ads b/src/displays.ads
index b278b6f..b7fa8fd 100644
--- a/src/displays.ads
+++ b/src/displays.ads
@@ -19,8 +19,6 @@ package Displays is
type Display is new FLTK.Widgets.Groups.Windows.Double.Double_Window with private;
-
-
type Keyboard_Callback is access function
(Key : in FLTK.Key_Combo)
return FLTK.Event_Outcome;
@@ -114,13 +112,14 @@ private
Text_Size : constant FLTK.Font_Size := 12;
+
Message_Box_Width : constant Integer := 440;
Message_Box_Height : constant Integer := 80;
-
Stat_Box_Width : constant Integer := Message_Box_Width / 2;
Stat_Box_Height : constant Integer := 20;
end Displays;
+
diff --git a/src/grids.adb b/src/grids.adb
index 12f1ae4..5bf82c3 100644
--- a/src/grids.adb
+++ b/src/grids.adb
@@ -8,10 +8,12 @@ package body Grids is
Text : in String)
return Grid is
begin
- return This : Grid :=
- (FLTK.Widgets.Widget'(FLTK.Widgets.Forge.Create (X, Y, W, H, Text)) with
- Cells => Square_Vector_Vectors.Empty_Vector,
- Rows => 0, Cols => 0);
+ return This : constant Grid :=
+ (FLTK.Widgets.Forge.Create (X, Y, W, H, Text)
+ with
+ Cells => Square_Vector_Vectors.Empty_Vector,
+ Rows => 0,
+ Cols => 0);
end Create;
@@ -129,3 +131,4 @@ package body Grids is
end Grids;
+
diff --git a/src/grids.ads b/src/grids.ads
index 98230f8..856ff17 100644
--- a/src/grids.ads
+++ b/src/grids.ads
@@ -59,7 +59,7 @@ package Grids is
(This : in out Grid;
X, Y : in Integer;
Item : in Squares.Square)
- with Pre => This.In_Bounds (X, Y);
+ with Pre => This.In_Bounds (X, Y);
@@ -106,3 +106,4 @@ private
end Grids;
+
diff --git a/src/main.adb b/src/main.adb
index 41b106e..cbdca0c 100644
--- a/src/main.adb
+++ b/src/main.adb
@@ -9,8 +9,11 @@ with
function Main
return Integer is
begin
+
Sokoban.Load_Level (Sokoban.LevelID'First);
Sokoban.Show;
return FLTK.Run;
+
end Main;
+
diff --git a/src/misc.adb b/src/misc.adb
index d1aa7ca..aa1b1e2 100644
--- a/src/misc.adb
+++ b/src/misc.adb
@@ -29,3 +29,4 @@ package body Misc is
end Misc;
+
diff --git a/src/moves.adb b/src/moves.adb
index 4024a4d..b86ba8c 100644
--- a/src/moves.adb
+++ b/src/moves.adb
@@ -69,3 +69,4 @@ package body Moves is
end Moves;
+
diff --git a/src/moves.ads b/src/moves.ads
index 3e86847..2c9a334 100644
--- a/src/moves.ads
+++ b/src/moves.ads
@@ -14,8 +14,6 @@ package Moves is
end record;
-
-
type Path is tagged private;
Empty_Path : constant Path;
@@ -72,3 +70,4 @@ private
end Moves;
+
diff --git a/src/pathfinding.adb b/src/pathfinding.adb
index 9e45ec1..5234cdb 100644
--- a/src/pathfinding.adb
+++ b/src/pathfinding.adb
@@ -14,11 +14,13 @@ package body Pathfinding is
X, Y : Integer;
end record;
+
function "<" (A, B : in Node) return Boolean is
begin
return A.X < B.X or (A.X = B.X and A.Y < B.Y);
end "<";
+
package Node_Sets is new Ada.Containers.Ordered_Sets
(Element_Type => Node);
package Node_To_Node_Maps is new Ada.Containers.Ordered_Maps
@@ -30,11 +32,13 @@ package body Pathfinding is
subtype G_Score is Integer;
subtype F_Score is Integer;
+
package G_Score_Maps is new Ada.Containers.Ordered_Maps
(Key_Type => Node, Element_Type => G_Score);
package F_Score_Maps is new Ada.Containers.Ordered_Maps
(Key_Type => Node, Element_Type => F_Score);
+
G_Scores : G_Score_Maps.Map := G_Score_Maps.Empty_Map;
F_Scores : F_Score_Maps.Map := F_Score_Maps.Empty_Map;
@@ -135,8 +139,8 @@ package body Pathfinding is
is
use type Node_Sets.Set;
- Start : Node := (X => SX, Y => SY);
- Goal : Node := (X => FX, Y => FY);
+ Start : constant Node := (X => SX, Y => SY);
+ Goal : constant Node := (X => FX, Y => FY);
Current : Node;
Closed_Set : Node_Sets.Set := Node_Sets.Empty_Set;
@@ -191,3 +195,4 @@ package body Pathfinding is
end Pathfinding;
+
diff --git a/src/pathfinding.ads b/src/pathfinding.ads
index 624c5d6..eec079f 100644
--- a/src/pathfinding.ads
+++ b/src/pathfinding.ads
@@ -16,8 +16,9 @@ package Pathfinding is
SX, SY : in Integer;
FX, FY : in Integer)
return Moves.Path
- with Pre => My_Grid.In_Bounds (SX, SY);
+ with Pre => My_Grid.In_Bounds (SX, SY);
end Pathfinding;
+
diff --git a/src/sokoban.adb b/src/sokoban.adb
index 8b7ecdb..1f85266 100644
--- a/src/sokoban.adb
+++ b/src/sokoban.adb
@@ -46,22 +46,26 @@ package body Sokoban is
type Game_State is (Loading, Play, Complete);
- Loading_Message : String := "Loading...";
+ Loading_Message : constant String :=
+ "Loading...";
- Play_Message : String := "Move with the arrow keys. Press u for undo." & ASCII.LF &
+ Play_Message : constant String :=
+ "Move with the arrow keys. Press u for undo." & ASCII.LF &
"To move to a square where there is a clear path, click with the mouse." & ASCII.LF &
"Press n to skip this level, q to quit, and r to restart this level.";
- Complete_Message : String := "Level complete!" & ASCII.LF &
+ Complete_Message : constant String :=
+ "Level complete!" & ASCII.LF &
"Press enter to progress to the next level, or q to quit.";
- Fully_Complete_Message : String := "Congratulations! All levels complete!" & ASCII.LF &
+ Fully_Complete_Message : constant String :=
+ "Congratulations! All levels complete!" & ASCII.LF &
"Press enter to start again at the first level, or q to quit.";
- U_Key : FLTK.Key_Combo := FLTK.Press ('u');
- N_Key : FLTK.Key_Combo := FLTK.Press ('n');
- R_Key : FLTK.Key_Combo := FLTK.Press ('r');
- Q_Key : FLTK.Key_Combo := FLTK.Press ('q');
+ U_Key : constant FLTK.Key_Combo := FLTK.Press ('u');
+ N_Key : constant FLTK.Key_Combo := FLTK.Press ('n');
+ R_Key : constant FLTK.Key_Combo := FLTK.Press ('r');
+ Q_Key : constant FLTK.Key_Combo := FLTK.Press ('q');
@@ -88,7 +92,7 @@ package body Sokoban is
use Ada.Strings, Ada.Strings.Fixed, Ada.Strings.Maps;
Data_File : File_Type;
- Filename : String := ADir.Compose
+ Filename : constant String := ADir.Compose
(Misc.Level_Path, "level" & Trim (LevelID'Image (Number), Both) & ".data");
Rows, Cols : Natural;
@@ -99,8 +103,8 @@ package body Sokoban is
Open (Data_File, In_File, Filename);
declare
- Row_Line : String := Get_Line (Data_File);
- Col_Line : String := Get_Line (Data_File);
+ Row_Line : constant String := Get_Line (Data_File);
+ Col_Line : constant String := Get_Line (Data_File);
Start, Finish : Natural;
begin
Find_Token (Row_Line, To_Set ("0123456789"), 1, Inside, Start, Finish);
@@ -112,7 +116,7 @@ package body Sokoban is
for Y in Integer range 1 .. Rows loop
declare
- Working_Line : String := Get_Line (Data_File);
+ Working_Line : constant String := Get_Line (Data_File);
begin
for X in Integer range 1 .. Cols loop
Add_New_Grid_Item (X, Y, Working_Line (X));
@@ -132,16 +136,12 @@ package body Sokoban is
end Load_Level;
-
-
procedure Show is
begin
My_Display.Show;
end Show;
-
-
procedure Hide is
begin
My_Display.Hide;
@@ -202,8 +202,6 @@ package body Sokoban is
end Keypress;
-
-
function Mouseclick
(X, Y : in Integer)
return FLTK.Event_Outcome
@@ -246,45 +244,43 @@ package body Sokoban is
Temp : Squares.Square;
begin
case Char is
- when '#' =>
- My_Grid.Set_Square (X, Y, Squares.Wall);
+ when '#' =>
+ My_Grid.Set_Square (X, Y, Squares.Wall);
- when ' ' =>
- My_Grid.Set_Square (X, Y, Squares.Empty);
+ when ' ' =>
+ My_Grid.Set_Square (X, Y, Squares.Empty);
- when '$' =>
- Temp := Squares.Empty;
- Temp.Set_Contents (Things.Treasure);
- My_Grid.Set_Square (X, Y, Temp);
+ when '$' =>
+ Temp := Squares.Empty;
+ Temp.Set_Contents (Things.Treasure);
+ My_Grid.Set_Square (X, Y, Temp);
- when '*' =>
- Temp := Squares.Goal;
- Temp.Set_Contents (Things.Treasure);
- My_Grid.Set_Square (X, Y, Temp);
+ when '*' =>
+ Temp := Squares.Goal;
+ Temp.Set_Contents (Things.Treasure);
+ My_Grid.Set_Square (X, Y, Temp);
- when '!' =>
- My_Grid.Set_Square (X, Y, Squares.Space);
+ when '!' =>
+ My_Grid.Set_Square (X, Y, Squares.Space);
- when '.' =>
- My_Grid.Set_Square (X, Y, Squares.Goal);
- Goals_Remaining := Goals_Remaining + 1;
+ when '.' =>
+ My_Grid.Set_Square (X, Y, Squares.Goal);
+ Goals_Remaining := Goals_Remaining + 1;
- when '@' =>
- Temp := Squares.Empty;
- Temp.Set_Contents (Things.Man);
- My_Grid.Set_Square (X, Y, Temp);
- Current_Man_X := X;
- Current_Man_Y := Y;
+ when '@' =>
+ Temp := Squares.Empty;
+ Temp.Set_Contents (Things.Man);
+ My_Grid.Set_Square (X, Y, Temp);
+ Current_Man_X := X;
+ Current_Man_Y := Y;
- when others =>
- raise Program_Error;
+ when others =>
+ raise Program_Error;
end case;
end Add_New_Grid_Item;
-
-
procedure Move_Man
(Delta_X, Delta_Y : in Integer)
is
@@ -341,11 +337,9 @@ package body Sokoban is
end Move_Man;
-
-
procedure Undo_Movement
is
- Last : Moves.Move := Move_Record.Latest;
+ Last : constant Moves.Move := Move_Record.Latest;
Prev : Squares.Square :=
My_Grid.Get_Square (Current_Man_X - Last.Delta_X, Current_Man_Y - Last.Delta_Y);
@@ -382,3 +376,4 @@ begin
end Sokoban;
+
diff --git a/src/sokoban.ads b/src/sokoban.ads
index 1f7fa17..64d8b6e 100644
--- a/src/sokoban.ads
+++ b/src/sokoban.ads
@@ -11,9 +11,6 @@ package Sokoban is
procedure Load_Level
(Number : in LevelID);
-
-
-
procedure Show;
procedure Hide;
@@ -21,3 +18,4 @@ package Sokoban is
end Sokoban;
+
diff --git a/src/squares.adb b/src/squares.adb
index 91b1bd1..fbb04c6 100644
--- a/src/squares.adb
+++ b/src/squares.adb
@@ -54,3 +54,4 @@ package body Squares is
end Squares;
+
diff --git a/src/squares.ads b/src/squares.ads
index 8a1f053..5ff9b81 100644
--- a/src/squares.ads
+++ b/src/squares.ads
@@ -40,12 +40,12 @@ package Squares is
function Get_Contents
(This : in Square)
return Things.Thing
- with Pre => Is_Walkable (This);
+ with Pre => Is_Walkable (This);
procedure Set_Contents
(This : in out Square;
Item : in Things.Thing)
- with Pre => Is_Walkable (This);
+ with Pre => Is_Walkable (This);
@@ -91,3 +91,4 @@ private
end Squares;
+
diff --git a/src/things.adb b/src/things.adb
index 66cc07e..0a14fa3 100644
--- a/src/things.adb
+++ b/src/things.adb
@@ -25,3 +25,4 @@ package body Things is
end Things;
+
diff --git a/src/things.ads b/src/things.ads
index 97889ba..ddd4bb4 100644
--- a/src/things.ads
+++ b/src/things.ads
@@ -37,7 +37,7 @@ private
end record;
- Man_Image : aliased FLTK.Images.RGB.PNG.PNG_Image := FLTK.Images.RGB.PNG.Forge.Create
+ Man_Image : aliased FLTK.Images.RGB.PNG.PNG_Image := FLTK.Images.RGB.PNG.Forge.Create
(Ada.Directories.Compose (Misc.Image_Path, "man.png"));
Treasure_Image : aliased FLTK.Images.RGB.PNG.PNG_Image := FLTK.Images.RGB.PNG.Forge.Create
(Ada.Directories.Compose (Misc.Image_Path, "treasure.png"));
@@ -47,7 +47,7 @@ private
(Self_Image => null);
- Man : constant Thing :=
+ Man : constant Thing :=
(Self_Image => Man_Image'Access);
Treasure : constant Thing :=
(Self_Image => Treasure_Image'Access);
@@ -55,3 +55,4 @@ private
end Things;
+