Advertisement
saberxv9

Slacky Maze Challenge

Jun 24th, 2023 (edited)
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.55 KB | None | 0 0
  1. {$I SRL-T/osr.simba}
  2.  
  3. type
  4.   TPArray = record
  5.     X, Y: Integer;
  6.   end;
  7.  
  8.   TVisitedCell = record
  9.     Distance: Integer;
  10.     PreviousCell: TPArray;
  11.   end;
  12.  
  13. var
  14.   bmp: TMufasaBitmap;  // Bitmap (to load image into)
  15.   start: TPArray;  // Start point
  16.   stop: TPArray;   // End point
  17.   visited: array of array of TVisitedCell;
  18.  
  19. function IsInBounds(const point: TPArray): Boolean; // Make sure not to go outside of the maze image
  20. begin
  21.   Result := (point.X >= 0) and (point.X < bmp.GetWidth()) and (point.Y >= 0) and (point.Y < bmp.GetHeight());
  22. end;
  23.  
  24. function LoadMazeFromBitmap: array of array of Integer;
  25. var
  26.   x, y: Integer;
  27. begin
  28.   SetLength(Result, bmp.GetWidth(), bmp.GetHeight());
  29.   for y := 0 to bmp.GetHeight() - 1 do
  30.   begin
  31.     for x := 0 to bmp.GetWidth() - 1 do
  32.     begin
  33.       if bmp.GetPixel(x, y) = clBlack then
  34.         Result[x, y] := 1 // Wall of the maze
  35.       else
  36.         Result[x, y] := 0; // Open area that's moveable in
  37.     end;
  38.   end;
  39. end;
  40.  
  41. function FindShortestPath(const maze: array of array of Integer): array of TPArray;
  42. var
  43.   directions: array[0..3] of TPArray; // N/E/S/W movement
  44.   queue: array[0..629*479] of TPArray; // Hard-coded maximum size of the maze xD
  45.   queueHead: Integer;   // Leading
  46.   queueTail: Integer;   // Trailing
  47.   current, next: TPArray;
  48.   x, y, i: Integer;
  49. begin
  50.   SetLength(visited, bmp.GetWidth(), bmp.GetHeight());
  51.   for y := 0 to bmp.GetHeight() - 1 do
  52.   begin
  53.     for x := 0 to bmp.GetWidth() - 1 do
  54.     begin
  55.       visited[x, y].Distance := -1;
  56.       visited[x, y].PreviousCell.X := -1;
  57.       visited[x, y].PreviousCell.Y := -1;
  58.     end;
  59.   end;
  60.  
  61.   directions[0].X := 1;
  62.   directions[0].Y := 0;
  63.   directions[1].X := -1;
  64.   directions[1].Y := 0;
  65.   directions[2].X := 0;
  66.   directions[2].Y := 1;
  67.   directions[3].X := 0;
  68.   directions[3].Y := -1;
  69.  
  70.   queueHead := 0;
  71.   queueTail := 0;
  72.   queue[queueTail].X := start.X;
  73.   queue[queueTail].Y := start.Y;
  74.   Inc(queueTail);
  75.   visited[start.X, start.Y].Distance := 0;
  76.  
  77.   while queueHead < queueTail do
  78.   begin
  79.     current := queue[queueHead];
  80.     Inc(queueHead);
  81.  
  82.     if (current.X = stop.X) and (current.Y = stop.Y) then
  83.       Break;
  84.  
  85.     for i := 0 to 3 do
  86.     begin
  87.       next.X := current.X + directions[i].X;
  88.       next.Y := current.Y + directions[i].Y;
  89.  
  90.       if IsInBounds(next) and (maze[next.X, next.Y] = 0) and (visited[next.X, next.Y].Distance = -1) then
  91.       begin
  92.         visited[next.X, next.Y].Distance := visited[current.X, current.Y].Distance + 1;
  93.         visited[next.X, next.Y].PreviousCell := current;
  94.         queue[queueTail].X := next.X;
  95.         queue[queueTail].Y := next.Y;
  96.         Inc(queueTail);
  97.       end;
  98.     end;
  99.   end;
  100.  
  101.   if (visited[stop.X, stop.Y].Distance = -1) then
  102.   begin
  103.     SetLength(Result, 0); // No path found, for debugging
  104.     Exit;
  105.   end;
  106.  
  107.   SetLength(Result, visited[stop.X, stop.Y].Distance + 1);
  108.   current := stop;
  109.   for i := visited[stop.X, stop.Y].Distance downto 0 do
  110.   begin
  111.     Result[i] := current;
  112.     current := visited[current.X, current.Y].PreviousCell;
  113.   end;
  114. end;
  115.  
  116. procedure MarkPathOnBitmap(const path: array of TPArray); // Visualize the path on the bitmap
  117. var
  118.   i: Integer;
  119. begin
  120.   for i := 0 to Length(path) - 1 do
  121.     bmp.SetPixel(path[i].X, path[i].Y, clBlue);
  122. end;
  123.  
  124. begin
  125.   bmp.Init();
  126.   bmp.LoadFromFile('A PLACE ON MY COMPUTER U CHEEKY GOON'); // Hi, it's Saber from Discord :)
  127.  
  128.   start.X := 0;
  129.   start.Y := 0;
  130.   stop.X := 630;
  131.   stop.Y := 480;
  132.  
  133.   MarkPathOnBitmap(FindShortestPath(LoadMazeFromBitmap));
  134.   bmp.Debug();
  135.  
  136.   bmp.Free();
  137. end.
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement