Example #1
0
        public static (int, Maze) GoToKey(MazeItem key, Maze maze)
        {
            var current = maze.FindAll(i => i == "@").First();

            maze.Distance(current);
            //totalSteps += key.Steps;
            //Console.WriteLine("Key: " + key.Name);

            var doors = maze.FindAll(i => Char.IsUpper(i[0]));

            maze.SetMapItem(current.X, current.Y, ".");
            maze.SetMapItem(key.X, key.Y, "@");

            if (doors.Any())
            {
                var door = doors.Where(d => d.Name.ToLowerInvariant() == key.Name).FirstOrDefault();
                if (door != null)
                {
                    maze.SetMapItem(door.X, door.Y, ".");
                }
            }
            else
            {
                //maze.DumpMap(1);
                //Console.Write("No doors left ");
                //break;
                //return (-1, null);
            }


            return(key.Steps, maze);
        }
Example #2
0
        public List <MazeItem> walk(MazeItem origin, MazeItem dest, List <MazeItem> steps)
        {
            if (origin.X == dest.X && origin.Y == dest.Y)
            {
                return(steps);
            }

            var             y = origin.Y;
            var             x = origin.X;
            List <MazeItem> newSteps;

            _pointMap[x, y] = 0;

            steps.Add(origin);
            var nextStep = new MazeItem()
            {
                ItemType = Room
            };

            nextStep.X = origin.X;
            nextStep.Y = origin.Y;

            if (y > 0 && _map[x, y - 1] != Wall && _pointMap[x, y - 1] == -1)
            {
                nextStep.Y = origin.Y - 1;
                newSteps   = walk(nextStep, dest, steps);
                if (newSteps != null)
                {
                    return(newSteps);
                }
            }

            if (y < _maxY && _map[x, y + 1] != Wall && _pointMap[x, y + 1] == -1)
            {
                nextStep.Y = origin.Y + 1;
                newSteps   = walk(nextStep, dest, steps);
                if (newSteps != null)
                {
                    return(newSteps);
                }
            }

            if (x > 0 && _map[x - 1, y] != Wall && _pointMap[x - 1, y] == -1)
            {
                nextStep.X = origin.X - 1;
                newSteps   = walk(nextStep, dest, steps);
                if (newSteps != null)
                {
                    return(newSteps);
                }
            }

            if (x < _maxX && _map[x + 1, y] != Wall && _pointMap[x + 1, y] == -1)
            {
                nextStep.X = origin.X + 1;
                newSteps   = walk(nextStep, dest, steps);
                if (newSteps != null)
                {
                    return(newSteps);
                }
            }


            return(null);
        }
Example #3
0
 public List <MazeItem> Walk(MazeItem origin, MazeItem dest)
 {
     InitPointMap();
     return(walk(origin, dest, new List <MazeItem>()));
 }
Example #4
0
 public int Distance(MazeItem origin, MazeItem dest)
 {
     return(Distance(origin.X, origin.Y, dest.X, dest.Y));
 }
Example #5
0
 public void Distance(MazeItem origin)
 {
     Distance(origin.X, origin.Y, origin.X, origin.Y);
 }