private IEnumerable<Node> getNeighbours(Node node) { var neighbours = new List<Node>(); for (int row = -1; row <= 1; row++) { for (int col = -1; col <= 1; col++) { //skip the current node if (row == 0 && col == 0) { continue; } var currentRow = node.Location.Row + row; var currentCol = node.Location.Column + col; //check bounds if (currentRow < 0 || currentCol < 0 || currentRow >= Map.Count || currentCol >= Map[currentRow].Count()) { continue; } neighbours.Add(Map[currentRow][currentCol]); } } return neighbours; }
private double getCost(Node from, Node to) { var cost = 0.0; if (from.SpecialCost != 0) { cost = from.SpecialCost; } var areOnTheSameLine = from.Location.Row == to.Location.Row || from.Location.Column == to.Location.Column; if (areOnTheSameLine) { cost += MapConstants.StraightMoveCost; } else { cost += MapConstants.DiagonalMoveCost; } return cost; }
private IEnumerable<Point> tracePath(Node lastNode) { var path = new Stack<Point>(); path.Push(lastNode.Location); while (lastNode.Parent != null) { lastNode = lastNode.Parent; path.Push(lastNode.Location); } return path.AsEnumerable(); }
private double getDistance(Node firstNode, Node secondNode) { var rowDistance = Math.Abs(firstNode.Location.Row - secondNode.Location.Row); var colDistance = Math.Abs(firstNode.Location.Column - secondNode.Location.Column); if (rowDistance < colDistance) { return MapConstants.DiagonalMoveCost * rowDistance + MapConstants.StraightMoveCost * (colDistance - rowDistance); } return MapConstants.DiagonalMoveCost * colDistance + MapConstants.StraightMoveCost * (rowDistance - colDistance); }