Exemple #1
0
 public AStarNode(AStarNode parent, int[] board) : this(board)
 {
     _parent = parent;
 }
Exemple #2
0
        private void AStar(AStarNode root)
        {
            var time       = DateTime.Now.Millisecond;
            var openList   = new List <AStarNode>();
            var closedList = new List <AStarNode>();

            openList.Add(root);
            var       goalFound          = false;
            AStarNode best               = null;
            var       boardConfiguration = 0;

            while (openList.Count > 0)
            {
                var bestValue     = int.MinValue;
                var bestHeuristic = -1;
                for (int i = 0; i < openList.Count; i++)
                {
                    if (openList[i].Function > bestValue)
                    {
                        bestValue     = openList[i].Function;
                        bestHeuristic = i;
                    }
                }
                best = openList[bestHeuristic];
                openList.RemoveAt(bestHeuristic);
                closedList.Add(best);
                boardConfiguration++;

                if (best.IsGoal())
                {
                    goalFound = true;
                    break;
                }
                else
                {
                    var childrenOfBest = best.Children();
                    if (childrenOfBest == null)
                    {
                        Console.WriteLine("No children");
                        Console.WriteLine(best.IsGoal());
                        return;
                    }
                    foreach (var node in childrenOfBest)
                    {
                        if (node == null)
                        {
                            continue;
                        }
                        if (node.Function >= best.Function)
                        {
                            openList.Add(node);
                        }
                    }
                }
            }
            if (goalFound)
            {
                var time2 = DateTime.Now.Millisecond;
                Console.WriteLine("Goal Board Found");
                PrintBoard(best.Board);
                Console.WriteLine($"{boardConfiguration} were tested");
                Console.WriteLine($"{openList.Count + closedList.Count} were created");
                Console.WriteLine($"Search took approximately {time2 - time} milliseconds");
            }
            else
            {
                Console.WriteLine("Goal board not found");
            }
        }
Exemple #3
0
 public AStarNode()
 {
     _parent = null;
 }