示例#1
0
 public PixelNode(PixelPath pixel, PixelNode prevPixel)
 {
     myPixel = pixel;
     myCoord = pixel.path[4].getCoord();
     myF     = new FCost(myCoord);
     prev    = prevPixel;
 }
示例#2
0
 public void Expand()
 {
     //foreach (PixelNode temp in choices)
     //{
     //    choices.RemoveAt(0);
     //}
     //System.Windows.Forms.MessageBox.Show(myCoord[0] + " " + myCoord[1]);
     Form1.maze.SetPixel(myCoord[0], myCoord[1], Color.Green);
     for (int i = 0; i < 9; i++)
     {
         if (i == 4)
         {
             continue;//don't need to expand same pixel;
         }
         PixelPath temp   = new PixelPath(myPixel.path[i].getCoord()[0], myPixel.path[i].getCoord()[1], Form1.dimensions);
         PixelNode choice = new PixelNode(temp, this);
         if (choice.myPixel.checker[i] == true)
         {
             choice.depth = this.depth + 1;//add partial path cost increment in here
             choice.myF.incPCost();;
             choices.Add(choice);
         }
         else
         {
             //not a valid expansion choice, continue
             continue;
         }
     }
 }
示例#3
0
        public Boolean Contains(PixelNode pn)
        {//checks center of PixelNode to see if that is in unexplored
            int x = pn.myCoord[0];
            int y = pn.myCoord[1];

            return(Contains(x, y));
        }
示例#4
0
        //HashSet<int[]> track = new HashSet<int[]>();
        public PixelNode GSearch(int x, int y)
        {
            MessageBox.Show(x + " " + y);
            int[] temp = { x, y };
            // track.Add(temp);
            PixelPath myPath = new PixelPath(x, y, dimensions);
            pixel     choice = new pixel(myPath.path[4].getCoord(), Color.Black);

            if (myPath.path[4].getCoord()[0] == endX && myPath.path[4].getCoord()[1] == endY)
            {
                return(new PixelNode(myPath));
            }
            else if (x < 0 || y < 0)
            {
                return(null);
            }
            Heuristic fCost = new Heuristic();

            for (int i = 0; i < 9; i++)
            {
                // if(track.Contains(myPath.path[i].getCoord()))
                //{
                // continue;
                // }
                Heuristic tempCost = new Heuristic(myPath.path[i].getCoord());
                if (i == 4)
                {
                    continue; //center pixel is the first pixel we account for and we don't want to stay there.
                }
                if (fCost.getHCost() > tempCost.getHCost())
                {
                    if (myPath.checker[i] == true)
                    {
                        choice = myPath.path[i];
                    }
                    else
                    {
                        //invald pixel location, Skip!
                    }
                }
            }
            int[] currentCoord = { choice.getCoord()[0], choice.getCoord()[1] };
            if (currentCoord[0] == endX && currentCoord[1] == endY)
            {
                PixelPath end    = new PixelPath(currentCoord[0], currentCoord[1], dimensions);
                PixelNode myNode = new PixelNode(end);
                return(myNode);
            }
            else if (choice.Equals(myPath.path[4]))
            {
                MessageBox.Show("We are not moving");
                return(null);
            }
            else
            {
                return(GSearch(currentCoord[0], currentCoord[1]));
            }
        }
