Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace WPF_Labyrinth_Generation
- {
- /// <summary>
- /// Логика взаимодействия для MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- enum CellState { Close, Open };
- class Cell
- {
- public Cell(Point currentPosition)
- {
- Visited = false;
- Position = currentPosition;
- }
- public CellState Left { get; set; }
- public CellState Right { get; set; }
- public CellState Bottom { get; set; }
- public CellState Top { get; set; }
- public Boolean Visited { get; set; }
- public Point Position { get; set; }
- }
- private Int32 _Width, _Height, _Walls;
- private Cell[,] Cells;
- public MainWindow()
- {
- InitializeComponent();
- }
- private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
- {
- _Width = Convert.ToInt32(TextBoxWidth.Text);
- }
- private void TextBox_TextChanged2(object sender, TextChangedEventArgs e)
- {
- _Height = Convert.ToInt32(TextBoxHeight.Text);
- }
- private void TextBox_TextChanged3(object sender, TextChangedEventArgs e)
- {
- _Walls = Convert.ToInt32(TextBoxWalls.Text);
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- base.OnInitialized(e);
- Cells = new Cell[_Width, _Height];
- //заполняем начальные данные для ячеек
- for (int y = 0; y < _Height; y++)
- for (int x = 0; x < _Width; x++)
- Cells[x, y] = new Cell(new Point(x, y));
- // Выбераем первую клеточку
- Random rand = new Random();
- Int32 startX = rand.Next(_Width);
- Int32 startY = rand.Next(_Height);
- Stack<Cell> path = new Stack<Cell>();
- Cells[startX, startY].Visited = true;
- path.Push(Cells[startX, startY]);
- while (path.Count > 0)
- {
- Cell _cell = path.Peek();
- //смотрим варианты, в какую сторону можно пойти
- List<Cell> nextStep = new List<Cell>();
- if (_cell.Position.X > 0 && !Cells[Convert.ToInt32(_cell.Position.X - 1), Convert.ToInt32(_cell.Position.Y)].Visited)
- nextStep.Add(Cells[Convert.ToInt32(_cell.Position.X) - 1, Convert.ToInt32(_cell.Position.Y)]);
- if (_cell.Position.X < _Width - 1 && !Cells[Convert.ToInt32(_cell.Position.X) + 1, Convert.ToInt32(_cell.Position.Y)].Visited)
- nextStep.Add(Cells[Convert.ToInt32(_cell.Position.X) + 1, Convert.ToInt32(_cell.Position.Y)]);
- if (_cell.Position.Y > 0 && !Cells[Convert.ToInt32(_cell.Position.X), Convert.ToInt32(_cell.Position.Y) - 1].Visited)
- nextStep.Add(Cells[Convert.ToInt32(_cell.Position.X), Convert.ToInt32(_cell.Position.Y) - 1]);
- if (_cell.Position.Y < _Height - 1 && !Cells[Convert.ToInt32(_cell.Position.X), Convert.ToInt32(_cell.Position.Y) + 1].Visited)
- nextStep.Add(Cells[Convert.ToInt32(_cell.Position.X), Convert.ToInt32(_cell.Position.Y) + 1]);
- if (nextStep.Count() > 0)
- {
- //выбираем сторону из возможных вариантов
- Cell next = nextStep[rand.Next(nextStep.Count())];
- //Открываем сторону, в которую пошли на ячейках
- if (next.Position.X != _cell.Position.X)
- {
- if (_cell.Position.X - next.Position.X > 0)
- {
- _cell.Left = CellState.Open;
- next.Right = CellState.Open;
- }
- else
- {
- _cell.Right = CellState.Open;
- next.Left = CellState.Open;
- }
- }
- if (next.Position.Y != _cell.Position.Y)
- {
- if (_cell.Position.Y - next.Position.Y > 0)
- {
- _cell.Top = CellState.Open;
- next.Bottom = CellState.Open;
- }
- else
- {
- _cell.Bottom = CellState.Open;
- next.Top = CellState.Open;
- }
- }
- next.Visited = true;
- path.Push(next);
- }
- else
- {
- //если пойти никуда нельзя, возвращаемся к предыдущему узлу
- path.Pop();
- }
- }
- renderCells();
- }
- private void renderCells()
- {
- mCanvas.Children.Clear();
- for (int y = 0; y < _Height; y++)
- {
- for (int x = 0; x < _Width; x++)
- {
- if (Cells[x, y].Top == CellState.Close)
- mCanvas.Children.Add(new Line()
- {
- Stroke = Brushes.Black,
- StrokeThickness = 1,
- X1 = 20 * x,
- Y1 = 20 * y,
- X2 = 20 * x + 20,
- Y2 = 20 * y
- });
- if (Cells[x, y].Left == CellState.Close)
- mCanvas.Children.Add(new Line()
- {
- Stroke = Brushes.Black,
- StrokeThickness = 1,
- X1 = 20 * x,
- Y1 = 20 * y,
- X2 = 20 * x,
- Y2 = 20 * y + 20
- });
- if (Cells[x, y].Right == CellState.Close)
- mCanvas.Children.Add(new Line()
- {
- Stroke = Brushes.Black,
- StrokeThickness = 1,
- X1 = 20 * x + 20,
- Y1 = 20 * y,
- X2 = 20 * x + 20,
- Y2 = 20 * y + 20
- });
- if (Cells[x, y].Bottom == CellState.Close)
- mCanvas.Children.Add(new Line()
- {
- Stroke = Brushes.Black,
- StrokeThickness = 1,
- X1 = 20 * x,
- Y1 = 20 * y + 20,
- X2 = 20 * x + 20,
- Y2 = 20 * y + 20
- });
- }
- }
- Int32 wallsX;
- for (int k = 0; k < _Walls; k++)
- {
- for (int y = 1; y < _Height - 1; y++)
- {
- for (int x = 1; x < _Width - 1; x++)
- {
- if (_Walls > 0)
- {
- wallsX = 0;
- if (Cells[x, y].Top == CellState.Close || Cells[x, y].Left == CellState.Close)
- {
- Random randWalls = new Random();
- wallsX = randWalls.Next(2);
- }
- if (wallsX % 2 == 0 && Cells[x, y].Top == CellState.Close)
- {
- mCanvas.Children.Add(new Line()
- {
- Stroke = Brushes.Red,
- StrokeThickness = 1,
- X1 = 20 * x,
- Y1 = 20 * y,
- X2 = 20 * x + 20,
- Y2 = 20 * y
- });
- _Walls--;
- }
- else if (wallsX % 2 == 0 && Cells[x, y].Left == CellState.Close)
- {
- mCanvas.Children.Add(new Line()
- {
- Stroke = Brushes.Red,
- StrokeThickness = 1,
- X1 = 20 * x,
- Y1 = 20 * y,
- X2 = 20 * x,
- Y2 = 20 * y + 20
- });
- _Walls--;
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement