Advertisement
Vehitsel

Untitled

Oct 23rd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.59 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Xml.Xsl;
  4.  
  5. namespace Maze
  6. {
  7.     class Program
  8.     {
  9.         static Random rand = new Random();
  10.         static char[,] maze;
  11.         static int x = 1;
  12.         static int y = 1;
  13.         static int n = 0;
  14.         static bool exit = false;
  15.         static int count = 0;
  16.         static void Main(string[] args)
  17.         {
  18.             if (int.TryParse(Console.ReadLine(), out n))
  19.             {
  20.                 maze = new char[n, n];
  21.                 for (int i = 0; i < n; i++)
  22.                 {
  23.                     for (int j = 0; j < n; j++)
  24.                     {
  25.                         maze[i, j] = '#';
  26.                     }
  27.                 }
  28.  
  29.                 maze[1, 1] = ' ';
  30.                 for (int i = 0; i < n; i++)
  31.                 {
  32.                     for (int j = 0; j < n; j++)
  33.                     {
  34.                         Console.Write("{0} ", maze[j, i]);
  35.                     }
  36.  
  37.                     Console.WriteLine(" ");
  38.                 }
  39.  
  40.                 CreateMaze();
  41.                 Console.WriteLine(" ");
  42.                 for (int i = 0; i < n; i++)
  43.                 {
  44.                     for (int j = 0; j < n; j++)
  45.                     {
  46.                         Console.Write("{0} ", maze[j, i]);
  47.                     }
  48.  
  49.                     Console.WriteLine(" ");
  50.                 }
  51.             }
  52.             Console.ReadKey();
  53.         }
  54.  
  55.         static void CreateMaze()
  56.         {
  57.             CreateWay();
  58.             while (!CheckComplite())
  59.             {
  60.                 count++;
  61.                 while (GenerateCoords())
  62.                 {
  63.                     int x1 = x;
  64.                     int y1 = y;
  65.                     for (int i = 0; i < 3; i++)
  66.                     {
  67.                         CreateWay();
  68.                         x = x1;
  69.                         y = y1;
  70.                     }
  71.                 }
  72.             }
  73.  
  74.             maze[0, 1] = '>';
  75.             for (int i = n - 2; i > 1; i--)
  76.             {
  77.                 if (maze[n - 2, i] == ' ')
  78.                 {
  79.                     maze[n - 1, i] = '<';
  80.                     break;
  81.                 }
  82.             }
  83.         }
  84.  
  85.         static bool CheckComplite()
  86.         {
  87.             int countWalls = 0;
  88.             int countAir = 0;
  89.             for (int i = 0; i < n; i++)
  90.             {
  91.                 for (int j = 0; j < n; j++)
  92.                 {
  93.                     if (maze[i, j] == ' ')
  94.                     {
  95.                         countAir++;
  96.                     }
  97.                     else
  98.                     {
  99.                         countWalls++;
  100.                     }
  101.                 }
  102.             }
  103.  
  104.             if ((countWalls - countAir < n*3)||(count > 2000))
  105.             {
  106.                 return true;
  107.             }
  108.  
  109.             return false;
  110.         }
  111.  
  112.         static bool GenerateCoords()
  113.         {
  114.             x = rand.Next(1, n - 1);
  115.             y = rand.Next(1, n - 1);
  116.             if ((ChooseDirection() != "error") && (maze[x, y] != '#'))
  117.             {
  118.                 return true;
  119.             }
  120.  
  121.             return false;
  122.         }
  123.         static void CreateWay()
  124.         {
  125.             while (Way()) { }
  126.  
  127.         }
  128.  
  129.         static bool Way()
  130.         {
  131.             string direction = ChooseDirection();
  132.             if (direction != "error")
  133.             {
  134.                 Dig(direction);
  135.  
  136.                 return true;
  137.             }
  138.             else
  139.             {
  140.                 return false;
  141.             }
  142.  
  143.  
  144.         }
  145.         static string ChooseDirection()
  146.         {
  147.             if ((x - 1 >= 0) && (y - 1 >= 0) && (x + 1 < n) && (y + 1 < n))
  148.             {
  149.                 string[] directions = { "left", "right", "up", "down" };
  150.                 if ((maze[x - 1, y - 1] == ' '))
  151.                 {
  152.                     directions = Exclude(directions, "left");
  153.                     directions = Exclude(directions, "up");
  154.                 }
  155.  
  156.                 if (maze[x - 1, y + 1] == ' ')
  157.                 {
  158.                     directions = Exclude(directions, "left");
  159.                     directions = Exclude(directions, "down");
  160.                 }
  161.  
  162.                 if (maze[x + 1, y - 1] == ' ')
  163.                 {
  164.                     directions = Exclude(directions, "right");
  165.                     directions = Exclude(directions, "up");
  166.                 }
  167.  
  168.                 if (maze[x + 1, y + 1] == ' ')
  169.                 {
  170.                     directions = Exclude(directions, "right");
  171.                     directions = Exclude(directions, "down");
  172.                 }
  173.  
  174.                 if (maze[x + 1, y] == ' ')
  175.                 {
  176.                     directions = Exclude(directions, "right");
  177.                 }
  178.  
  179.                 if (maze[x - 1, y] == ' ')
  180.                 {
  181.                     directions = Exclude(directions, "left");
  182.                 }
  183.  
  184.                 if (maze[x, y + 1] == ' ')
  185.                 {
  186.                     directions = Exclude(directions, "down");
  187.                 }
  188.  
  189.                 if (maze[x, y - 1] == ' ')
  190.                 {
  191.                     directions = Exclude(directions, "up");
  192.                 }
  193.  
  194.                 if (x - 1 <= 0)
  195.                 {
  196.                     directions = Exclude(directions, "left");
  197.                 }
  198.  
  199.                 if ((exit == false) && (x + 1 >= n - 1))
  200.                 {
  201.                 }
  202.                 else if (x + 1 >= n - 1)
  203.                 {
  204.                     directions = Exclude(directions, "right");
  205.                 }
  206.  
  207.                 if (y - 1 <= 0)
  208.                 {
  209.                     directions = Exclude(directions, "up");
  210.                 }
  211.  
  212.                 if (y + 1 >= n - 1)
  213.                 {
  214.                     directions = Exclude(directions, "down");
  215.                 }
  216.  
  217.                 if (y + 2 < n)
  218.                 {
  219.                     if (maze[x, y + 2] == ' ')
  220.                     {
  221.                         directions = Exclude(directions, "down");
  222.                     }
  223.                     if ((x + 2 < n) && (maze[x + 2, y + 2] == ' '))
  224.                     {
  225.                         directions = Exclude(directions, "down");
  226.                         directions = Exclude(directions, "right");
  227.                     }
  228.                     if ((x - 2 >= 0) && (maze[x - 2, y + 2] == ' '))
  229.                     {
  230.                         directions = Exclude(directions, "down");
  231.                         directions = Exclude(directions, "left");
  232.                     }
  233.                 }
  234.  
  235.                 if (y - 2 >= 0)
  236.                 {
  237.                     if (maze[x, y - 2] == ' ')
  238.                     {
  239.                         directions = Exclude(directions, "up");
  240.                     }
  241.                     if ((x + 2 < n) && (maze[x + 2, y - 2] == ' '))
  242.                     {
  243.                         directions = Exclude(directions, "up");
  244.                         directions = Exclude(directions, "right");
  245.                     }
  246.                     if ((x - 2 >= 0) && (maze[x - 2, y - 2] == ' '))
  247.                     {
  248.                         directions = Exclude(directions, "up");
  249.                         directions = Exclude(directions, "left");
  250.                     }
  251.                 }
  252.  
  253.                 if (x + 2 < n)
  254.                 {
  255.                     if (maze[x + 2, y] == ' ')
  256.                     {
  257.                         directions = Exclude(directions, "right");
  258.                     }
  259.                     if ((y + 2 < n) && (maze[x + 2, y + 2] == ' '))
  260.                     {
  261.                         directions = Exclude(directions, "down");
  262.                         directions = Exclude(directions, "right");
  263.                     }
  264.                     if ((y - 2 >= 0) && (maze[x + 2, y - 2] == ' '))
  265.                     {
  266.                         directions = Exclude(directions, "up");
  267.                         directions = Exclude(directions, "right");
  268.                     }
  269.                 }
  270.                 if (x - 2 >= 0)
  271.                 {
  272.                     if (maze[x - 2, y] == ' ')
  273.                     {
  274.                         directions = Exclude(directions, "left");
  275.                     }
  276.                     if ((y - 2 >= 0) && (maze[x - 2, y - 2] == ' '))
  277.                     {
  278.                         directions = Exclude(directions, "up");
  279.                         directions = Exclude(directions, "left");
  280.                     }
  281.                     if ((y + 2 < n) && (maze[x - 2, y + 2] == ' '))
  282.                     {
  283.                         directions = Exclude(directions, "down");
  284.                         directions = Exclude(directions, "left");
  285.                     }
  286.                 }
  287.  
  288.  
  289.                 if (directions != null)
  290.                 {
  291.                     int r = rand.Next(0, directions.Length);
  292.                     if (directions[r] == "right")
  293.                     {
  294.                         exit = true;
  295.                     }
  296.                     return directions[r];
  297.                 }
  298.                 return "error";
  299.  
  300.             }
  301.             else
  302.             {
  303.                 return "error";
  304.             }
  305.         }
  306.  
  307.         static string[] Exclude(string[] array, string element)
  308.         {
  309.             if (array == null)
  310.             {
  311.                 return null;
  312.             }
  313.             int n = -1;
  314.             for (int i = 0; i < array.Length; i++)
  315.             {
  316.                 if (Equals(array[i], element))
  317.                 {
  318.                     n = i;
  319.                 }
  320.             }
  321.             if (n != -1)
  322.             {
  323.                 if (array.Length != 1)
  324.                 {
  325.                     for (int i = n; i < array.Length - 1; i++)
  326.                     {
  327.                         array[i] = array[i + 1];
  328.                     }
  329.  
  330.                     Array.Resize(ref array, array.Length - 1);
  331.                 }
  332.                 else
  333.                 {
  334.                     return null;
  335.                 }
  336.             }
  337.  
  338.             return array;
  339.         }
  340.  
  341.         static void Dig(string direction)
  342.         {
  343.             switch (direction)
  344.             {
  345.                 case "left":
  346.                     maze[x - 1, y] = ' ';
  347.                     x--;
  348.                     break;
  349.                 case "right":
  350.                     maze[x + 1, y] = ' ';
  351.                     x++;
  352.                     break;
  353.                 case "up":
  354.                     maze[x, y - 1] = ' ';
  355.                     y--;
  356.                     break;
  357.                 case "down":
  358.                     maze[x, y + 1] = ' ';
  359.                     y++;
  360.                     break;
  361.             }
  362.         }
  363.     }
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement