Ejemplo n.º 1
0
 private static void SetUpMaze()
 {
     Cells              = new bool[videoMode.Width / 20 - 1, videoMode.Height / 20 - 1];
     Walls              = new bool[videoMode.Width / 10 - 2, videoMode.Height / 10 - 2];
     VisitedWalls       = new bool[videoMode.Width / 10 - 2, videoMode.Height / 10 - 2];
     CellStack          = new Stack <IntPair>();
     FinishedGenerating = false;
     Solving            = false;
     CurrentCell        = new IntPair();
     for (int i = 0; i < Walls.GetLength(0); i++)
     {
         for (int j = 0; j < Walls.GetLength(1); j++)
         {
             Walls[i, j] = true;
         }
     }
 }
Ejemplo n.º 2
0
 public static bool CheckNeighbors(IntPair start, IntPair neighbor, bool[,] Maze, bool[,] Walls)
 {
     bool ret = true;
     try
     {
         IntPair temp = neighbor*2 + new IntPair(0, 1);
         if (temp.X < Maze.GetLength(0) && temp.Y < Maze.GetLength(1) && temp.X > 0 && temp.Y > 0)
             ret = ret && temp == start || Walls[temp.X, temp.Y];
         temp = neighbor + new IntPair(1, 0);
         if (temp.X < Maze.GetLength(0) && temp.Y < Maze.GetLength(1) && temp.X > 0 && temp.Y > 0)
             ret = ret && (temp == start || Walls[temp.X, temp.Y]);
         temp = neighbor*2 + new IntPair(-1, 0);
         if (temp.X < Maze.GetLength(0) && temp.Y < Maze.GetLength(1) && temp.X > 0 && temp.Y > 0)
             ret = ret && (temp == start || Walls[temp.X, temp.Y]);
         temp = neighbor*2 + new IntPair(0, -1);
         if (temp.X < Maze.GetLength(0) && temp.Y < Maze.GetLength(1) && temp.X > 0 && temp.Y > 0)
             ret = ret && (temp == start || Walls[temp.X, temp.Y]);
     }
     catch (IndexOutOfRangeException e)
     {
         return false;
     }
     return ret;
 }
Ejemplo n.º 3
0
        private static bool SolveIterate(Stack<IntPair> cellStack, bool[,] cells, bool[,] walls, IntPair StartCell,
                                         IntPair Endcell)
        {
            iter++;
            if (CurrentCell == Endcell*2)
                return false;
            IntPair pos = CurrentCell;
            var neighbors = new List<IntPair>();
            var tempcell = new IntPair(CurrentCell.X + 1, CurrentCell.Y);
            if (tempcell.X < walls.GetLength(0) && !walls[tempcell.X, tempcell.Y] &&
                !VisitedWalls[tempcell.X, tempcell.Y])
                neighbors.Add(tempcell);
            tempcell = new IntPair(CurrentCell.X, CurrentCell.Y + 1);
            if (tempcell.Y < walls.GetLength(1) && !walls[tempcell.X, tempcell.Y] &&
                !VisitedWalls[tempcell.X, tempcell.Y])
                neighbors.Add(tempcell);
            tempcell = new IntPair(CurrentCell.X, CurrentCell.Y - 1);
            if (tempcell.Y >= 0 && !walls[tempcell.X, tempcell.Y] && !VisitedWalls[tempcell.X, tempcell.Y])
                neighbors.Add(tempcell);
            tempcell = new IntPair(CurrentCell.X - 1, CurrentCell.Y);
            if (tempcell.X >= 0 && !walls[tempcell.X, tempcell.Y] && !VisitedWalls[tempcell.X, tempcell.Y])
                neighbors.Add(tempcell);

            if (neighbors.Count == 0)
            {
                if (CellStack.Count == 0)
                    return false;
                CurrentCell = CellStack.Pop();
                return true;
            }
            if (cellStack.Count > 0 && cellStack.Peek() != CurrentCell)
                cellStack.Push(CurrentCell);
            IntPair newpos = neighbors[r.Next(neighbors.Count)];
            VisitedWalls[newpos.X, newpos.Y] = true;
            cellStack.Push(newpos);
            CurrentCell = newpos;
            return true;
        }
Ejemplo n.º 4
0
 private static void SetUpMaze()
 {
     Cells = new bool[videoMode.Width/20 - 1,videoMode.Height/20 - 1];
     Walls = new bool[videoMode.Width/10 - 2,videoMode.Height/10 - 2];
     VisitedWalls = new bool[videoMode.Width/10 - 2,videoMode.Height/10 - 2];
     CellStack = new Stack<IntPair>();
     FinishedGenerating = false;
     Solving = false;
     CurrentCell = new IntPair();
     for (int i = 0; i < Walls.GetLength(0); i++)
         for (int j = 0; j < Walls.GetLength(1); j++)
         {
             Walls[i, j] = true;
         }
 }
