Files

86 lines
3.1 KiB
ObjectPascal
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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.