示例#5
0
        int depthLimit = 1000; //change for deeper or shallower search
        public PixelNode ASearch()
        {
            Frontier  myQueue = new Frontier();
            PixelPath beginPixel, endPixel;

            beginPixel = new PixelPath(startX, startY, dimensions);
            endPixel   = new PixelPath(endX, endY, dimensions);
            HashSet <int[]> explored  = new HashSet <int[]>();
            PixelNode       startNode = new PixelNode(beginPixel);

            if (beginPixel.Equals(endPixel))
            {//start and end is the same place. This line is here for satisfying the
             //alogorithm rather than consistent application. I don't know if there is a situation that a pixel could be marked as red and blue
                return(startNode);
            }
            myQueue.Add(startNode);
            while (!myQueue.IsEmpty())
            {
                PixelNode temp = myQueue.PopTop();
                if (temp == null || temp.depth == depthLimit)
                {
                    MessageBox.Show("TempInvalid");
                    return(null);
                }
                else
                {
                    if (explored.Contains(temp.myCoord))
                    {
                        continue;
                        //no reason to do anything, this pixel has been explored
                    }
                    else
                    {     //unexplored
                        if (temp.IsEnd())
                        { //We found the end!
                            return(temp);
                        }
                        else
                        {//start processing pixelNode
                            temp.Expand();
                            foreach (PixelNode child in temp.choices)
                            {
                                if (!(myQueue.Contains(child)))
                                {//add children not in Queue
                                    myQueue.Add(child);
                                }
                            }
                            explored.Add(temp.myCoord);
                        }
                    }
                }
            }
            MessageBox.Show("We didn't find it");
            return(null);
        }
示例#6
0
 public PixelNode PopTop()
 {//remove and return first element
     if (IsEmpty())
     {
         return(null);
     }
     else
     {
         PixelNode top = unexplored.First();
         unexplored.Remove(top);
         return(top);
     }
 }
示例#7
0
 public void Remove(PixelNode pn)
 {//remove PixelNode from unexplored
     unexplored.Remove(pn);
 }
示例#8
0
 public void Add(PixelNode pn)
 {//add PixelNode to unexplored
     unexplored.Add(pn);
 }
示例#9
0
        private void button1_Click_1(object sender, EventArgs e)
        {//This is set up to execute the entire alogrithm on successful file selection
            //grabbing image file
            OpenFileDialog imageSelect = new OpenFileDialog();

            imageSelect.Filter = "PNG Image|*.png|JPG Image|*.jpg|BMP Image|*.bmp";
            imageSelect.Title  = "Select Maze";
            if (imageSelect.ShowDialog() == DialogResult.OK)
            {
                imageFile         = Image.FromFile(imageSelect.FileName); //we now have the file location of the maze
                pictureBox1.Image = imageFile;
                MessageBox.Show("Here is the selected Maze", "Maze Selected");
                maze = new Bitmap(imageFile);
                FindStartNEnd();
                MessageBox.Show(dimensions[0].ToString() + " by " + dimensions[1].ToString(), "Maze Dimensions");
                MessageBox.Show("(" + startX.ToString() + "," + startY.ToString() + ")", "Start Pixel");
                MessageBox.Show("(" + endX.ToString() + "," + endY.ToString() + ")", "End Pixel");
                //start search
                PixelNode finish = new PixelNode();
                bool      check  = true;

                while (check)
                {
                    MessageBox.Show("Please type your prefered version of search: A, G, or U");
                    searchType = textBoxInput.Text;
                    switch (searchType)
                    {
                    case "u":
                        finish = USearch();
                        check  = false;
                        break;

                    //case "g":
                    //    PixelNode finish = GSearch();
                    //break
                    case "a":
                        finish = ASearch();
                        check  = false;
                        break;

                    default:
                        MessageBox.Show("Please type A, G, or U for your preferred search");
                        break;
                    }
                }
                //save output
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter   = "PNG Image|*.png|JPG Image|*.jpg|BMP Image|*.bmp";
                sfd.FileName = "outputMaze";
                sfd.Title    = "Save Output Maze";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    string path = sfd.FileName;
                    maze.Save(path);
                    imageFile = Image.FromFile(path);
                }
                try
                {//catch null pointers
                    MessageBox.Show(finish.myCoord.ToString());
                }
                catch (Exception myException)
                {
                    MessageBox.Show(myException.Message + " This happened because the algorithm travelled outside of the Maze.");
                }
            }
            else
            {
                MessageBox.Show("You did not select a file!\nPlease reselect the \"Select Maze\" button to choose a maze.", "User Failed to select a file");
            }
        }