Ejemplo n.º 1
0
        private SortedList <int, GameNode> SolveWithBFS()
        {
            SortedList <int, GameNode> winNodes = new SortedList <int, GameNode>();
            GameNode           root             = new GameNode(Stacks, new Movement(stackWinCount, stackWinCount + 1));
            Queue <GameNode>   stateQueue       = new Queue <GameNode>();
            HashSet <GameNode> visited          = new HashSet <GameNode>(root);
            int leastMoves = Int32.MaxValue;
            var sw         = Stopwatch.StartNew();

            stateQueue.Enqueue(root);
            while (true)
            {
                TotalNodeTraversed++;
                if (stateQueue.Count == 0)
                {
                    break;
                }
                GameNode currentNode = stateQueue.Dequeue();
#if DEBUG
                //ShowGame(currentNode);
#endif
                List <Movement> moves = currentNode.GetValidMoves();

                if (moves.Count > 0)
                {
                    currentNode.Childs = new List <GameNode>();
                    foreach (var move in moves)
                    {
                        GameNode newNode = currentNode.GenerateChildNode(move);
                        if (newNode.IsWin(stackWinCount))
                        {
                            if (leastMoves > newNode.MoveCount)
                            {
                                leastMoves = newNode.MoveCount;
                            }

                            winNodes.TryAdd(newNode.MoveCount, newNode);
                        }
                        else if (newNode.MoveCount < leastMoves - 1)
                        {
                            if (visited.TryGetValue(newNode, out GameNode matchedNode))
                            {
                                if (matchedNode.MoveCount > newNode.MoveCount)
                                {
                                    visited.Remove(matchedNode);

                                    if (matchedNode.Parent != null)
                                    {
                                        matchedNode.Parent.Childs.Remove(matchedNode);
                                    }
                                    currentNode.Childs.Add(newNode);
                                    visited.Add(newNode);
                                }
                            }
                            else
                            {
                                currentNode.Childs.Add(newNode);
                                visited.Add(newNode);
                            }
                        }
                    }
                    var orderedNodes = currentNode.Childs.OrderByDescending(node => node.GetScoreWithNoMoveCountPenalty());
                    foreach (var node in orderedNodes)
                    {
                        stateQueue.Enqueue(node);
                    }
                }
            }
            TimeFinished = sw.ElapsedMilliseconds / 1000f;
            return(winNodes);
        }
Ejemplo n.º 2
0
        private GameNode SolveWithDFS()
        {
            GameNode           root       = new GameNode(Stacks, new Movement(stackWinCount, stackWinCount + 1));
            Stack <GameNode>   stateStack = new Stack <GameNode>();
            HashSet <GameNode> visited    = new HashSet <GameNode>(root);
            var sw = Stopwatch.StartNew();

            stateStack.Push(root);
            while (true)
            {
                TotalNodeTraversed++;
                if (stateStack.Count == 0)
                {
                    break;
                }
                GameNode currentNode = stateStack.Pop();
#if DEBUG
                //ShowGame(currentNode);
#endif
                List <Movement> moves = currentNode.GetValidMoves();

                if (moves.Count > 0)
                {
                    currentNode.Childs = new List <GameNode>(moves.Count);
                    foreach (var move in moves)
                    {
                        GameNode newNode = currentNode.GenerateChildNode(move);
                        if (newNode.IsWin(stackWinCount))
                        {
                            return(newNode);
                        }
                        else
                        {
                            if (visited.TryGetValue(newNode, out GameNode matchedNode))
                            {
                                if (matchedNode.MoveCount > newNode.MoveCount)
                                {
                                    visited.Remove(matchedNode);

                                    if (matchedNode.Parent != null)
                                    {
                                        matchedNode.Parent.Childs.Remove(matchedNode);
                                    }
                                    currentNode.Childs.Add(newNode);
                                    visited.Add(newNode);
                                }
                            }
                            else
                            {
                                currentNode.Childs.Add(newNode);
                                visited.Add(newNode);
                            }
                        }
                    }
                    var orderedNodes = currentNode.Childs.OrderByDescending(node => node.GetScoreWithNoMoveCountPenalty());
                    foreach (var node in orderedNodes)
                    {
                        stateStack.Push(node);
                    }
                }
            }
            TimeFinished = sw.ElapsedMilliseconds / 1000f;
            return(null);
        }