Exemple #1
0
        /// <summary>
        /// Génère aléatoirement un nouveau labyrinthe
        /// </summary>
        /// <param name="length">Longueur du labyrinthe</param>
        /// <param name="width">Largeur du labyrinthe</param>
        /// <returns>Le labyrinthe généré</returns>
        public Maze Generate(int length, int width)
        {
            Random rnd = new Random();

            this.MazeInformations = new Maze
            {
                Map    = new MapType[length, width],
                Length = length,
                Width  = width,
                Entree = rnd.Next(1, width - 1)
            };

            for (int i = 0; i < MazeInformations.Length; i++)
            {
                for (int j = 0; j < MazeInformations.Width; j++)
                {
                    if (i == 0 || i == MazeInformations.Length - 1 || j == 0 || j == MazeInformations.Width - 1)
                    {
                        MazeInformations.Map[i, j] = MapType.Wall;
                    }
                    else
                    {
                        MazeInformations.Map[i, j] = MapType.Unknown;
                    }
                }
            }

            MazeInformations.Map[0, MazeInformations.Entree] = MapType.Free;
            Point entreePoint = new Point(1, MazeInformations.Entree);

            Exposed.Add(entreePoint);

            while (Exposed.Any())
            {
                int rand = rnd.Next(Exposed.Count());

                Point point = Exposed[rand];

                if (Decide(point))
                {
                    Dig(point);
                }
                else
                {
                    MazeInformations.Map[point.X, point.Y] = MapType.Wall;
                }
                Exposed.RemoveAt(rand);
            }

            for (int i = 0; i < MazeInformations.Length; i++)
            {
                for (int j = 0; j < MazeInformations.Width; j++)
                {
                    if (MazeInformations.Map[i, j] == MapType.Unknown)
                    {
                        MazeInformations.Map[i, j] = MapType.Wall;
                    }
                }
            }
            return(MazeInformations);
        }