Пример #1
0
        private static GameOf8 AStarPlace(GameOf8 root, int maxdepth)
        {
            List <GameOf8> open = new List <GameOf8> {
                root
            };
            List <GameOf8> closed = new List <GameOf8>();

            while (open.Count > 0)
            {
                open = open.OrderBy(x => x.utilityByPlace + x.Depth).ToList();
                if (open.ElementAt(0).Depth == maxdepth)
                {
                    closed.Add(open.ElementAt(0));
                    open.RemoveAt(0);
                    continue;
                }
                GameOf8 current = open.ElementAt(0);
                closed.Add(current);
                open.RemoveAt(0);
                if (current.CheckWin())
                {
                    Console.Write($" Searched in {closed.Count} states");
                    return(current);
                }
                current.ExpandNode();

                for (int i = 0; i < current.children.Count; i++)
                {
                    GameOf8 currentchild = current.children[i];


                    if (open.Find(x => x.Compare(currentchild)) == null && closed.Find(x => x.Compare(currentchild)) == null)
                    {
                        open.Add(currentchild);
                    }
                }
            }

            #region old

            //List<GameOf8> tree = new List<GameOf8> { root };
            //List<GameOf8> PriorityQueue = new List<GameOf8> { root };
            //while (PriorityQueue.Count > 0 && PriorityQueue[0].Depth < maxdepth)
            //{
            //    PriorityQueue = PriorityQueue.OrderBy(x => x.utilityByPlace + x.Depth).ToList();
            //    if (PriorityQueue[0].CheckWin())
            //    {
            //        Console.Write($" Searched in {tree.Count} states");
            //        return PriorityQueue.ElementAt(0);
            //    }
            //    else
            //    {
            //        GameOf8 moveup = new GameOf8(PriorityQueue[0]);
            //        GameOf8 movedown = new GameOf8(PriorityQueue[0]);
            //        GameOf8 moveleft = new GameOf8(PriorityQueue[0]);
            //        GameOf8 moveright = new GameOf8(PriorityQueue[0]);
            //        if (moveup.MoveUp())
            //            if (tree.Find(x => x.Compare(moveup)) == null)
            //            {
            //                PriorityQueue.Add(moveup);
            //                tree.Add(moveup);
            //            }

            //        if (movedown.MoveDown())
            //            if (tree.Find(x => x.Compare(movedown)) == null)
            //            {
            //                PriorityQueue.Add(movedown);
            //                tree.Add(movedown);
            //            }

            //        if (moveleft.MoveLeft())
            //            if (tree.Find(x => x.Compare(moveleft)) == null)
            //            {
            //                PriorityQueue.Add(moveleft);
            //                tree.Add(moveleft);
            //            }

            //        if (moveright.MoveRight())
            //            if (tree.Find(x => x.Compare(moveright)) == null)
            //            {
            //                PriorityQueue.Add(moveright);
            //                tree.Add(moveright);
            //            }
            //    }
            //    PriorityQueue.RemoveAt(0);
            //}

            #endregion old

            Console.Write($" Searched in {closed.Count} states");
            return(null);
        }
Пример #2
0
        private static GameOf8 DFS(GameOf8 root, int maxdepth)
        {
            List <GameOf8> open = new List <GameOf8> {
                root
            };
            List <GameOf8> closed = new List <GameOf8>();

            while (open.Count > 0)
            {
                if (open.ElementAt(0).Depth == maxdepth)
                {
                    closed.Add(open.ElementAt(0));
                    open.RemoveAt(0);
                    continue;
                }
                GameOf8 current = open.ElementAt(0);
                open.RemoveAt(0);
                closed.Add(current);

                if (current.CheckWin())
                {
                    Console.Write($" Searched in {closed.Count} states");
                    return(current);
                }

                current.ExpandNode();
                for (int i = 0; i < current.children.Count; i++)
                {
                    GameOf8 currentchild = current.children[i];

                    if (open.Find(x => x.Compare(currentchild)) == null && closed.Find(x => x.Compare(currentchild)) == null)
                    {
                        open.Insert(0, currentchild);
                        continue;
                    }
                }
            }

            #region old

            //List<GameOf8> tree = new List<GameOf8> { root };
            //Stack<GameOf8> stack = new Stack<GameOf8>();

            //stack.Push(root);

            //while (stack.Count > 0)
            //{
            //    if (stack.Peek().Depth > maxdepth) { stack.Pop(); continue; }
            //    if (stack.Peek().CheckWin())
            //    {
            //        Console.Write($" Searched in {tree.Count} states");
            //        return stack.ElementAt(0);
            //    }
            //    GameOf8 moveup = new GameOf8(stack.Peek());
            //    GameOf8 movedown = new GameOf8(stack.Peek());
            //    GameOf8 moveleft = new GameOf8(stack.Peek());
            //    GameOf8 moveright = new GameOf8(stack.Peek());
            //    stack.Pop();

            //    if (moveleft.MoveLeft())
            //    {
            //        if (tree.Find(x => x.Compare(moveleft)) == null)
            //        {
            //            stack.Push(moveleft);
            //            tree.Add(moveleft);
            //        }
            //    }
            //    if (moveup.MoveUp())
            //    {
            //        if (tree.Find(x => x.Compare(moveup)) == null)
            //        {
            //            stack.Push(moveup);
            //            tree.Add(moveup);
            //        }
            //    }

            //    if (movedown.MoveDown())
            //    {
            //        if (tree.Find(x => x.Compare(movedown)) == null)
            //        {
            //            stack.Push(movedown);
            //            tree.Add(movedown);
            //        }
            //    }

            //    if (moveright.MoveRight())
            //    {
            //        if (tree.Find(x => x.Compare(moveright)) == null)
            //        {
            //            stack.Push(moveright);
            //            tree.Add(moveright);
            //        }
            //    }

            //}

            #endregion old

            Console.Write($" Searched in {closed.Count} states");
            return(null);
        }