Ejemplo n.º 5
0
        private static void Main(string[] args)
        {
            var form = new SettingsDialog();
            Application.EnableVisualStyles();
            Application.Run(form);
            videoMode = VideoMode.DesktopMode;
            RenderWindow window = new RenderWindow(videoMode, "Maze Crap", Styles.Fullscreen);
            window.EnableVerticalSync(Settings.Default.VerticalSync);
            window.ShowMouseCursor(false);
            window.Closed += (sender, e) => Application.Exit();
            window.KeyPressed += (sender, e) => ((RenderWindow) sender).Close();
            SetUpMaze();
            target = new Image(VideoMode.DesktopMode.Width/10, VideoMode.DesktopMode.Height/10) {Smooth = false};
            float accumulator = 0;
            float WaitTime = 0;
            var fps = (float) Settings.Default.Framerate;
            window.Show(true);
            bool hasrun = false;
            while (window.IsOpened())
            {
                accumulator += window.GetFrameTime();
                while (accumulator > fps)
                {
                    if (FinishedGenerating && !Solving)
                    {
                        if (WaitTime < Settings.Default.WaitTime && hasrun)
                        {
                            WaitTime += window.GetFrameTime();
                        }
                        else
                        {
                            WaitTime = 0;
                            CurrentCell = StartCell*2;
                            VisitedWalls[CurrentCell.X, CurrentCell.Y] = true;
                            CellStack.Clear();
                            Solving = true;
                            hasrun = true;
                        }
                        accumulator -= fps;
                        continue;
                    }
                    if (Solving && FinishedSolving)
                    {
                        if (WaitTime < Settings.Default.WaitTime)
                            WaitTime += window.GetFrameTime();
                        else
                        {
                            Solving = false;
                            FinishedGenerating = false;
                            FinishedSolving = false;
                            SetUpMaze();
                            continue;
                        }
                        continue;
                    }
                    if (Solving && !FinishedSolving)
                    {
                        if (Settings.Default.ShowSolving)
                            FinishedSolving = !SolveIterate(CellStack, Cells, Walls, StartCell, EndCell);
                        else
                        {
                            while (SolveIterate(CellStack, Cells, Walls, StartCell, EndCell)) ;
                            FinishedSolving = true;
                        }
                        accumulator -= fps;
                        continue;
                    }

                    if (Settings.Default.ShowGeneration)
                    {
                        FinishedGenerating = !GenerateIterate(CellStack, Cells, Walls);
                        hasrun = true;
                    }
                    else
                    {
                        while (GenerateIterate(CellStack, Cells, Walls)) ;
                        FinishedGenerating = true;
                    }
                    accumulator -= fps;
                    NeedsRedraw = true;
                }
                Render(window);
                window.Display();
                window.DispatchEvents();
            }
        }
Ejemplo n.º 6
0
 private static bool GenerateIterate(Stack<IntPair> CellStack, bool[,] Maze, bool[,] Walls)
 {
     if (CellStack.Count == 0 && CurrentCell == new IntPair(0, 0))
     {
         if (started)
             return false;
         var temppos = new IntPair(r.Next(Maze.GetLength(0)), (r.Next(Maze.GetLength(1))));
         Maze[temppos.X, temppos.Y] = true;
         Walls[temppos.X*2, temppos.Y*2] = false;
         CellStack.Push(temppos);
         CurrentCell = temppos;
         StartCell = temppos;
         BackTrackLevel++;
         return true;
     }
     IntPair pos = CurrentCell;
     var neighbors = new List<IntPair>();
     if (pos.X + 1 < Maze.GetLength(0) && !Maze[pos.X + 1, pos.Y] &&
         IntPair.CheckNeighbors(pos, new IntPair(pos.X + 1, pos.Y), Maze, Walls))
         neighbors.Add(new IntPair(pos.X + 1, pos.Y));
     if (pos.Y + 1 < Maze.GetLength(1) && !Maze[pos.X, pos.Y + 1] &&
         IntPair.CheckNeighbors(pos, new IntPair(pos.X, pos.Y + 1), Maze, Walls))
         neighbors.Add(new IntPair(pos.X, pos.Y + 1));
     if (pos.X - 1 >= 0 && !Maze[pos.X - 1, pos.Y] &&
         IntPair.CheckNeighbors(pos, new IntPair(pos.X - 1, pos.Y), Maze, Walls))
         neighbors.Add(new IntPair(pos.X - 1, pos.Y));
     if (pos.Y - 1 >= 0 && !Maze[pos.X, pos.Y - 1] &&
         IntPair.CheckNeighbors(pos, new IntPair(pos.X, pos.Y - 1), Maze, Walls))
         neighbors.Add(new IntPair(pos.X, pos.Y - 1));
     if (neighbors.Count == 0)
     {
         if (CellStack.Count == 0)
             return false;
         CurrentCell = CellStack.Pop();
         BackTrackLevel--;
         return true;
     }
     IntPair newpos = neighbors[r.Next(neighbors.Count)];
     Maze[newpos.X, newpos.Y] = true;
     Walls[newpos.X*2, newpos.Y*2] = false;
     Walls[(newpos.X*2 + CurrentCell.X*2)/2, (newpos.Y*2 + CurrentCell.Y*2)/2] = false;
     CellStack.Push(newpos);
     CurrentCell = newpos;
     BackTrackLevel++;
     if (BackTrackLevel >= 0)
     {
         EndCell = newpos;
         BackTrackLevel = 0;
     }
     return true;
 }
Ejemplo n.º 7
0
        private static void Main(string[] args)
        {
            var form = new SettingsDialog();

            Application.EnableVisualStyles();
            Application.Run(form);
            videoMode = VideoMode.DesktopMode;
            RenderWindow window = new RenderWindow(videoMode, "Maze Crap", Styles.Fullscreen);

            window.EnableVerticalSync(Settings.Default.VerticalSync);
            window.ShowMouseCursor(false);
            window.Closed     += (sender, e) => Application.Exit();
            window.KeyPressed += (sender, e) => ((RenderWindow)sender).Close();
            SetUpMaze();
            target = new Image(VideoMode.DesktopMode.Width / 10, VideoMode.DesktopMode.Height / 10)
            {
                Smooth = false
            };
            float accumulator = 0;
            float WaitTime    = 0;
            var   fps         = (float)Settings.Default.Framerate;

            window.Show(true);
            bool hasrun = false;

            while (window.IsOpened())
            {
                accumulator += window.GetFrameTime();
                while (accumulator > fps)
                {
                    if (FinishedGenerating && !Solving)
                    {
                        if (WaitTime < Settings.Default.WaitTime && hasrun)
                        {
                            WaitTime += window.GetFrameTime();
                        }
                        else
                        {
                            WaitTime    = 0;
                            CurrentCell = StartCell * 2;
                            VisitedWalls[CurrentCell.X, CurrentCell.Y] = true;
                            CellStack.Clear();
                            Solving = true;
                            hasrun  = true;
                        }
                        accumulator -= fps;
                        continue;
                    }
                    if (Solving && FinishedSolving)
                    {
                        if (WaitTime < Settings.Default.WaitTime)
                        {
                            WaitTime += window.GetFrameTime();
                        }
                        else
                        {
                            Solving            = false;
                            FinishedGenerating = false;
                            FinishedSolving    = false;
                            SetUpMaze();
                            continue;
                        }
                        continue;
                    }
                    if (Solving && !FinishedSolving)
                    {
                        if (Settings.Default.ShowSolving)
                        {
                            FinishedSolving = !SolveIterate(CellStack, Cells, Walls, StartCell, EndCell);
                        }
                        else
                        {
                            while (SolveIterate(CellStack, Cells, Walls, StartCell, EndCell))
                            {
                                ;
                            }
                            FinishedSolving = true;
                        }
                        accumulator -= fps;
                        continue;
                    }

                    if (Settings.Default.ShowGeneration)
                    {
                        FinishedGenerating = !GenerateIterate(CellStack, Cells, Walls);
                        hasrun             = true;
                    }
                    else
                    {
                        while (GenerateIterate(CellStack, Cells, Walls))
                        {
                            ;
                        }
                        FinishedGenerating = true;
                    }
                    accumulator -= fps;
                    NeedsRedraw  = true;
                }
                Render(window);
                window.Display();
                window.DispatchEvents();
            }
        }
