Exemplo n.º 1
0
 private void BG_Worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
 {
     try
     {
         if (MakeMazeCB.Checked)
         {
             MazeGenerator mazeGenerator = new MazeGenerator(new Size((int)ColumnsNUD.Value, (int)RowsNUD.Value));
             mazeGenerator.Generate();
             mazeGenerator.SaveAsImage(MakeOutputPathTB.Text);
             mazeGenerator = null;
             if (!SolveMazeCB.Checked)
             {
                 System.Diagnostics.Process.Start(MakeOutputPathTB.Text);
             }
         }
         if (SolveMazeCB.Checked)
         {
             MazeSolver mazeSolver = new MazeSolver(SolveInputPathTB.Text);
             mazeSolver.Solve();
             mazeSolver.SaveSolutionAsImage(SolveOutputPathTB.Text);
             mazeSolver = null;
             System.Diagnostics.Process.Start(SolveOutputPathTB.Text);
         }
         GC.Collect();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
         throw;
     }
 }
Exemplo n.º 2
0
    public Maze Solve(MazeSolver solver)
    {
        var ans = new Maze(width, height);

        ans.Field = solver.Solve(width, height, CopyField(width, height, Field));
        return(ans);
    }
Exemplo n.º 3
0
    Vector3 getNextPosition(MazeGrid.Grid maze, Cell current, Cell target)
    {
        MazeSolver        solver = new MazeSolver(maze, 0, 0, rows - 1, columns - 1);
        LinkedList <Cell> path   = solver.Solve(current);

        Cell next = path.First.Next.Value;

        return(new Vector3(next.Row * wallLength + wallLength / 2, 0.0f, next.Column * wallLength + wallLength / 2));
    }
Exemplo n.º 4
0
        private void btnSolve_Click(object sender, EventArgs e)
        {
            var solutions = _solver.Solve();

            if (!solutions.Any())
            {
                DoLog("No Solutions found!");
                return;
            }

            DoLog("Maze solved: " + solutions.Count + " ways found.");
            var i = 1;

            foreach (var s in solutions)
            {
                DoLog(i++ + ": " + s);
            }
        }
Exemplo n.º 5
0
        private void _backGroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            object[] args = e.Argument as object[];

            int  value   = 25; // Cell size
            bool solving = (bool)args[1];

            if (!solving)
            {
                //this._maze.Generate(this.pictureBoxDraw.Width / value,
                //    (this.pictureBoxDraw.Height - value) / value,
                //    (int)args[2]);
                _maze          = new Maze(mazePicBox.Width, mazePicBox.Height, 25);
                _mazeGenerator = new GenerateMaze(_maze);
                _mazeGenerator.Generate(value);
            }
            else
            {
                var solver = new MazeSolver(_maze);
                solver.Solve();
                this.hasSolution = true;
            }
            this.mazePicBox.Invalidate();
        }
Exemplo n.º 6
0
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            var maze = this.generator.GetMaze(
                int.Parse(this.toolStripTextBox2.Text),
                int.Parse(this.toolStripTextBox1.Text),
                int.Parse(this.toolStripTextBox3.Text)
                );
            var mazeString = new String[maze.GetLength(1),maze.GetLength(0)];
            GraphNode<MazeNode,int> start = null;
            GraphNode<MazeNode,int> end = null;

            for(int i=0;i<maze.GetLength(1) ;i++)
            {
                for(int j=0;j<maze.GetLength(0);j++)
                {
                    var mazeCell = maze[j,i];
                    var strCell = String.Empty;
                    switch(mazeCell.Value.Type)
                    {
                        case Maze.CellType.start : strCell = "S"; start = mazeCell; break;
                        case Maze.CellType.end : strCell = "E";end = mazeCell ; break;
                        case Maze.CellType.empty : strCell = "";break;
                        default : throw new Exception("Unsuported cell type:" + mazeCell.Value.Type);
                    }

                    foreach(var n in mazeCell.Neighbours)
                    {
                        if(mazeCell.Value.X ==n.Key.Value.X)
                        {
                            if(mazeCell.Value.Y == n.Key.Value.Y +1)
                            {
                                strCell+="↑";
                            }
                            else if(mazeCell.Value.Y == n.Key.Value.Y -1)
                            {
                                strCell +="↓" ;
                            }
                            else
                            {
                                strCell+="?";
                            }
                        }
                        else if(mazeCell.Value.Y ==n.Key.Value.Y)
                        {
                            if(mazeCell.Value.X == n.Key.Value.X +1)
                            {
                                strCell+="←";
                            }
                            else if(mazeCell.Value.X == n.Key.Value.X -1)
                            {
                                strCell +="→" ;
                            }
                            else
                            {
                                strCell+="?";
                            }
                        }
                        else
                        {
                            strCell+="?";
                        }
                    }

                    mazeString[i,j] = strCell;
                }
            }

            // solve it

            var mazeSolver = new MazeSolver();
            var path =  mazeSolver.Solve(maze,start.Value.X , start.Value.Y,end.Value.X,end.Value.Y).ToList();

            // do display it
            this.dataGridView1.Rows.Clear();
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.DefaultCellStyle.Font =
                new System.Drawing.Font("Lucida Sans Unicode", 10F,
                                     System.Drawing.FontStyle.Regular,
                                     System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            for(int index=0;index<mazeString.GetLength(0);index++)
            {
                this.dataGridView1.Columns.Add(index.ToString(),index.ToString());
            }
            this.dataGridView1.SuspendLayout();
            for(int index=0;index<mazeString.GetLength(1);index++)
            {
                var row = new DataGridViewRow();
                row.HeaderCell.Value = index.ToString();
                for(int i=0;i<mazeString.GetLength(0);i++)
                {
                    var cell = new DataGridViewTextBoxCell() { Value = mazeString[i, index] };
                    row.Cells.Add(cell);
                    if(path.Any(p=>p.Value.X == index && p.Value.Y == i))
                    {
                        cell.Style.BackColor = Color.Green;
                    }
                }
                this.dataGridView1.Rows.Add(row);

            }
            this.dataGridView1.ResumeLayout(true);
        }