Advertisement
Vehitsel

Untitled

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