Ejemplo n.º 1
0
        private static bool CheckForBackTrack(Point pointToBeChecked, List <Direction> listOfDirections, Point startPoint)
        {
            var currentLocation = new Point(startPoint.GetX(), startPoint.GetY());

            foreach (var direction in listOfDirections)
            {
                switch (direction)
                {
                case Direction.East:
                    currentLocation.SetX(currentLocation.GetX() + 1);
                    break;

                case Direction.North:
                    currentLocation.SetY(currentLocation.GetY() + 1);
                    break;

                case Direction.South:
                    currentLocation.SetY(currentLocation.GetY() - 1);
                    break;

                case Direction.West:
                    currentLocation.SetX(currentLocation.GetX() - 1);
                    break;
                }
                if (pointToBeChecked.GetX().Equals(currentLocation.GetX()) &&
                    pointToBeChecked.GetY().Equals(currentLocation.GetY()))
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        private void StepBranchedSearch(Point startPoint, Point endPoint,
                                        List <PathPossiblity> unfinishedPathPossiblities, ref PathPossiblity finishedDirections)
        {
            for (var i = 0; i < unfinishedPathPossiblities.Count; i++)
            {
                var currentPos = new Point(startPoint.GetX(), startPoint.GetY());
                //Find where we are at along the path
                foreach (var direction in unfinishedPathPossiblities[i].Directions)
                {
                    switch (direction)
                    {
                    case Direction.East:
                        currentPos.SetX(currentPos.GetX() + 1);
                        break;

                    case Direction.North:
                        currentPos.SetY(currentPos.GetY() + 1);
                        break;

                    case Direction.South:
                        currentPos.SetY(currentPos.GetY() - 1);
                        break;

                    case Direction.West:
                        currentPos.SetX(currentPos.GetX() - 1);
                        break;
                    }
                }
                switch (unfinishedPathPossiblities[i].NextDirection)
                {
                case Direction.East:
                    currentPos.SetX(currentPos.GetX() + 1);
                    unfinishedPathPossiblities[i].PathLength = unfinishedPathPossiblities[i].PathLength +
                                                               PathingHelper[
                        currentPos.GetX(), currentPos.GetY()];
                    break;

                case Direction.North:
                    currentPos.SetY(currentPos.GetY() + 1);
                    unfinishedPathPossiblities[i].PathLength = unfinishedPathPossiblities[i].PathLength +
                                                               PathingHelper[
                        currentPos.GetX(), currentPos.GetY()];
                    break;

                case Direction.South:
                    currentPos.SetY(currentPos.GetY() - 1);
                    unfinishedPathPossiblities[i].PathLength = unfinishedPathPossiblities[i].PathLength +
                                                               PathingHelper[
                        currentPos.GetX(), currentPos.GetY()];
                    break;

                case Direction.West:
                    currentPos.SetX(currentPos.GetX() - 1);
                    unfinishedPathPossiblities[i].PathLength = unfinishedPathPossiblities[i].PathLength +
                                                               PathingHelper[
                        currentPos.GetX(), currentPos.GetY()];
                    break;
                }
                //After position calculation add the NextDirection to List<Directions> for future copying
                unfinishedPathPossiblities[i].Directions.Add(unfinishedPathPossiblities[i].NextDirection);


                //Remove paths longer than the shortest path found
                if (unfinishedPathPossiblities[i].PathLength > finishedDirections.PathLength &&
                    finishedDirections.PathLength != 0)
                {
                    unfinishedPathPossiblities.RemoveAt(i);
                    i--;
                }
                //See if the path has arrived at the office
                else if (currentPos.GetX() + 1 == endPoint.GetX() && currentPos.GetY() == endPoint.GetY())
                {
                    unfinishedPathPossiblities[i].Directions.Add(Direction.East);
                    finishedDirections = unfinishedPathPossiblities[i];
                    unfinishedPathPossiblities.RemoveAt(i);
                    i--;
                }
                else if (currentPos.GetX() - 1 == endPoint.GetX() && currentPos.GetY() == endPoint.GetY())
                {
                    unfinishedPathPossiblities[i].Directions.Add(Direction.West);
                    finishedDirections = unfinishedPathPossiblities[i];
                    unfinishedPathPossiblities.RemoveAt(i);
                    i--;
                }
                else if (currentPos.GetX() == endPoint.GetX() && currentPos.GetY() + 1 == endPoint.GetY())
                {
                    unfinishedPathPossiblities[i].Directions.Add(Direction.North);
                    finishedDirections = unfinishedPathPossiblities[i];
                    unfinishedPathPossiblities.RemoveAt(i);
                    i--;
                }
                else if (currentPos.GetX() == endPoint.GetX() && currentPos.GetY() - 1 == endPoint.GetY())
                {
                    unfinishedPathPossiblities[i].Directions.Add(Direction.South);
                    finishedDirections = unfinishedPathPossiblities[i];
                    unfinishedPathPossiblities.RemoveAt(i);
                    i--;
                }
                //If the office hasn't been found add another round of possiblies
                else
                {
                    //Means you are currently on top of an intersection
                    if (PathingHelper[currentPos.GetX(), currentPos.GetY()] < 0)
                    {
                        //No Uturns
                        //Scrubbing Out of index
                        if (currentPos.GetX() != PathingHelper.GetLength(0) - 1)
                        {
                            if (PathingHelper[currentPos.GetX() + 1, currentPos.GetY()] > 0 &&
                                unfinishedPathPossiblities[i].NextDirection != Direction.West)
                            {
                                var buffer = new Point(currentPos.GetX() + 1, currentPos.GetY());
                                if (!CheckForBackTrack(buffer, unfinishedPathPossiblities[i].Directions, startPoint))
                                {
                                    var newPossiblity = new PathPossiblity
                                    {
                                        PathLength = unfinishedPathPossiblities[i].PathLength
                                    };
                                    //Copy the list by value
                                    foreach (var direction in unfinishedPathPossiblities[i].Directions)
                                    {
                                        newPossiblity.Directions.Add(direction);
                                    }
                                    newPossiblity.NextDirection = Direction.East;
                                    unfinishedPathPossiblities.Add(newPossiblity);
                                }
                            }
                        }
                        //Scrubbing for out of index
                        if (currentPos.GetX() != 0)
                        {
                            if (PathingHelper[currentPos.GetX() - 1, currentPos.GetY()] > 0 &&
                                unfinishedPathPossiblities[i].NextDirection != Direction.East)
                            {
                                var buffer = new Point(currentPos.GetX() - 1, currentPos.GetY());
                                if (!CheckForBackTrack(buffer, unfinishedPathPossiblities[i].Directions, startPoint))
                                {
                                    var newPossiblity = new PathPossiblity();
                                    newPossiblity.PathLength = unfinishedPathPossiblities[i].PathLength;
                                    //Copy the list by value
                                    foreach (var direction in unfinishedPathPossiblities[i].Directions)
                                    {
                                        newPossiblity.Directions.Add(direction);
                                    }
                                    newPossiblity.NextDirection = Direction.West;
                                    unfinishedPathPossiblities.Add(newPossiblity);
                                }
                            }
                        }
                        if (currentPos.GetY() != PathingHelper.GetLength(1) - 1)
                        {
                            if (PathingHelper[currentPos.GetX(), currentPos.GetY() + 1] > 0 &&
                                unfinishedPathPossiblities[i].NextDirection != Direction.South)
                            {
                                var buffer = new Point(currentPos.GetX(), currentPos.GetY() + 1);
                                if (!CheckForBackTrack(buffer, unfinishedPathPossiblities[i].Directions, startPoint))
                                {
                                    var newPossiblity = new PathPossiblity();
                                    newPossiblity.PathLength = unfinishedPathPossiblities[i].PathLength;
                                    //Copy the list by value
                                    foreach (var direction in unfinishedPathPossiblities[i].Directions)
                                    {
                                        newPossiblity.Directions.Add(direction);
                                    }
                                    newPossiblity.NextDirection = Direction.North;
                                    unfinishedPathPossiblities.Add(newPossiblity);
                                }
                            }
                        }
                        if (currentPos.GetY() != 0)
                        {
                            if (PathingHelper[currentPos.GetX(), currentPos.GetY() - 1] > 0 &&
                                unfinishedPathPossiblities[i].NextDirection != Direction.North)
                            {
                                var buffer = new Point(currentPos.GetX(), currentPos.GetY() - 1);
                                if (!CheckForBackTrack(buffer, unfinishedPathPossiblities[i].Directions, startPoint))
                                {
                                    var newPossiblity = new PathPossiblity();
                                    newPossiblity.PathLength = unfinishedPathPossiblities[i].PathLength;
                                    //Copy the list by value
                                    foreach (var direction in unfinishedPathPossiblities[i].Directions)
                                    {
                                        newPossiblity.Directions.Add(direction);
                                    }
                                    newPossiblity.NextDirection = Direction.South;
                                    unfinishedPathPossiblities.Add(newPossiblity);
                                }
                            }
                        }
                        //Remove the path that ends at the intersection
                        unfinishedPathPossiblities.RemoveAt(i);
                        i--;
                    }
                }
            }
        }