Exemplo n.º 1
0
        public PathData pathTo(Pos target, Pos currentLocation, Tile[][] board, int spikeCost = 5)
        {
            Dictionary<string, InternalTile> Navigated = new Dictionary<string, InternalTile>();
            HashSet<InternalTile> Closed = new HashSet<InternalTile>();

            InternalTile beginning = new InternalTile() { TilePos = currentLocation, Weight = 0 };
            HashSet<InternalTile> Opened = new HashSet<InternalTile> {
                beginning
            };

            Dictionary<string, int> Scores = new Dictionary<string, int> {
                {GetKey(beginning.TilePos.x, beginning.TilePos.y), beginning.Weight}
            };


            Dictionary<string, float> FullScores = new Dictionary<string, float> {
                {GetKey(beginning.TilePos.x, beginning.TilePos.y), GetDistance(currentLocation, target)}
            };

            while (Opened.Any()) {
                InternalTile lowest = Opened.First(tile => GetKey(tile.TilePos.x, tile.TilePos.y) == GetLowestCostTile(FullScores, Opened));

                if (lowest.TilePos.x == target.x && lowest.TilePos.y == target.y) {
                    return ReconstructPath(Navigated, target);
                }

                Opened.Remove(lowest);
                Closed.Add(lowest);

                foreach (Pos neighbor in GetNeighbors(lowest.TilePos, board.Length, board[0].Length)) {
                    if (Closed.Any(tile => tile.TilePos.x == neighbor.x && tile.TilePos.y == neighbor.y)) {
                        continue;
                    }

                    string neighborKey = GetKey(neighbor.x, neighbor.y);
                    int curScore = Scores[GetKey(lowest.TilePos.x, lowest.TilePos.y)] + 1;
                    if (!Opened.Any(tile => tile.TilePos.x == neighbor.x && tile.TilePos.y == neighbor.y)) {
                        Opened.Add(new InternalTile { TilePos = new Pos { x = neighbor.x, y = neighbor.y } });
                    } else if (curScore >= (Scores.ContainsKey(neighborKey) ? Scores[neighborKey] : int.MaxValue)) {
                        continue;
                    }

                    Navigated.Add(neighborKey, lowest);
                    Scores.Add(neighborKey, curScore);
                    FullScores.Add(neighborKey, curScore + GetDistance(neighbor, target));
                }
            }

            return null;
        }
Exemplo n.º 2
0
        public PathData pathTo(Pos target, Pos currentLocation, Tile[][] board, int spikeCost = 5)
        {
            Dictionary <string, InternalTile> Navigated = new Dictionary <string, InternalTile>();
            HashSet <InternalTile>            Closed    = new HashSet <InternalTile>();

            InternalTile beginning = new InternalTile()
            {
                TilePos = currentLocation, Weight = 0
            };
            HashSet <InternalTile> Opened = new HashSet <InternalTile> {
                beginning
            };

            Dictionary <string, int> Scores = new Dictionary <string, int> {
                { GetKey(beginning.TilePos.x, beginning.TilePos.y), beginning.Weight }
            };


            Dictionary <string, float> FullScores = new Dictionary <string, float> {
                { GetKey(beginning.TilePos.x, beginning.TilePos.y), GetDistance(currentLocation, target) }
            };

            while (Opened.Any())
            {
                InternalTile lowest = Opened.First(tile => GetKey(tile.TilePos.x, tile.TilePos.y) == GetLowestCostTile(FullScores, Opened));

                if (lowest.TilePos.x == target.x && lowest.TilePos.y == target.y)
                {
                    return(ReconstructPath(Navigated, target));
                }

                Opened.Remove(lowest);
                Closed.Add(lowest);

                foreach (Pos neighbor in GetNeighbors(lowest.TilePos, board.Length, board[0].Length))
                {
                    if (Closed.Any(tile => tile.TilePos.x == neighbor.x && tile.TilePos.y == neighbor.y))
                    {
                        continue;
                    }

                    string neighborKey = GetKey(neighbor.x, neighbor.y);
                    int    curScore    = Scores[GetKey(lowest.TilePos.x, lowest.TilePos.y)] + 1;
                    if (!Opened.Any(tile => tile.TilePos.x == neighbor.x && tile.TilePos.y == neighbor.y))
                    {
                        Opened.Add(new InternalTile {
                            TilePos = new Pos {
                                x = neighbor.x, y = neighbor.y
                            }
                        });
                    }
                    else if (curScore >= (Scores.ContainsKey(neighborKey) ? Scores[neighborKey] : int.MaxValue))
                    {
                        continue;
                    }

                    Navigated.Add(neighborKey, lowest);
                    Scores.Add(neighborKey, curScore);
                    FullScores.Add(neighborKey, curScore + GetDistance(neighbor, target));
                }
            }

            return(null);
        }