private void CreateHeap() { directionHeap.ResetHeap(); BestDirectionHeapNode rootNode = new BestDirectionHeapNode(Directions.NoDirection, GetManhattanDistanceFromGhosts(thePacman.image.Location), thePacman.image.Location, null); AddDirectionsToHeap(rootNode, 0); }
public BestDirectionHeapNode GetBestNode() { BestDirectionHeapNode cd = heapNodes[0]; string message = ""; while (cd.parentNode != null) { message += "|" + cd.direction.ToString() + cd.statementValue.ToString(); cd = cd.parentNode; } Console.WriteLine(message + "||" + cd.statementValue); return(heapNodes[0]); }
public BestDirectionHeapNode(Directions direction, int manhattanDistance, Point location, BestDirectionHeapNode parentNode) { this.direction = direction; this.manhattanDistance = manhattanDistance; this.parentNode = parentNode; this.location = location; if (parentNode != null) { statementValue = manhattanDistance + parentNode.manhattanDistance; } else { statementValue = manhattanDistance; } }
public Directions FindBestDirection() { CreateHeap(); BestDirectionHeapNode bestNode = directionHeap.GetBestNode(); while (bestNode.parentNode.parentNode != null) { bestNode = bestNode.parentNode; } if (bestNode.statementValue > 100) { Console.WriteLine(GetDirectionForFood(FindNearestFoodLocation(thePacman.image.Location))); return(GetDirectionForFood(FindNearestFoodLocation(thePacman.image.Location))); } return(bestNode.direction); }
private void MoveToUp(int index) { int parent = (index - 1) / 2; BestDirectionHeapNode bottom = heapNodes[index]; while (index > 0 && heapNodes[parent].statementValue <= bottom.statementValue) { if (heapNodes[parent].statementValue == bottom.statementValue && heapNodes[parent].direction > bottom.direction) { break; } heapNodes[index] = heapNodes[parent]; index = parent; parent = (parent - 1) / 2; } heapNodes[index] = bottom; }
private void AddDirectionsToHeap(BestDirectionHeapNode parentNode, int depthLevel) { if (depthLevel < maxDepthLevel) { for (int directionRecorder = 0; directionRecorder < (int)Directions.NoDirection; directionRecorder++) { Point childLocation = mapObjectOperator.GetTargetMovingArea((Directions)directionRecorder, parentNode.location); if (!mapObjectOperator.IsWall(childLocation)) { BestDirectionHeapNode newNode = new BestDirectionHeapNode((Directions)directionRecorder, GetManhattanDistanceFromGhosts(childLocation), childLocation, parentNode); AddDirectionsToHeap(newNode, depthLevel + 1); } } } else { directionHeap.AddNode(parentNode); } }
public void AddNode(BestDirectionHeapNode newNode) { heapNodes.Add(newNode); MoveToUp(currentSize++); }