Ejemplo n.º 1
0
        //Uses a depth first search to find any path that reaches the end goal
        public static bool CheckPathExists(AIBoard board, bool isPlayerOne)
        {
            //Moves to be visited is used to prevent the revisiting of nodes by another branch.
            HashSet <string>   movesToBeVisited = new HashSet <string>();
            Stack <SearchNode> spaces           = new Stack <SearchNode>();
            bool result = false;

            //Adds the appropriate starting node depending on specified player.
            if (isPlayerOne)
            {
                spaces.Push(new SearchNode(board.GetPlayerOnePos()));
            }
            else
            {
                spaces.Push(new SearchNode(board.GetPlayerTwoPos()));
            }



            while (spaces.Count != 0 && !result)
            {
                SearchNode currentNode = spaces.Pop();

                //Check the win conditions of the appropriate player.
                if (HasDirectPath(board, isPlayerOne, currentNode.space))
                {
                    result = true;
                    break;
                }

                //Get the possible moves from the space of the current node.
                List <string> movesFromSpace = board.GetAdjacentMoves(currentNode.space);
                //Adds the appropriate nodes to the stack to be searched.
                foreach (string move in movesFromSpace)
                {
                    if (!movesToBeVisited.Contains(move))
                    {
                        spaces.Push(new SearchNode(move, currentNode.depth));
                        movesToBeVisited.Add(move);
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        //Finds shortest path for the chosen player to the end of the board and returns it.
        public static int FindShortestPath(AIBoard board, bool isPlayerOne)
        {
            Queue <SearchNode> spaces           = new Queue <SearchNode>();
            HashSet <string>   movesToBeVisited = new HashSet <string>();
            int result = -1;

            if (isPlayerOne)
            {
                spaces.Enqueue(new SearchNode(board.GetPlayerOnePos()));
            }
            else
            {
                spaces.Enqueue(new SearchNode(board.GetPlayerTwoPos()));
            }
            movesToBeVisited.Add(spaces.Peek().space);

            //Will exit after all paths return without finding end or
            //break out of loop when first path to finish is found.
            while (spaces.Count != 0 && result == -1)
            {
                SearchNode currentNode = spaces.Dequeue();

                //Checks for easy direct path
                if (HasDirectPath(board, isPlayerOne, currentNode.space))
                {
                    result = currentNode.depth + FindDirectDistance(currentNode.space, isPlayerOne);
                    break;
                }

                //Get a list of moves from the current node location.
                //If the node has not already been visited by this branch it is added to the queue.
                List <string> movesFromSpace = board.GetAdjacentMoves(currentNode.space);
                foreach (string move in movesFromSpace)
                {
                    if (!movesToBeVisited.Contains(move))
                    {
                        spaces.Enqueue(new SearchNode(move, currentNode.depth));
                        movesToBeVisited.Add(move);
                    }
                }
            }
            return(result);
        }