Ejemplo n.º 1
0
        public void BuildMaze(MazeSlot slot)
        {
            slot.VisitedByBuilder = true;
            List <MazeSlot> neighbor = GetNeighbor(slot);

            if (neighbor.Count == 0)
            {
                if (slotStack.Count == 0)
                {
                    return;
                }
                BuildMaze(slotStack.Pop());
            }
            else
            {
                slotStack.Push(slot);
                int nextPosition = random.Next(neighbor.Count);

                MazeSlot next = neighbor[nextPosition];
                slot.DestroyWall(next);
                next.DestroyWall(slot);

                BuildMaze(next);
            }
        }
Ejemplo n.º 2
0
        public void DestroyWall(MazeSlot other)
        {
            WallPosition position = WallPosition.None;

            if (X > other.X)
            {
                position = WallPosition.Left;
            }
            else if (X < other.X)
            {
                position = WallPosition.Rigth;
            }
            else if (Y > other.Y)
            {
                position = WallPosition.Down;
            }
            else if (Y < other.Y)
            {
                position = WallPosition.Up;
            }

            if (position != WallPosition.None)
            {
                DestroyedWalls.Add(position);
            }
        }
Ejemplo n.º 3
0
 private void InitializeMaze()
 {
     for (int x = 0; x < Maze.GetLength(0); x++)
     {
         for (int y = 0; y < Maze.GetLength(1); y++)
         {
             Maze[x, y] = new MazeSlot(x, y);
         }
     }
 }
Ejemplo n.º 4
0
        public override bool Equals(object obj)
        {
            MazeSlot other = obj as MazeSlot;

            if (other == null)
            {
                return(false);
            }

            return(X == other.X && Y == other.Y);
        }
Ejemplo n.º 5
0
        private static void SolveMaze(MazeSlot[,] maze)
        {
            try
            {
                MazeSolver       solver   = new MazeSolver(maze);
                Stack <MazeSlot> solution = solver.Solve(maze[0, 0], maze[maze.GetLength(0) - 1, maze.GetLength(1) - 1]);

                while (solution.Count > 0)
                {
                    MazeSlot slot = solution.Pop();
                    maze[slot.X, slot.Y].SolutionPath = true;
                }
            }
            catch (Exception)
            {
                Console.WriteLine();
                Console.WriteLine("Não achei uma solução para esse labirinto");
            }
        }
Ejemplo n.º 6
0
        private Stack <MazeSlot> Move(MazeSlot slot)
        {
            if (slot.Equals(end))
            {
                slotStack.Push(slot);
                return(slotStack);
            }

            List <MazeSlot> moves = NextMoves(slot);

            while (moves.Count > 0)
            {
                slotStack.Push(slot);
                slot.VisitedBySolver = true;
                slot = moves.First();

                if (slot.Equals(end))
                {
                    slotStack.Push(slot);
                    return(slotStack);
                }

                moves = NextMoves(slot);

                if (moves.Count == 0)
                {
                    slot.VisitedBySolver = true;
                    do
                    {
                        if (slotStack.Count == 0)
                        {
                            throw new Exception("Could not find a solution!");
                        }
                        slot  = slotStack.Pop();
                        moves = NextMoves(slot);
                    }while (moves.Count == 0);
                }
            }

            return(slotStack);
        }
Ejemplo n.º 7
0
        public List <MazeSlot> GetNeighbor(MazeSlot slot)
        {
            List <MazeSlot> result = new List <MazeSlot>(4);

            if (slot.X - 1 >= 0 && !Maze[slot.X - 1, slot.Y].VisitedByBuilder)
            {
                result.Add(Maze[slot.X - 1, slot.Y]);
            }
            if (slot.X + 1 < Maze.GetLength(0) && !Maze[slot.X + 1, slot.Y].VisitedByBuilder)
            {
                result.Add(Maze[slot.X + 1, slot.Y]);
            }
            if (slot.Y - 1 >= 0 && !Maze[slot.X, slot.Y - 1].VisitedByBuilder)
            {
                result.Add(Maze[slot.X, slot.Y - 1]);
            }
            if (slot.Y + 1 < Maze.GetLength(1) && !Maze[slot.X, slot.Y + 1].VisitedByBuilder)
            {
                result.Add(Maze[slot.X, slot.Y + 1]);
            }

            return(result);
        }
Ejemplo n.º 8
0
        public List <MazeSlot> NextMoves(MazeSlot slot)
        {
            List <MazeSlot> result = new List <MazeSlot>(4);

            if (slot.DestroyedWalls.Contains(WallPosition.Up) && !maze[slot.X, slot.Y + 1].VisitedBySolver)
            {
                result.Add(maze[slot.X, slot.Y + 1]);
            }
            if (slot.DestroyedWalls.Contains(WallPosition.Down) && !maze[slot.X, slot.Y - 1].VisitedBySolver)
            {
                result.Add(maze[slot.X, slot.Y - 1]);
            }
            if (slot.DestroyedWalls.Contains(WallPosition.Left) && !maze[slot.X - 1, slot.Y].VisitedBySolver)
            {
                result.Add(maze[slot.X - 1, slot.Y]);
            }
            if (slot.DestroyedWalls.Contains(WallPosition.Rigth) && !maze[slot.X + 1, slot.Y].VisitedBySolver)
            {
                result.Add(maze[slot.X + 1, slot.Y]);
            }

            return(result);
        }
Ejemplo n.º 9
0
 public Stack <MazeSlot> Solve(MazeSlot start, MazeSlot end)
 {
     this.end = end;
     return(Move(start));
 }