function KruskalGenerateMaze(Width, Height : Integer) : Maze; type Wall = record { тип "стена" } x, y, dx, dy : Integer; end; var TheMaze : Maze; { сам лабиринт } Walls : array of Wall; { массив стен } Temp : array of Real; { временный массив для сортировки стен } i, j : Integer; tempw : Wall; tempr : Real; CurWall : Wall; locations : Integer; counter : Integer; procedure BreakWall(x, y, dx, dy : Integer); { разрушить стену } begin { между локациями } if dx = -1 then TheMaze[x, y].left_wall := false else if dx = 1 then TheMaze[x + 1, y].left_wall := false else if dy = -1 then TheMaze[x, y].up_wall := false else TheMaze[x, y + 1].up_wall := false; end; function IsConnected(xs, ys, xf, yf : Integer) : Boolean; ... { используется алгоритм волновой трассировки } begin { выделение памяти для массива стен } { в лабиринте Width * Height изначально { (Width - 1) * Height + (Height - 1) * Width стен } SetLength(Walls, (Width - 1) * Height + (Height - 1) * Width); SetLength(Temp, (Width - 1) * Height + (Height - 1) * Width); SetLength(TheMaze, Width + 1, Height + 1); { указать размер лабиринта } for i := 0 to Width do { все стены изначально } for j := 0 to Height do { существуют } begin TheMaze[i, j].left_wall := true; TheMaze[i, j].up_wall := true; end; Randomize; for i := 0 to (Width - 1) * Height + (Height - 1) * Width - 1 do Temp[i] := Random; { заполнение массива Temp случайными числами } counter := 0; { заполнение массива стен } for i := 1 to Width - 1 do for j := 0 to Height - 1 do begin { сначала все горизонтальные } Walls[counter].x := i; Walls[counter].y := j; Walls[counter].dx := -1; Walls[counter].dy := 0; counter := counter + 1; end; for i := 0 to Width - 1 do for j := 1 to Height - 1 do begin { затем все вертикальные } Walls[counter].x := i; Walls[counter].y := j; Walls[counter].dx := 0; Walls[counter].dy := -1; counter := counter + 1; end; for i := 0 to (Width - 1) * Height + (Height - 1) * Width - 1 do for j := i to (Width - 1) * Height + (Height - 1) * Width - 1 do if Temp[i] <@062> Temp[j] then { перемешиваем массив стен } begin tempr := Temp[i]; Temp[i] := Temp[j]; Temp[j] := tempr; tempw := Walls[i]; Walls[i] := Walls[j]; Walls[j] := tempw; end; locations := Width * Height; i := 0; while locations > 1 do { прямолинейная реализация } begin { алгоритма Краскала } CurWall := Walls[i]; i := i + 1; if not IsConnected(CurWall.x, CurWall.y, CurWall.x + CurWall.dx, CurWall.y + CurWall.dy) then begin BreakWall(CurWall.x, CurWall.y, CurWall.dx, CurWall.dy); locations := locations - 1; ShowMaze(TheMaze); Application.ProcessMessages; end; end; KruskalGenerateMaze := TheMaze; end;