示例#1
0
        private void removeWall(CoordAndStatus currentCell, CoordAndStatus neighbourCell, CoordAndStatus[,] cells)
        {
            Point p = Bettwen(currentCell, neighbourCell);

            cells[p.X, p.Y].wall  = false;
            cells[p.X, p.Y].visit = true;
        }
示例#2
0
        private void btnGoClick(object sender, EventArgs e)
        {
            this.SuspendLayout();
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    CoordAndStatus c = (CoordAndStatus)cell[i, j].Tag;
                    if (c.visit)
                    {
                        c.visit        = false;
                        cell[i, j].Tag = null;
                        cell[i, j].Tag = c;
                    }
                }
            }

            CoordAndStatus[,] cc = FindWay();
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    cell[i, j].Tag = null;
                    cell[i, j].Tag = cc[i, j];
                    if (cc[i, j].visit)
                    {
                        cell[i, j].BackColor = Color.Violet;
                    }
                }
            }
            cell[start.X, start.Y].BackColor   = Color.Green;
            cell[finish.X, finish.Y].BackColor = Color.Red;
            this.ResumeLayout();
        }
示例#3
0
        private List <CoordAndStatus> getNotVisitNeighbourthInMaze(CoordAndStatus currentCell, CoordAndStatus[,] cells)
        {
            List <CoordAndStatus> neighbourth = new List <CoordAndStatus>();

            Point[] p = new Point[4];
            p[0] = new Point(currentCell.x + 2, currentCell.y);
            p[1] = new Point(currentCell.x - 2, currentCell.y);
            p[2] = new Point(currentCell.x, currentCell.y + 2);
            p[3] = new Point(currentCell.x, currentCell.y - 2);

            for (int i = 0; i < p.Length; i++)
            {
                try
                {
                    if (!cells[p[i].X, p[i].Y].visit)
                    {
                        Point b = Bettwen(currentCell, cells[p[i].X, p[i].Y]);
                        if (!cells[b.X, b.Y].wall)
                        {
                            cells[b.X, b.Y].visit = true;
                            neighbourth.Add(cells[p[i].X, p[i].Y]);
                        }
                    }
                }
                catch (IndexOutOfRangeException)
                {
                }
            }

            return(neighbourth);
        }
示例#4
0
 private CoordAndStatus[,] GetTag()
 {
     CoordAndStatus[,] s = new CoordAndStatus[height, width];
     for (int i = 0; i < height; i++)
     {
         for (int j = 0; j < width; j++)
         {
             s[i, j] = (CoordAndStatus)this.cell[i, j].Tag;
         }
     }
     return(s);
 }
示例#5
0
        private Point Bettwen(CoordAndStatus neighbourCell, CoordAndStatus currentCell)
        {
            int xDiff = neighbourCell.x - currentCell.x;
            int yDiff = neighbourCell.y - currentCell.y;

            xDiff = (xDiff != 0) ? (xDiff / Math.Abs(xDiff)) : 0;
            yDiff = (yDiff != 0) ? (yDiff / Math.Abs(yDiff)) : 0;

            Point target = new Point();

            target.X = currentCell.x + xDiff; //координаты стенки
            target.Y = currentCell.y + yDiff;

            return(target);
        }
示例#6
0
        private CoordAndStatus[,] GenerateMaze()
        {
            CoordAndStatus[,] cells = new CoordAndStatus[height, width];
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    cells[i, j] = new CoordAndStatus(i, j, (i % 2 == 1 && j % 2 == 1 && i != 0 && j != 0 && i != height - 1 && j != width - 1) ? false : true, false);
                }
            }

            CoordAndStatus startCell = cells[start.X, start.Y];

            startCell.visit = true;
            CoordAndStatus currentCell = startCell;

            Stack stack = new Stack();

            do
            {
                List <CoordAndStatus> notVisitNeibourth = getNeighborth(currentCell, cells);

                if (notVisitNeibourth.Count > 0)
                {
                    stack.Push(currentCell);
                    Random         rand = new Random();
                    int            n    = (notVisitNeibourth.Count == 1) ? 0 : rand.Next(notVisitNeibourth.Count, 24513) % notVisitNeibourth.Count;
                    CoordAndStatus neib = cells[notVisitNeibourth[n].x, notVisitNeibourth[n].y];
                    removeWall(currentCell, neib, cells);
                    currentCell       = neib;
                    currentCell.visit = true;
                }
                else if (stack.Count > 0)
                {
                    currentCell = (CoordAndStatus)stack.Pop();
                }
            }while (GetAllNotVisitedCells(cells) > 0);
            return(cells);
        }
示例#7
0
        private CoordAndStatus[,] FindWay()
        {
            CoordAndStatus[,] cells = GetTag();
            List <Way> way = new List <Way>();

            CoordAndStatus startCell = cells[start.X, start.Y];

            startCell.visit = true;
            CoordAndStatus currentCell = startCell;

            Stack stack = new Stack();

            List <CoordAndStatus> notVisitNeibourth;

            while (currentCell.x != finish.X || currentCell.y != finish.Y)
            {
                notVisitNeibourth = getNotVisitNeighbourthInMaze(currentCell, cells);

                if (notVisitNeibourth.Count > 0)
                {
                    stack.Push(currentCell);
                    Random         rand = new Random();
                    int            n    = (notVisitNeibourth.Count == 1) ? 0 : rand.Next(notVisitNeibourth.Count, 2452) % notVisitNeibourth.Count;
                    CoordAndStatus neib = cells[notVisitNeibourth[n].x, notVisitNeibourth[n].y];
                    currentCell       = neib;
                    currentCell.visit = true;
                }
                else if (stack.Count > 0)
                {
                    currentCell = (CoordAndStatus)stack.Pop();
                }
                else
                {
                    MessageBox.Show("Fail!!!");
                }
            }

            return(cells);
        }