public List <Hex> GetPath(Hex start, Hex dest) { OpenList = new List <Shortest_Path>(); ClosedList = new List <Shortest_Path>(); List <Hex> rtnVal = new List <Hex>(); int H = GetDistance(start, dest); Shortest_Path p = new Shortest_Path(null, start, 0, H); ClosedList.Add(p); Shortest_Path result = Recursive_FindPath(p, dest); //p는 시작점. if (result == null) { Debug.Break(); } //if(result == null) //{ // return rtnVal; //} while (result.Parent != null) { rtnVal.Insert(0, result.CurHex); //역순으로 들어가는것이기에, 0번인덱스에 추가하는것으로. //rtnVal.Add(result.CurHex); result = result.Parent; } return(rtnVal); }
public Shortest_Path Recursive_FindPath(Shortest_Path parent, Hex dest) { if (parent.CurHex.MapPos == dest.MapPos) { return(parent); //목적지를 찾은경우. } List <Hex> neighbors = GetNeightbors(parent.CurHex); foreach (Hex h in neighbors) { Shortest_Path newP = new Shortest_Path(parent, h, parent.G + 1, GetDistance(h, dest)); AddToOpenList(newP); } Shortest_Path bestP; if (OpenList.Count == 0) { return(null); //목적지까지 가는길이 없는경우 } bestP = OpenList[0]; foreach (Shortest_Path p in OpenList) { if (p.F < bestP.F) { bestP = p; } } OpenList.Remove(bestP); ClosedList.Add(bestP); return(Recursive_FindPath(bestP, dest)); }
public int H; //현재부터 도착점까지의 거리값. public Shortest_Path(Shortest_Path parent, Hex hex, int g, int h) { Parent = parent; CurHex = hex; G = g; H = h; F = H + G; }
public void AddToOpenList(Shortest_Path p) { foreach (Shortest_Path inP2 in ClosedList) { if (p.CurHex.MapPos == inP2.CurHex.MapPos) { return; } } foreach (Shortest_Path inP in OpenList) { if (p.CurHex.MapPos == inP.CurHex.MapPos) { if (p.F < inP.F) { OpenList.Remove(inP); OpenList.Add(p); return; } } } OpenList.Add(p); }