Exemplo n.º 1
0
        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;
        }
Exemplo n.º 2
0
        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;
        }
Exemplo n.º 3
0
        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();
        }
Exemplo n.º 4
0
        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);
        }