Beispiel #1
0
        public Person(Home home, Office work, PathPossiblity pathToWork, PathPossiblity pathToHome)
        {
            PersonTracker++;
            Home            = home;
            Work            = work;
            CurrentLocation = Home.Location;


            TimeInTraffic = 0;

            PathToWork = pathToWork;
            PathToHome = pathToHome;
        }
Beispiel #2
0
        public Person(Home home, Office work, int[,] pathingHelper)
        {
            PersonTracker++;
            Home            = home;
            Work            = work;
            CurrentLocation = Home.Location;
            PathingHelper   = pathingHelper;

            TimeInTraffic = 0;

            PathToWork = new PathPossiblity();
            PathToHome = new PathPossiblity();
            //From Home to Work
            CalculatePath(Home.Location, Work.Location, ref PathToWork);
            //From Work to Home
            CalculatePath(Work.Location, Home.Location, ref PathToHome);
        }
Beispiel #3
0
        private void CalculatePath(Point startPoint, Point endPoint, ref PathPossiblity pathToDestination)
        {
            var unfinsihedPathPossiblity = new List <PathPossiblity>();


            //Avoiding reference error
            var currentLocation = new Point(startPoint.GetX(), startPoint.GetY());

            //Left One
            try
            {
                if (PathingHelper[currentLocation.GetX() - 1, currentLocation.GetY()] > 0)
                {
                    var directionAPossiblity = new PathPossiblity();
                    directionAPossiblity.Directions.Add(Direction.West);
                    directionAPossiblity.NextDirection = Direction.North;
                    unfinsihedPathPossiblity.Add(directionAPossiblity);
                    var directionBPosiblity = new PathPossiblity();
                    directionBPosiblity.Directions.Add(Direction.West);
                    directionBPosiblity.NextDirection = Direction.South;
                    unfinsihedPathPossiblity.Add(directionBPosiblity);
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
            //Right One
            try
            {
                if (PathingHelper[currentLocation.GetX() + 1, currentLocation.GetY()] > 0)
                {
                    var directionAPossiblity = new PathPossiblity();
                    directionAPossiblity.Directions.Add(Direction.East);
                    directionAPossiblity.NextDirection = Direction.North;
                    unfinsihedPathPossiblity.Add(directionAPossiblity);
                    var directionBPosiblity = new PathPossiblity();
                    directionBPosiblity.Directions.Add(Direction.East);
                    directionBPosiblity.NextDirection = Direction.South;
                    unfinsihedPathPossiblity.Add(directionBPosiblity);
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
            } //Up One
            try
            {
                if (PathingHelper[currentLocation.GetX(), currentLocation.GetY() + 1] > 0)
                {
                    var directionAPossiblity = new PathPossiblity();
                    directionAPossiblity.Directions.Add(Direction.North);
                    directionAPossiblity.NextDirection = Direction.East;
                    unfinsihedPathPossiblity.Add(directionAPossiblity);
                    var directionBPosiblity = new PathPossiblity();
                    directionBPosiblity.Directions.Add(Direction.North);
                    directionBPosiblity.NextDirection = Direction.West;
                    unfinsihedPathPossiblity.Add(directionBPosiblity);
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
            } //Down One
            try
            {
                if (PathingHelper[currentLocation.GetX(), currentLocation.GetY() - 1] > 0)
                {
                    var directionAPossiblity = new PathPossiblity();
                    directionAPossiblity.Directions.Add(Direction.South);
                    directionAPossiblity.NextDirection = Direction.East;
                    unfinsihedPathPossiblity.Add(directionAPossiblity);
                    var directionBPosiblity = new PathPossiblity();
                    directionBPosiblity.Directions.Add(Direction.South);
                    directionBPosiblity.NextDirection = Direction.West;
                    unfinsihedPathPossiblity.Add(directionBPosiblity);
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
            }

            //Main Path Finding Loop

            while (unfinsihedPathPossiblity.Count > 0)
            {
                StepBranchedSearch(startPoint, endPoint, unfinsihedPathPossiblity, ref pathToDestination);
            }
        }
Beispiel #4
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--;
                    }
                }
            }
        }