Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. public List<Node> GetPossibleDestinations(Graph graph, Node start, int distanceLimit)
  2. {
  3. var frontier = new Queue<Node>();
  4. frontier.Enqueue(start);
  5. var visited = new List<Node>();
  6. visited.Add(start);
  7.  
  8. while (frontier.Count > 0)
  9. {
  10. var currentNode = frontier.Dequeue();
  11.  
  12. if (currentNode.distance >= distanceLimit)
  13. {
  14. continue;
  15. }
  16.  
  17. var neighbors = graph.GetNeighbours(currentNode);
  18. foreach (var neighbor in neighbors)
  19. {
  20. if (neighbor.nodeType == NodeType.Open && !visited.Contains(neighbor))
  21. {
  22. frontier.Enqueue(neighbor);
  23. visited.Add(neighbor);
  24. neighbor.distance = 1 + currentNode.distance;
  25. }
  26. }
  27. }
  28.  
  29. return visited;
  30. }
  31.  
  32. public List<Node> GetNeighbours(Node node)
  33. {
  34. List<Node> neighbours = new List<Node>();
  35.  
  36. for (int x = -1; x <= 1; x++)
  37. {
  38. for (int y = -1; y <= 1; y++)
  39. {
  40. if (x == 0 && y == 0)
  41. continue;
  42.  
  43. int checkX = node.gridX + x;
  44. int checkY = node.gridY + y;
  45.  
  46. if (checkX >= 0 && checkX < graphSizeX && checkY >= 0 && checkY < graphSizeY)
  47. {
  48. neighbours.Add(nodes[checkX, checkY]);
  49. }
  50. }
  51. }
  52.  
  53. return neighbours;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement