Example #1
0
        private IList <LocationWrapper> GetNeighbours(Location current)
        {
            IList <LocationWrapper> neighbours = new List <LocationWrapper> ();
            // north of current
            LocationWrapper north = new LocationWrapper(new Location(current.X - 1, current.Y));
            LocationWrapper south = new LocationWrapper(new Location(current.X + 1, current.Y));
            LocationWrapper west  = new LocationWrapper(new Location(current.X, current.Y - 1));
            LocationWrapper east  = new LocationWrapper(new Location(current.X, current.Y + 1));

            if (north.Location.X >= 0)
            {
                neighbours.Add(north);
            }
            if (south.Location.X < this.map.Tiles.GetLength(0))
            {
                neighbours.Add(south);
            }
            if (east.Location.Y < this.map.Tiles.GetLength(1))
            {
                neighbours.Add(east);
            }
            if (west.Location.Y >= 0)
            {
                neighbours.Add(west);
            }
            return(checkNeighbours(neighbours));
        }
Example #2
0
 private bool contains(IList <LocationWrapper> closedList, LocationWrapper location)
 {
     foreach (var l in closedList)
     {
         if (l.Location.X == location.Location.X && l.Location.Y == location.Location.Y)
         {
             return(true);
         }
     }
     return(false);
 }
Example #3
0
        private IList <Location> Backtrack(LocationWrapper current)
        {
            IList <Location> path = new List <Location>();

            path.Add(current.Location);
            while (current.Parent != null)
            {
                path.Add(current.Parent.Location);
                current = current.Parent;
            }
            return(path.Reverse().ToList());
        }
Example #4
0
        public IList <Location> A_star(Location Start, Location finish)
        {
            IList <LocationWrapper> openlist   = new List <LocationWrapper>();
            IList <LocationWrapper> closedList = new List <LocationWrapper>();
            var StartWrapper = new LocationWrapper(Start);

            StartWrapper.Parent = null;
            openlist.Add(StartWrapper);
            while (openlist.Count > 0)
            {
                var current = GetMin(openlist);
                openlist.Remove(current);
                if (current.Location.X == finish.X && current.Location.Y == finish.Y)
                {
                    return(Backtrack(current));
                }
                var Neighbours = GetNeighbours(current.Location);
                closedList.Add(current);
                foreach (var location in Neighbours)
                {
                    if (!contains(closedList, location))
                    {
                        var new_G = current.G_score + 1;
                        if (openlist.Contains(location))
                        {
                            if (new_G < location.G_score)
                            {
                                location.Parent  = current;
                                location.G_score = new_G;
                            }
                        }
                        else
                        {
                            if (location.Parent == null)
                            {
                                location.Parent = current;
                            }
                            location.G_score = new_G;
                            openlist.Add(location);
                        }
                        location.H_score = CalculateDistance(location.Location, finish);
                        if (this.map.Tiles[location.Location.X, location.Location.Y].TileType == TileType.Enemy)
                        {
                            location.E_score = 150;
                        }
                    }
                }
            }
            return(null);
        }