private ANode[,] GetMap(string[,] map) { int row = map.GetLength(0); int col = map.GetLength(1); ANode[,] aNodes = new ANode[row, col]; ANode aNode; bool isobs = true;; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { switch (map[i, j]) { case "M1": case "M2": case "M3": isobs = false; break; default: isobs = true; break; } aNode = new ANode(new Point(i, j), 0, 0, isobs, null) { Id = map[i, j] }; aNodes[i, j] = aNode; } } return(aNodes); }
public ANode(Point point, int gc, int hc, bool obs, ANode parentNode) { this.GCost = gc; this.HCost = hc; this.FCost = this.GCost + this.HCost; this.Obstacles = obs; this.Parent = parentNode; this.Position = point; }
public ANode Star(Point ownpoint, Point tarPoint, ANode[,] map) { endPoint = tarPoint; starPoint = ownpoint; openList.Add(map[ownpoint.X, ownpoint.Y]); int row = map.GetLength(0); int col = map.GetLength(1); int nextX, nextY; ANode aNode = null; do { openList.Sort(); if (openList.Count == 0) { return(map[ownpoint.X, ownpoint.Y]); } aNode = openList[0]; openList.Remove(aNode); closeList.Add(aNode); if (aNode.Position == tarPoint) { break; } //左 nextY = aNode.Position.Y - 1; if (nextY >= 0) { AddOpenList(map[aNode.Position.X, nextY], aNode); } //右 nextY = aNode.Position.Y + 1; if (nextY < col) { AddOpenList(map[aNode.Position.X, nextY], aNode); } //上 nextX = aNode.Position.X - 1; if (nextX >= 0) { AddOpenList(map[nextX, aNode.Position.Y], aNode); } //下 nextX = aNode.Position.X + 1; if (nextX < row) { AddOpenList(map[nextX, aNode.Position.Y], aNode); } } while (true); return(aNode); }
private void AddOpenList(ANode nextNode, ANode nowNode) { if (nextNode.Obstacles) { return; } if (closeList.Contains(nextNode)) { return; } if (openList.Contains(nextNode)) { return; } openList.Add(nextNode); nextNode.GCost = nowNode.GCost + GetCost(nextNode.Position.X, nextNode.Position.Y, nowNode.Position.X, nowNode.Position.Y); nextNode.HCost = GetCost(nextNode.Position.X, nextNode.Position.Y, endPoint.X, endPoint.Y); nextNode.FCost = nextNode.GCost + nextNode.HCost; nextNode.Parent = nowNode; }
public int CompareTo(object obj) { if (obj == null) { return(1); } ANode aNode = obj as ANode; if (this.FCost.CompareTo(aNode.FCost) != 0) { return(this.FCost.CompareTo(aNode.FCost)); } else if (this.HCost.CompareTo(aNode.HCost) != 0) { return(this.HCost.CompareTo(aNode.HCost)); } else { return(1); } }