Ejemplo n.º 8
0
        private static bool GenerateIterate(Stack <IntPair> CellStack, bool[,] Maze, bool[,] Walls)
        {
            if (CellStack.Count == 0 && CurrentCell == new IntPair(0, 0))
            {
                if (started)
                {
                    return(false);
                }
                var temppos = new IntPair(r.Next(Maze.GetLength(0)), (r.Next(Maze.GetLength(1))));
                Maze[temppos.X, temppos.Y]         = true;
                Walls[temppos.X * 2, temppos.Y *2] = false;
                CellStack.Push(temppos);
                CurrentCell = temppos;
                StartCell   = temppos;
                BackTrackLevel++;
                return(true);
            }
            IntPair pos       = CurrentCell;
            var     neighbors = new List <IntPair>();

            if (pos.X + 1 < Maze.GetLength(0) && !Maze[pos.X + 1, pos.Y] &&
                IntPair.CheckNeighbors(pos, new IntPair(pos.X + 1, pos.Y), Maze, Walls))
            {
                neighbors.Add(new IntPair(pos.X + 1, pos.Y));
            }
            if (pos.Y + 1 < Maze.GetLength(1) && !Maze[pos.X, pos.Y + 1] &&
                IntPair.CheckNeighbors(pos, new IntPair(pos.X, pos.Y + 1), Maze, Walls))
            {
                neighbors.Add(new IntPair(pos.X, pos.Y + 1));
            }
            if (pos.X - 1 >= 0 && !Maze[pos.X - 1, pos.Y] &&
                IntPair.CheckNeighbors(pos, new IntPair(pos.X - 1, pos.Y), Maze, Walls))
            {
                neighbors.Add(new IntPair(pos.X - 1, pos.Y));
            }
            if (pos.Y - 1 >= 0 && !Maze[pos.X, pos.Y - 1] &&
                IntPair.CheckNeighbors(pos, new IntPair(pos.X, pos.Y - 1), Maze, Walls))
            {
                neighbors.Add(new IntPair(pos.X, pos.Y - 1));
            }
            if (neighbors.Count == 0)
            {
                if (CellStack.Count == 0)
                {
                    return(false);
                }
                CurrentCell = CellStack.Pop();
                BackTrackLevel--;
                return(true);
            }
            IntPair newpos = neighbors[r.Next(neighbors.Count)];

            Maze[newpos.X, newpos.Y]         = true;
            Walls[newpos.X * 2, newpos.Y *2] = false;
            Walls[(newpos.X * 2 + CurrentCell.X * 2) / 2, (newpos.Y * 2 + CurrentCell.Y * 2) / 2] = false;
            CellStack.Push(newpos);
            CurrentCell = newpos;
            BackTrackLevel++;
            if (BackTrackLevel >= 0)
            {
                EndCell        = newpos;
                BackTrackLevel = 0;
            }
            return(true);
        }
Ejemplo n.º 9
0
        private static bool SolveIterate(Stack <IntPair> cellStack, bool[,] cells, bool[,] walls, IntPair StartCell,
                                         IntPair Endcell)
        {
            iter++;
            if (CurrentCell == Endcell * 2)
            {
                return(false);
            }
            IntPair pos       = CurrentCell;
            var     neighbors = new List <IntPair>();
            var     tempcell  = new IntPair(CurrentCell.X + 1, CurrentCell.Y);

            if (tempcell.X < walls.GetLength(0) && !walls[tempcell.X, tempcell.Y] &&
                !VisitedWalls[tempcell.X, tempcell.Y])
            {
                neighbors.Add(tempcell);
            }
            tempcell = new IntPair(CurrentCell.X, CurrentCell.Y + 1);
            if (tempcell.Y < walls.GetLength(1) && !walls[tempcell.X, tempcell.Y] &&
                !VisitedWalls[tempcell.X, tempcell.Y])
            {
                neighbors.Add(tempcell);
            }
            tempcell = new IntPair(CurrentCell.X, CurrentCell.Y - 1);
            if (tempcell.Y >= 0 && !walls[tempcell.X, tempcell.Y] && !VisitedWalls[tempcell.X, tempcell.Y])
            {
                neighbors.Add(tempcell);
            }
            tempcell = new IntPair(CurrentCell.X - 1, CurrentCell.Y);
            if (tempcell.X >= 0 && !walls[tempcell.X, tempcell.Y] && !VisitedWalls[tempcell.X, tempcell.Y])
            {
                neighbors.Add(tempcell);
            }

            if (neighbors.Count == 0)
            {
                if (CellStack.Count == 0)
                {
                    return(false);
                }
                CurrentCell = CellStack.Pop();
                return(true);
            }
            if (cellStack.Count > 0 && cellStack.Peek() != CurrentCell)
            {
                cellStack.Push(CurrentCell);
            }
            IntPair newpos = neighbors[r.Next(neighbors.Count)];

            VisitedWalls[newpos.X, newpos.Y] = true;
            cellStack.Push(newpos);
            CurrentCell = newpos;
            return(true);
        }