Beispiel #1
0
        public List <ListHex> PathBetween(ListHex start, ListHex end, int currentBest)
        {
            // Get the best looking node
            var bestLookingHex = _searchSpace.Board
                                 .OrderBy(x => x.F())
                                 .ThenBy(x => x.RandomValue)
                                 .FirstOrDefault(z => z.Status == Status.Open);

            if (bestLookingHex == null)
            {
                if (start.Status == Status.Untested || start.Status == Status.Open)
                {
                    bestLookingHex = start;
                }
                else
                {
                    return(new List <ListHex>());
                }
            }

            if (bestLookingHex.Equals(end))
            {
                AddLogLine(bestLookingHex + " is at end.");
                var preferredPath = new List <ListHex>();

                var parent = bestLookingHex;
                while (parent != null)
                {
                    if (!preferredPath.Contains(parent))
                    {
                        preferredPath.Add(parent);
                        parent = parent.Parent;
                    }
                    else
                    {
                        parent = null;
                    }
                }

                return(preferredPath);
            }

            bestLookingHex.Status = Status.Closed;


            var neighbours = _searchSpace.GetNeighboursFrom(bestLookingHex, _playerSearchingFor);

            AddLog(neighbours.Count + " neighbours to examine.");
            foreach (var node in neighbours)
            {
                if (node.Owner != opponent)
                {
                    if (node.Status == Status.Open)
                    {
                        if (node.G > bestLookingHex.G +
                            (node.Owner == _playerSearchingFor
                                ? costPerFriendlyNode
                                : costPerOpenNode))
                        {
                            node.Parent = bestLookingHex;
                            node.G      = bestLookingHex.G +
                                          (node.Owner == _playerSearchingFor
                                         ? costPerFriendlyNode
                                         : costPerOpenNode);

                            node.H =
                                (_playerSearchingFor == PlayerType.Blue
                                    ? _searchSpace.Size - 1 - node.Row
                                    : _searchSpace.Size - 1 - node.Column) * costPerNodeLeft;
                        }
                    }
                    else if (node.Status == Status.Untested)
                    {
                        node.Status = Status.Open;
                        node.Parent = bestLookingHex;
                        node.G      = bestLookingHex.G +
                                      (node.Owner == _playerSearchingFor
                                     ? costPerFriendlyNode
                                     : costPerOpenNode);

                        node.H = (_playerSearchingFor == PlayerType.Blue
                                     ? _searchSpace.Size - 1 - node.Row
                                     : _searchSpace.Size - 1 - node.Column) * costPerNodeLeft;
                    }
                }
            }

            return(PathBetween(start, end, currentBest));
        }