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
|
unit MazeUnit;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils;
type
Location = record
left_wall, up_wall: Boolean;
end;
Maze = array of array of Location;
(* Загрузить лабиринт. *)
procedure LoadMaze(var TheMaze: Maze; FileName: string);
(* Сохранить лабиринт *)
procedure SaveMaze(TheMaze: Maze; FileName: string);
function CanGo(TheMaze: Maze; x, y, dx, dy: Integer): Boolean;
implementation
procedure LoadMaze(var TheMaze: Maze; FileName: string);
var f: TextFile; (* Файл с описанием лабиринта *)
Height, Width: Integer; (* высота и ширина лабиринта *)
x, y: Integer; (* текущая локация *)
lw, uw: Integer; (* временные переменные *)
begin
AssignFile(f, FileName); (* открыть файл *)
Reset(f);
ReadLn(f, Width, Height); (* прочитать высоту и ширину *)
SetLength(TheMaze, Width + 1, Height + 1); (* изменить размер лабиринта *)
for y := 0 to Height do (* Цикл по всем локациям *)
for x := 0 to Width do
if (y = Height) or (x = Width) then (* если локация - служебная *)
begin
TheMaze[x, y].left_wall := true; (* обе стены существуют *)
TheMaze[x, y].up_wall := true
end
else
begin (* иначе считываем *)
ReadLn(f, uw, lw); (* из файла *)
TheMaze[x, y].left_wall := Boolean(lw); (* прочитанное целое *)
TheMaze[x, y].up_wall := Boolean(uw); (* число надо привести *)
end; (* к типу Boolean *)
CloseFile(f); (* Закрыть файл *)
end;
procedure SaveMaze(TheMaze: Maze; FileName: string);
var f: TextFile; (* файл с описанием лабиринта *)
Height, Width: Integer; (* высота и ширина *)
x, y: Integer; (* координаты текущей локации *)
begin
AssignFile(f, FileName); (* открыать файл *)
Rewrite(f); (* для записи *)
Height := High(TheMaze[0]); (* определяем высоту *)
Width := High(TheMaze); (* и ширину лабиринта *)
WriteLn(f, Width, ' ', Height); (* запись в файл высоты и ширины *)
for y := 0 to Height - 1 do
for x := 0 to Width - 1 do
WriteLn(f, Integer(TheMaze[x, y].up_wall), ' ',
Integer(TheMaze[x, y].left_wall));
CloseFile(f); (* закрыть файл *)
end;
(* служебная функция: определяет, можно ли пройти из локации
(x, y) в локацию (x + dx, y + dy), то есть нет ли между ними стены *)
function CanGo(TheMaze: Maze; x, y, dx, dy: Integer): Boolean;
begin
if dx = -1 then CanGo := not TheMaze[x, y].left_wall
else if dx = 1 then CanGo := not TheMaze[x + 1, y].left_wall
else if dy = -1 then CanGo := not TheMaze[x, y].up_wall
else CanGo := not TheMaze[x, y + 1].up_wall;
end;
end.
|