Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15.  
  16. namespace WPF_Labyrinth_Generation
  17. {
  18.     /// <summary>
  19.     /// Логика взаимодействия для MainWindow.xaml
  20.     /// </summary>
  21.     public partial class MainWindow : Window
  22.     {
  23.         enum CellState { Close, Open };
  24.         class Cell
  25.         {
  26.             public Cell(Point currentPosition)
  27.             {
  28.                 Visited = false;
  29.                 Position = currentPosition;
  30.             }
  31.  
  32.             public CellState Left { get; set; }
  33.             public CellState Right { get; set; }
  34.             public CellState Bottom { get; set; }
  35.             public CellState Top { get; set; }
  36.             public Boolean Visited { get; set; }
  37.             public Point Position { get; set; }
  38.         }
  39.  
  40.         private Int32 _Width, _Height, _Walls;
  41.         private Cell[,] Cells;
  42.  
  43.         public MainWindow()
  44.         {
  45.             InitializeComponent();
  46.         }
  47.  
  48.         private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  49.         {
  50.             _Width = Convert.ToInt32(TextBoxWidth.Text);
  51.         }
  52.  
  53.         private void TextBox_TextChanged2(object sender, TextChangedEventArgs e)
  54.         {
  55.             _Height = Convert.ToInt32(TextBoxHeight.Text);
  56.         }
  57.  
  58.         private void TextBox_TextChanged3(object sender, TextChangedEventArgs e)
  59.         {
  60.             _Walls = Convert.ToInt32(TextBoxWalls.Text);
  61.         }
  62.  
  63.         private void Button_Click(object sender, RoutedEventArgs e)
  64.         {
  65.             base.OnInitialized(e);
  66.             Cells = new Cell[_Width, _Height];
  67.  
  68.             //заполняем начальные данные для ячеек
  69.             for (int y = 0; y < _Height; y++)
  70.                 for (int x = 0; x < _Width; x++)
  71.                     Cells[x, y] = new Cell(new Point(x, y));
  72.  
  73.             // Выбераем первую клеточку
  74.             Random rand = new Random();
  75.             Int32 startX = rand.Next(_Width);
  76.             Int32 startY = rand.Next(_Height);
  77.  
  78.             Stack<Cell> path = new Stack<Cell>();
  79.  
  80.             Cells[startX, startY].Visited = true;
  81.             path.Push(Cells[startX, startY]);
  82.  
  83.             while (path.Count > 0)
  84.             {
  85.                 Cell _cell = path.Peek();
  86.  
  87.                 //смотрим варианты, в какую сторону можно пойти
  88.  
  89.                 List<Cell> nextStep = new List<Cell>();
  90.                 if (_cell.Position.X > 0 && !Cells[Convert.ToInt32(_cell.Position.X - 1), Convert.ToInt32(_cell.Position.Y)].Visited)
  91.                     nextStep.Add(Cells[Convert.ToInt32(_cell.Position.X) - 1, Convert.ToInt32(_cell.Position.Y)]);
  92.                 if (_cell.Position.X < _Width - 1 && !Cells[Convert.ToInt32(_cell.Position.X) + 1, Convert.ToInt32(_cell.Position.Y)].Visited)
  93.                     nextStep.Add(Cells[Convert.ToInt32(_cell.Position.X) + 1, Convert.ToInt32(_cell.Position.Y)]);
  94.                 if (_cell.Position.Y > 0 && !Cells[Convert.ToInt32(_cell.Position.X), Convert.ToInt32(_cell.Position.Y) - 1].Visited)
  95.                     nextStep.Add(Cells[Convert.ToInt32(_cell.Position.X), Convert.ToInt32(_cell.Position.Y) - 1]);
  96.                 if (_cell.Position.Y < _Height - 1 && !Cells[Convert.ToInt32(_cell.Position.X), Convert.ToInt32(_cell.Position.Y) + 1].Visited)
  97.                     nextStep.Add(Cells[Convert.ToInt32(_cell.Position.X), Convert.ToInt32(_cell.Position.Y) + 1]);
  98.  
  99.                 if (nextStep.Count() > 0)
  100.                 {
  101.                     //выбираем сторону из возможных вариантов
  102.                     Cell next = nextStep[rand.Next(nextStep.Count())];
  103.  
  104.                     //Открываем сторону, в которую пошли на ячейках
  105.                     if (next.Position.X != _cell.Position.X)
  106.                     {
  107.                         if (_cell.Position.X - next.Position.X > 0)
  108.                         {
  109.                             _cell.Left = CellState.Open;
  110.                             next.Right = CellState.Open;
  111.                         }
  112.                         else
  113.                         {
  114.                             _cell.Right = CellState.Open;
  115.                             next.Left = CellState.Open;
  116.                         }
  117.                     }
  118.                     if (next.Position.Y != _cell.Position.Y)
  119.                     {
  120.                         if (_cell.Position.Y - next.Position.Y > 0)
  121.                         {
  122.                             _cell.Top = CellState.Open;
  123.                             next.Bottom = CellState.Open;
  124.                         }
  125.                         else
  126.                         {
  127.                             _cell.Bottom = CellState.Open;
  128.                             next.Top = CellState.Open;
  129.                         }
  130.                     }
  131.  
  132.                     next.Visited = true;
  133.                     path.Push(next);
  134.                 }
  135.                 else
  136.                 {
  137.                     //если пойти никуда нельзя, возвращаемся к предыдущему узлу
  138.                     path.Pop();
  139.                 }
  140.             }
  141.  
  142.  
  143.             renderCells();
  144.         }
  145.  
  146.         private void renderCells()
  147.         {
  148.             mCanvas.Children.Clear();
  149.             for (int y = 0; y < _Height; y++)
  150.             {
  151.                 for (int x = 0; x < _Width; x++)
  152.                 {
  153.                     if (Cells[x, y].Top == CellState.Close)
  154.                         mCanvas.Children.Add(new Line()
  155.                         {
  156.                             Stroke = Brushes.Black,
  157.                             StrokeThickness = 1,
  158.                             X1 = 20 * x,
  159.                             Y1 = 20 * y,
  160.                             X2 = 20 * x + 20,
  161.                             Y2 = 20 * y
  162.                         });
  163.  
  164.                     if (Cells[x, y].Left == CellState.Close)
  165.                         mCanvas.Children.Add(new Line()
  166.                         {
  167.                             Stroke = Brushes.Black,
  168.                             StrokeThickness = 1,
  169.                             X1 = 20 * x,
  170.                             Y1 = 20 * y,
  171.                             X2 = 20 * x,
  172.                             Y2 = 20 * y + 20
  173.                         });
  174.  
  175.                     if (Cells[x, y].Right == CellState.Close)
  176.                         mCanvas.Children.Add(new Line()
  177.                         {
  178.                             Stroke = Brushes.Black,
  179.                             StrokeThickness = 1,
  180.                             X1 = 20 * x + 20,
  181.                             Y1 = 20 * y,
  182.                             X2 = 20 * x + 20,
  183.                             Y2 = 20 * y + 20
  184.                         });
  185.  
  186.                     if (Cells[x, y].Bottom == CellState.Close)
  187.                         mCanvas.Children.Add(new Line()
  188.                         {
  189.                             Stroke = Brushes.Black,
  190.                             StrokeThickness = 1,
  191.                             X1 = 20 * x,
  192.                             Y1 = 20 * y + 20,
  193.                             X2 = 20 * x + 20,
  194.                             Y2 = 20 * y + 20
  195.                         });
  196.  
  197.                 }
  198.  
  199.             }
  200.             Int32 wallsX;
  201.             for (int k = 0; k < _Walls; k++)
  202.             {
  203.                 for (int y = 1; y < _Height - 1; y++)
  204.                 {
  205.                     for (int x = 1; x < _Width - 1; x++)
  206.                     {
  207.                         if (_Walls > 0)
  208.                         {
  209.                             wallsX = 0;
  210.                             if (Cells[x, y].Top == CellState.Close || Cells[x, y].Left == CellState.Close)
  211.                             {
  212.                                 Random randWalls = new Random();
  213.                                 wallsX = randWalls.Next(2);
  214.                             }
  215.                             if (wallsX % 2 == 0 && Cells[x, y].Top == CellState.Close)
  216.                             {
  217.                                 mCanvas.Children.Add(new Line()
  218.                                 {
  219.                                     Stroke = Brushes.Red,
  220.                                     StrokeThickness = 1,
  221.                                     X1 = 20 * x,
  222.                                     Y1 = 20 * y,
  223.                                     X2 = 20 * x + 20,
  224.                                     Y2 = 20 * y
  225.                                 });
  226.                                 _Walls--;
  227.                             }
  228.                             else if (wallsX % 2 == 0 && Cells[x, y].Left == CellState.Close)
  229.                             {
  230.                                 mCanvas.Children.Add(new Line()
  231.                                 {
  232.                                     Stroke = Brushes.Red,
  233.                                     StrokeThickness = 1,
  234.                                     X1 = 20 * x,
  235.                                     Y1 = 20 * y,
  236.                                     X2 = 20 * x,
  237.                                     Y2 = 20 * y + 20
  238.                                 });
  239.                                 _Walls--;
  240.                             }
  241.                         }
  242.                     }
  243.                 }
  244.             }
  245.         }
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement