コード例 #1
0
        /// <summary>
        /// Process a node, finding new paths
        /// </summary>
        /// <param name="node">The current node</param>
        /// <param name="path">The path travelled so far</param>
        public void ProcessNode(FieldCoord node, List <FieldCoord> path)
        {
            List <FieldCoord> border = GetBorder(node);

            foreach (FieldCoord newnode in border)
            {
                bool present = false;
                foreach (FieldCoord fc in path)
                {
                    if (fc.Row == newnode.Row && fc.Col == newnode.Col)
                    {
                        present = true;
                        break;
                    }
                }

                if (!present)
                {
                    List <FieldCoord> newpath = new List <FieldCoord>(path);

                    newpath.Add(newnode);
                    if (mGrid.GetCell(newnode.Row, newnode.Col) == FieldElement.Finish)
                    {
                        mSolutions.Add(newpath);
                    }
                    else
                    {
                        ProcessNode(newnode, newpath);
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Get the closest elements to this grid cell
        /// </summary>
        /// <param name="node">The coodinates related to the current grid cell</param>
        /// <returns>The elements in the border</returns>
        private List <FieldCoord> GetBorder(FieldCoord node)
        {
            List <FieldCoord> border = new List <FieldCoord>();

            // North
            if (node.Row - 1 >= 0)
            {
                FieldElement elem = mGrid.GetCell(node.Row - 1, node.Col);
                if (elem == FieldElement.Empty || elem == FieldElement.Finish)
                {
                    border.Add(new FieldCoord(node.Row - 1, node.Col));
                }
            }

            // South
            if (node.Row + 1 < mGrid.Rows)
            {
                FieldElement elem = mGrid.GetCell(node.Row + 1, node.Col);
                if (elem == FieldElement.Empty || elem == FieldElement.Finish)
                {
                    border.Add(new FieldCoord(node.Row + 1, node.Col));
                }
            }

            // West
            if (node.Col - 1 >= 0)
            {
                FieldElement elem = mGrid.GetCell(node.Row, node.Col - 1);
                if (elem == FieldElement.Empty || elem == FieldElement.Finish)
                {
                    border.Add(new FieldCoord(node.Row, node.Col - 1));
                }
            }

            // East
            if (node.Col + 1 < mGrid.Cols)
            {
                FieldElement elem = mGrid.GetCell(node.Row, node.Col + 1);
                if (elem == FieldElement.Empty || elem == FieldElement.Finish)
                {
                    border.Add(new FieldCoord(node.Row, node.Col + 1));
                }
            }

            return(border);
        }
コード例 #3
0
        /// <summary>
        /// Found the solutions of the maze with depth first algorithm
        /// </summary>
        public void Solve()
        {
            mStart = FindFieldElement(FieldElement.Start);

            mFinish = FindFieldElement(FieldElement.Finish);

            if (mStart == null || mFinish == null)
            {
                return;
            }

            List <FieldCoord> path = new List <FieldCoord>();

            path.Add(mStart);

            ProcessNode(mStart, path);
        }