Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class DFS : MonoBehaviour
  7. {
  8.  
  9.     public List<int> Route = new List<int>(); //List of Integers from one node to the other
  10.     public List<bool> Visited = new List<bool>(); //Stores if they have been stored or not
  11.     public List<int> CalculatedPath = new List<int>();  //Makes Path work
  12.     public Graph Map;
  13.     public List<int> CalculatePath(GraphNode Source, GraphNode Target) //Coding the route
  14.     {
  15.         List<int> Path = new List<int>();
  16.         int currentNode = Target.index;
  17.         Path.Add(currentNode);
  18.         while (currentNode != Source.index)
  19.         {
  20.             currentNode = Route[currentNode];
  21.             Path.Add(currentNode);
  22.         }
  23.         return Path;
  24.     }
  25.     public bool CalculateRoute(GraphNode Source, GraphNode Target)
  26.     {
  27.         Stack<GraphEdge> graphEdges = new Stack<GraphEdge>(); //Thats the stack
  28.         Route = new List<int>(Map.Nodes.Count);
  29.         Visited = new List<bool>(Map.Nodes.Count);
  30.         for (int i = 0; i < Map.Nodes.Count; i++)// Looping through the map node
  31.         {
  32.             Route.Add(-10);
  33.             Visited.Add(false); //This initalises both the lists,
  34.         }
  35.         for (int i = 0; i < Source.AdjacencyList.Length; i++) //LIFO
  36.             graphEdges.Push(Source.AdjacencyList[i]); //Adding all the adjactency edges to the start node
  37.         Visited[Source.index] = true;
  38.         while (graphEdges.Count > 0)
  39.         {
  40.             GraphEdge edge = graphEdges.Pop();
  41.             Route[edge.to.index] = edge.from.index;
  42.             Visited[edge.to.index] = true;
  43.             for (int i = 0; i < Source.AdjacencyList.Length; i++) //Attempting LIFO, Arrays have .length rather than .count
  44.                 Visited[edge.to.index] = true;
  45.  
  46.             if (edge.to.index == Target.index)
  47.             {
  48.                 CalculatedPath = CalculatePath(Source, Target);
  49.                 for (int i = 0; i < CalculatedPath.Count - 1; i++)
  50.                 {
  51.                     // This draws the line (Somewhat poorly if you add a number more than one but heyho)
  52.                     //From what Ive been testing out, it does a path via how many lines I put in next to the Array and it gets really scruffy.
  53.                 }
  54.                 return true;   //This is where I aim to generate the path and for it to come out true
  55.             }
  56.             for (int i = 0; i < edge.to.AdjacencyList.Length; i++)
  57.                 if (!Visited[edge.to.AdjacencyList[i].to.index])
  58.                     graphEdges.Push(edge.to.AdjacencyList[i]);
  59.         }
  60.         return false;
  61.     }
  62. }
  63. //Note, even though I understand this now, most of it was cause of the help of Kirean, Work on it when you get home, it does lines now but not very well
  64. //What Im trying to say is it does lines but does it poorly
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement