示例#1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (int.TryParse(textBox1.Text, out inputInt))
            {
                if (inputInt == 0)
                {
                    string message = "Cannot use 0 as integer. Please Enter another Integer number.";
                    string caption = "Error Detected in Input";

                    MessageBoxButtons buttons = MessageBoxButtons.OK;
                    DialogResult      result;

                    result = MessageBox.Show(message, caption, buttons);

                    //if (result == DialogResult.OK)
                    //{

                    //}
                }
                else
                {
                    cols = inputInt;
                    rows = inputInt;

                    c_width = Canvas.Width / cols;

                    grid = new Spot[cols, rows];

                    //building grid
                    for (int i = 0; i < cols; i++)
                    {
                        for (int j = 0; j < rows; j++)
                        {
                            grid[i, j] = new Spot(i, j);
                        }
                    }

                    currentSpot = grid[0, 0];

                    while (true)
                    {
                        currentSpot.visited = true;

                        //Using recursive backtracker algorithm
                        //https://en.wikipedia.org/wiki/Maze_generation_algorithm#Recursive_backtracker

                        //step 1
                        next = currentSpot.CheckNeighbors(grid);

                        if (next != null)
                        {
                            next.visited = true;

                            //step 2
                            stack.Push(currentSpot);
                            //Console.WriteLine($"stack count is {stack.Count}");

                            //step 3
                            RemoveWalls(currentSpot, next);

                            //step 4
                            currentSpot = next;
                            //Console.WriteLine($"currentSpot is {currentSpot.i} and {currentSpot.j}");
                        }
                        else if (stack.Count > 0)
                        {
                            currentSpot = stack.Pop();
                            //Console.WriteLine($"pop a stack and count is {stack.Count}");
                            //Console.WriteLine($"currentSpot after pop is {currentSpot.i} and {currentSpot.j}");
                        }
                        else if (next == null)
                        {
                            break;
                        }

                        //Call DrawGrid here (inside while loop) when we want to see how it works step by step. Really slow down the program
                        //because we used 2d array and how each element is accessed in 2d array(?) and how WF implements draw graphics.
                        //DrawGrid();
                    }

                    //Call DrawGrid outside while loop when the maze has already been generated. Really fast visualization
                    DrawGrid();
                }
            }

            else
            {
                string message = "Please Enter an Integer number.";
                string caption = "Error Detected in Input";

                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult      result;

                result = MessageBox.Show(message, caption, buttons);
            }
        }