1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
-- Programmed by Jedidiah Barber
-- Licensed under the Sunset License v1.0
-- See license.txt for further details
with
Ada.Directories,
Ada.Strings.Fixed,
Ada.Strings.Maps,
Ada.Text_IO,
Data,
Displays,
FLTK.Widgets,
Grids,
Moves,
Pathfinding,
Squares,
Things;
use
Ada.Text_IO;
package body Sokoban is
package ADir renames Ada.Directories;
-- Forward declarations of helper functions.
procedure Add_New_Grid_Item
(X, Y : in Natural;
Char : in Character);
procedure Move_Man
(Delta_X, Delta_Y : in Integer);
procedure Undo_Movement;
-- Miscellaneous game data.
type Game_State is (Loading, Play, Complete);
Loading_Message : constant String :=
"Loading...";
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 : constant String :=
"Level complete!" & ASCII.LF &
"Press enter to progress to the next level, or q to quit.";
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 : 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');
-- Global state of the game.
My_Display : Displays.Display := Displays.Create;
My_Grid : Grids.Grid := Grids.Create (0, 0);
Current_Level : LevelID;
Level_State : Game_State := Loading;
Current_Man_X : Integer;
Current_Man_Y : Integer;
Move_Record : Moves.Path;
Goals_Remaining : Integer;
-- Main program interface.
procedure Load_Level
(Number : in LevelID)
is
use Ada.Strings, Ada.Strings.Fixed, Ada.Strings.Maps;
Data_File : File_Type;
Filename : constant String := ADir.Compose
(Data.Level_Path, "level" & Trim (LevelID'Image (Number), Both) & ".data");
Rows, Cols : Natural;
begin
Level_State := Loading;
My_Display.Set_Message (Loading_Message);
Goals_Remaining := 0;
Open (Data_File, In_File, Filename);
declare
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);
Rows := Natural'Value (Row_Line (Start .. Finish));
Find_Token (Col_Line, To_Set ("0123456789"), 1, Inside, Start, Finish);
Cols := Natural'Value (Col_Line (Start .. Finish));
end;
My_Display.Adjust_Grid (Cols, Rows);
for Y in Integer range 1 .. Rows loop
declare
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));
end loop;
end;
end loop;
Close (Data_File);
Current_Level := Number;
My_Display.Centre_On_Screen;
Move_Record := Moves.Empty_Path;
My_Display.Set_Message (Play_Message);
My_Display.Set_Level_Number (Natural (Current_Level));
My_Display.Set_Move_Number (Move_Record.Length);
My_Grid.Redraw;
Level_State := Play;
end Load_Level;
procedure Show is
begin
My_Display.Show;
end Show;
procedure Hide is
begin
My_Display.Hide;
end Hide;
-- Keyboard and mouse control handling.
function Keypress
(Key : in FLTK.Key_Combo)
return FLTK.Event_Outcome
is
use type FLTK.Key_Combo;
begin
if Key = Q_Key then
Hide;
return FLTK.Handled;
end if;
if Level_State = Play then
if Key = FLTK.Press (FLTK.Up_Key) then
Move_Man (0, -1);
elsif Key = FLTK.Press (FLTK.Down_Key) then
Move_Man (0, 1);
elsif Key = FLTK.Press (FLTK.Left_Key) then
Move_Man (-1, 0);
elsif Key = FLTK.Press (FLTK.Right_Key) then
Move_Man (1, 0);
elsif Key = U_Key then
if Move_Record.Length > 0 then
Undo_Movement;
end if;
elsif Key = N_Key then
if Current_Level = LevelID'Last then
My_Display.Set_Message (Fully_Complete_Message);
Level_State := Complete;
else
Load_Level (Current_Level + 1);
end if;
elsif Key = R_Key then
Load_Level (Current_Level);
else
return FLTK.Not_Handled;
end if;
return FLTK.Handled;
elsif Level_State = Complete and Key = FLTK.Press (FLTK.Enter_Key) then
if Current_Level = LevelID'Last then
Load_Level (LevelID'First);
else
Load_Level (Current_Level + 1);
end if;
return FLTK.Handled;
else
return FLTK.Not_Handled;
end if;
end Keypress;
function Mouseclick
(X, Y : in Integer)
return FLTK.Event_Outcome
is
Col, Row, DX, DY : Integer;
Movement : Moves.Path;
Temp : Squares.Square;
begin
My_Grid.Pixel_To_Colrow (X, Y, Col, Row);
if My_Grid.In_Bounds (Col, Row) then
Movement := Pathfinding.A_Star (My_Grid, Current_Man_X, Current_Man_Y, Col, Row);
Movement.Total_Delta (DX, DY);
Temp := My_Grid.Get_Square (Current_Man_X, Current_Man_Y);
Temp.Set_Contents (Things.Nothing);
My_Grid.Set_Square (Current_Man_X, Current_Man_Y, Temp);
Current_Man_X := Current_Man_X + DX;
Current_Man_Y := Current_Man_Y + DY;
Temp := My_Grid.Get_Square (Current_Man_X, Current_Man_Y);
Temp.Set_Contents (Things.Man);
My_Grid.Set_Square (Current_Man_X, Current_Man_Y, Temp);
Move_Record.Add (Movement);
My_Display.Set_Move_Number (Move_Record.Length);
My_Grid.Redraw;
return FLTK.Handled;
end if;
return FLTK.Not_Handled;
end Mouseclick;
-- Helper functions.
procedure Add_New_Grid_Item
(X, Y : in Natural;
Char : in Character)
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.Empty);
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 '!' =>
My_Grid.Set_Square (X, Y, Squares.Space);
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 others =>
raise Program_Error;
end case;
end Add_New_Grid_Item;
procedure Move_Man
(Delta_X, Delta_Y : in Integer)
is
use type Squares.Square, Things.Thing;
Current : Squares.Square :=
My_Grid.Get_Square (Current_Man_X, Current_Man_Y);
Next : Squares.Square :=
My_Grid.Get_Square (Current_Man_X + Delta_X, Current_Man_Y + Delta_Y);
Next_Next : Squares.Square :=
My_Grid.Get_Square (Current_Man_X + Delta_X * 2, Current_Man_Y + Delta_Y * 2);
begin
if Next.Is_Walkable then
if Next.Get_Contents = Things.Nothing then
Current.Set_Contents (Things.Nothing);
My_Grid.Set_Square (Current_Man_X, Current_Man_Y, Current);
Current_Man_X := Current_Man_X + Delta_X;
Current_Man_Y := Current_Man_Y + Delta_Y;
Next.Set_Contents (Things.Man);
My_Grid.Set_Square (Current_Man_X, Current_Man_Y, Next);
Move_Record.Add ((Delta_X => Delta_X, Delta_Y => Delta_Y, Push => False));
My_Display.Set_Move_Number (Move_Record.Length);
My_Grid.Redraw;
elsif Next.Get_Contents = Things.Treasure and then
Next_Next.Is_Walkable and then Next_Next.Get_Contents = Things.Nothing
then
Current.Set_Contents (Things.Nothing);
My_Grid.Set_Square (Current_Man_X, Current_Man_Y, Current);
Current_Man_X := Current_Man_X + Delta_X;
Current_Man_Y := Current_Man_Y + Delta_Y;
Next.Set_Contents (Things.Man);
My_Grid.Set_Square (Current_Man_X, Current_Man_Y, Next);
Next_Next.Set_Contents (Things.Treasure);
My_Grid.Set_Square (Current_Man_X + Delta_X, Current_Man_Y + Delta_Y, Next_Next);
Move_Record.Add ((Delta_X => Delta_X, Delta_Y => Delta_Y, Push => True));
My_Display.Set_Move_Number (Move_Record.Length);
My_Grid.Redraw;
if Next = Squares.Goal and Next_Next /= Squares.Goal then
Goals_Remaining := Goals_Remaining + 1;
elsif Next /= Squares.Goal and Next_Next = Squares.Goal then
Goals_Remaining := Goals_Remaining - 1;
end if;
if Goals_Remaining = 0 then
My_Display.Set_Message (Complete_Message);
Level_State := Complete;
end if;
end if;
end if;
end Move_Man;
procedure Undo_Movement
is
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);
Current : Squares.Square :=
My_Grid.Get_Square (Current_Man_X, Current_Man_Y);
Next : Squares.Square :=
My_Grid.Get_Square (Current_Man_X + Last.Delta_X, Current_Man_Y + Last.Delta_Y);
begin
if Last.Push then
Current.Set_Contents (Things.Treasure);
Next.Set_Contents (Things.Nothing);
else
Current.Set_Contents (Things.Nothing);
end if;
Prev.Set_Contents (Things.Man);
My_Grid.Set_Square (Current_Man_X, Current_Man_Y, Current);
My_Grid.Set_Square (Current_Man_X + Last.Delta_X, Current_Man_Y + Last.Delta_Y, Next);
My_Grid.Set_Square (Current_Man_X - Last.Delta_X, Current_Man_Y - Last.Delta_Y, Prev);
Current_Man_X := Current_Man_X - Last.Delta_X;
Current_Man_Y := Current_Man_Y - Last.Delta_Y;
Move_Record.Drop_Latest;
My_Display.Set_Move_Number (Move_Record.Length);
My_Grid.Redraw;
end Undo_Movement;
begin
My_Display.Set_Grid (My_Grid);
My_Display.Set_Keyboard_Callback (Keypress'Access);
My_Display.Set_Mouse_Callback (Mouseclick'Access);
end Sokoban;
|