public static void VizualizePath(
            Labyrint labyrint,
            Point from,
            Point to,
            int bombCount)
        {
            if (!BelongsToLabyrint(labyrint, from) || !BelongsToLabyrint(labyrint, from))
            {
                throw new ArgumentException("Point doesn't belongs to labirynt");
            }
            var path = GetPath(labyrint, from, to, bombCount);

            if (path is null)
            {
                Console.WriteLine("No ways");
                return;
            }
            Console.WriteLine(labyrint);
            Console.ForegroundColor = ConsoleColor.Red;
            foreach (var point in path)
            {
                Thread.Sleep(700);
                Console.SetCursorPosition(point.Y, point.X);
                Console.Write("*");
            }
        }
Example #2
0
        public static Labyrint Generate()
        {
            var labirynt = new Labyrint {
                labirynt = new CellState[ExampleLabirynt.Length, ExampleLabirynt[0].Length]
            };

            for (var x = 0; x < ExampleLabirynt.Length; x++)
            {
                for (var y = 0; y < ExampleLabirynt[0].Length; y++)
                {
                    labirynt[x, y] = ExampleLabirynt[x][y] == '#' ? CellState.Wall : CellState.Empty;
                }
            }
            return(labirynt);
        }
        private static IEnumerable <Point> GetPath(Labyrint labyrint,
                                                   Point from,
                                                   Point to,
                                                   int bombCount)
        {
            var visited = new HashSet <Point>();
            var queue   = new System.Collections.Generic.Queue <State>();
            var ways    = new Dictionary <Point, Point>();

            queue.Enqueue(new State(from, null, bombCount));
            while (queue.Count != 0)
            {
                var currentState = queue.Dequeue();
                visited.Add(currentState.Point);
                var neighbours = GetNeighbours(currentState.Point)
                                 .Where(x => !visited.Contains(x) && BelongsToLabyrint(labyrint, x));
                foreach (var point in neighbours)
                {
                    ways[point] = currentState.Point;
                    if (!labyrint.IsWall(point))
                    {
                        queue.Enqueue(new State(point, currentState, currentState.BombCount));
                    }
                    else if (currentState.BombCount > 0)
                    {
                        queue.Enqueue(new State(point, currentState, currentState.BombCount - 1));
                    }
                    if (point.Equals(to))
                    {
                        return(BuildPath(currentState));
                    }
                }
            }

            return(null);
        }
 private static bool BelongsToLabyrint(Labyrint labyrint, Point p)
 {
     return(0 <= p.X && p.X < labyrint.Width && 0 <= p.Y && p.Y < labyrint.Height);
 }