Esempio n. 1
0
        public PuzzleTreeNode <IPuzzle> Insert(IPuzzle data)
        {
            Root = new PuzzleTreeNode <IPuzzle>(data);

            AddInfoTo(Root);

            return(Root);
        }
Esempio n. 2
0
        public PuzzleTreeNode <T> Insert(T data, PuzzleTreeNode <T> parent)
        {
            var newNode = new PuzzleTreeNode <T>(data);

            newNode.Parent = parent;
            parent.Children.Add(newNode);

            return(newNode);
        }
Esempio n. 3
0
        public PuzzleTreeNode <IPuzzle> Insert(IPuzzle data, PuzzleTreeNode <IPuzzle> parent)
        {
            var newNode = new PuzzleTreeNode <IPuzzle>(data);

            AddInfoTo(newNode);
            newNode.Parent = parent;
            parent.Children.Add(newNode);

            return(newNode);
        }
Esempio n. 4
0
        public IList <IPuzzle> GetNodePathToRoot(PuzzleTreeNode <IPuzzle> puzzleNode)
        {
            IList <IPuzzle> pathToRoot = new List <IPuzzle>();

            do
            {
                pathToRoot.Add(puzzleNode.Data);
                puzzleNode = puzzleNode.Parent;
            } while (puzzleNode != null);

            return(pathToRoot);
        }
        private PuzzleTreeNode <IPuzzle> StartToBuildPuzzleTree(PuzzleEvents events, PuzzleTreeNode <IPuzzle> parent)
        {
            var parentPuzzle = parent.Data;

            if (IsARepeatedPuzzle(parentPuzzle))
            {
                return(null);
            }

            AddToPuzzleRepeatedListIfNeed(parentPuzzle);

            foreach (var allowedMovement in parentPuzzle.AllowedMovements())
            {
                var puzzleChild = (Puzzle)parentPuzzle.Clone();

                Console.WriteLine($"allowedMovement: {allowedMovement}");
                puzzleChild.Move(allowedMovement);

                if (!IsARepeatedPuzzle(puzzleChild))
                {
                    var puzzleChildNode = tree.Insert(puzzleChild, parent);

                    if (puzzleChild.IsDone())
                    {
                        return(puzzleChildNode);
                    }

                    events.onStateChange.Invoke(puzzleChild);
                    var childrenResolution = StartToBuildPuzzleTree(events, puzzleChildNode);

                    if (childrenResolution != null)
                    {
                        return(childrenResolution);
                    }
                }
            }

            return(null);
        }
Esempio n. 6
0
 private void AddInfoTo(PuzzleTreeNode <IPuzzle> node)
 {
     node.AmountOfPiecesOutOfOrder = node.Data.AmountOfPiecesOutOfOrder();
     node.MovementsToFinish        = node.Data.MovementsToFinishAllPieces();
 }
Esempio n. 7
0
 public PuzzleTreeNode <T> Insert(T data)
 {
     return(Root = new PuzzleTreeNode <T>(data));
 }
Esempio n. 8
0
        private PuzzleTreeNode <IPuzzle> StartToBuildPuzzleTree(PuzzleEvents events, PuzzleTreeNode <IPuzzle> parent)
        {
            PuzzleTreeNode <IPuzzle> solution = null;
            var hasMoreItems       = true;
            var foundSolution      = false;
            var openedParents      = new Dictionary <string, PuzzleTreeNode <IPuzzle> >();
            var closedParents      = new Dictionary <string, PuzzleTreeNode <IPuzzle> >();
            var childRepeatControl = new Dictionary <string, PuzzleTreeNode <IPuzzle> >();
            var parentPuzzle       = parent.Data;
            var parentPuzzleString = parentPuzzle.ToString();

            if (parentPuzzle.IsDone())
            {
                solution = parent;

                return(solution);
            }

            openedParents[parentPuzzleString] = parent;

            while (hasMoreItems && !foundSolution)
            {
                foreach (var allowedMovement in parentPuzzle.AllowedMovements())
                {
                    var puzzleChild = (Puzzle)parentPuzzle.Clone();

                    puzzleChild.Move(allowedMovement);

                    var puzzleChildString  = puzzleChild.ToString();
                    var isARepeatedPuzzle  = childRepeatControl.ContainsKey(puzzleChildString);
                    var childWasAParent    = closedParents.ContainsKey(puzzleChildString);
                    var childWillBeAParent = openedParents.ContainsKey(puzzleChildString);

                    if (!childWasAParent || !childWillBeAParent || !isARepeatedPuzzle)
                    {
                        var puzzleChildNode = tree.Insert(puzzleChild, parent);

                        if (!childWasAParent)
                        {
                            openedParents[puzzleChildString] = puzzleChildNode;
                        }

                        if (!isARepeatedPuzzle)
                        {
                            if (puzzleChild.IsDone())
                            {
                                solution      = puzzleChildNode;
                                foundSolution = true;

                                return(puzzleChildNode);
                            }

                            events.onStateChange.Invoke(puzzleChild);
                            childRepeatControl[puzzleChildString] = puzzleChildNode;
                        }
                    }
                }

                openedParents.Remove(parentPuzzleString);
                closedParents[parentPuzzleString] = parent;

                if (!openedParents.Any())
                {
                    hasMoreItems = false;
                    break;
                }

                childRepeatControl[parentPuzzleString] = parent;
                parent             = GetBestNodeByHeuristic(openedParents);
                parentPuzzle       = parent.Data;
                parentPuzzleString = parentPuzzle.ToString();
            }

            return(solution);
        }
Esempio n. 9
0
        private PuzzleTreeNode <IPuzzle> StartToBuildPuzzleTree(PuzzleEvents events, PuzzleTreeNode <IPuzzle> parent)
        {
            PuzzleTreeNode <IPuzzle> nodeSolution = null;
            var hasMoreItems  = true;
            var foundSolution = false;
            var openedParents = new Dictionary <string, PuzzleTreeNode <IPuzzle> >();
            var closedParents = new Dictionary <string, PuzzleTreeNode <IPuzzle> >();
            var parentPuzzle  = parent.Data;

            if (parentPuzzle.IsDone())
            {
                foundSolution = true;
                return(parent);
            }

            openedParents.Add(parentPuzzle.ToString(), parent);

            while (hasMoreItems && !foundSolution)
            {
                foreach (var allowedMovement in parentPuzzle.AllowedMovements())
                {
                    var puzzleChild = (Puzzle)parentPuzzle.Clone();

                    puzzleChild.Move(allowedMovement);

                    if (!IsARepeatedPuzzle(puzzleChild))
                    {
                        var puzzleChildNode = tree.Insert(puzzleChild, parent);

                        if (puzzleChild.IsDone())
                        {
                            foundSolution = true;
                            return(puzzleChildNode);
                        }

                        events.onStateChange.Invoke(puzzleChild);
                        openedParents.Add(puzzleChild.ToString(), puzzleChildNode);
                        AddToPuzzleRepeatedListIfNeed(puzzleChild);
                    }
                }

                var parentPuzzleString = parentPuzzle.ToString();

                openedParents.Remove(parentPuzzleString);

                if (!closedParents.ContainsKey(parentPuzzleString))
                {
                    closedParents.Add(parentPuzzleString, parent);
                }

                if (openedParents.Count == 0)
                {
                    hasMoreItems = false;
                    break;
                }

                AddToPuzzleRepeatedListIfNeed(parentPuzzle);
                parent       = openedParents.First().Value;
                parentPuzzle = parent.Data;
            }

            return(nodeSolution);
        }