Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.95 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.  
  15.         static void Main(string[] args)
  16.         {
  17.             if (int.TryParse(Console.ReadLine(), out n))
  18.             {
  19.                 maze = new char[n, n];
  20.                 for (int i = 0; i < n; i++)
  21.                 {
  22.                     for (int j = 0; j < n; j++)
  23.                     {
  24.                         maze[i, j] = '#';
  25.                     }
  26.                 }
  27.  
  28.                 maze[1, 1] = ' ';
  29.                 for (int i = 0; i < n; i++)
  30.                 {
  31.                     for (int j = 0; j < n; j++)
  32.                     {
  33.                         Console.Write("{0} ", maze[j, i]);
  34.                     }
  35.  
  36.                     Console.WriteLine(" ");
  37.                 }
  38.                
  39.                 CreateMaze();
  40.                 Console.WriteLine(" ");
  41.                 for (int i = 0; i < n; i++)
  42.                 {
  43.                     for (int j = 0; j < n; j++)
  44.                     {
  45.                         Console.Write("{0}", maze[j, i]);
  46.                     }
  47.  
  48.                     Console.WriteLine(" ");
  49.                 }
  50.             }
  51.         }
  52.  
  53.         static void CreateMaze()
  54.         {
  55.             CreateWay();
  56.             while (!CheckComplite())
  57.             {
  58.                 while (GenerateCoords())
  59.                 {
  60.                     CreateWay();
  61.                 }
  62.             }
  63.  
  64.             maze[0, 1] = '>';
  65.             for (int i = n-2; i > 1; i--)
  66.             {
  67.                 if (maze[n - 2, i] == ' ')
  68.                 {
  69.                     maze[n - 1, i] = '<';
  70.                     break;
  71.                 }
  72.             }
  73.         }
  74.  
  75.         static bool CheckComplite()
  76.         {
  77.             int countWalls = 0;
  78.             int countAir = 0;
  79.             for (int i = 0; i < n; i++)
  80.             {
  81.                 for (int j = 0; j < n; j++)
  82.                 {
  83.                     if (maze[i, j] == ' ')
  84.                     {
  85.                         countAir++;
  86.                     }
  87.                     else
  88.                     {
  89.                         countWalls++;
  90.                     }
  91.                 }
  92.             }
  93.  
  94.             if (countAir >= countWalls - n/2)//условие отношения пустоты к стенам клятым
  95.             {
  96.                 return true;
  97.             }
  98.  
  99.             return false;
  100.         }
  101.  
  102.         static bool GenerateCoords()
  103.         {
  104.             x = rand.Next(1, n - 1);
  105.             y = rand.Next(1, n - 1);
  106.             if ((ChooseDirection() != "error") && (maze[x, y] != '#'))
  107.             {
  108.                 return true;
  109.             }
  110.  
  111.             return false;
  112.         }
  113.         static void CreateWay()
  114.         {
  115.             while (Way()){}
  116.            
  117.         }
  118.  
  119.         static bool Way()
  120.         {
  121.             string direction = ChooseDirection();
  122.             Console.Write($" {direction}");
  123.             if (direction != "error"){
  124.                 Dig(direction);
  125.                
  126.                 return true;
  127.             }
  128.             else
  129.             {
  130.                 return false;
  131.             }
  132.  
  133.  
  134.         }
  135.         static string ChooseDirection()
  136.         {
  137.             if ((x - 1 >= 0) && (y - 1 >= 0) && (x + 1 < n) && (y + 1 < n))
  138.             {
  139.                 string[] directions = {"left","right","up","down"};
  140.                 if ((maze[x - 1, y - 1] == ' '))
  141.                 {
  142.                     directions = Exclude(directions, "left");
  143.                     directions = Exclude(directions, "up");
  144.                 }
  145.                
  146.                 if (maze[x - 1, y + 1] == ' ')
  147.                 {
  148.                     directions = Exclude(directions, "left");
  149.                     directions = Exclude(directions, "down");
  150.                 }
  151.  
  152.                 if (maze[x + 1, y - 1] == ' ')
  153.                 {
  154.                     directions = Exclude(directions, "right");
  155.                     directions = Exclude(directions, "up");
  156.                 }
  157.  
  158.                 if (maze[x + 1, y + 1] == ' ')
  159.                 {
  160.                     directions = Exclude(directions, "right");
  161.                     directions = Exclude(directions, "down");
  162.                 }
  163.  
  164.                 if (maze[x + 1, y] == ' ')
  165.                 {
  166.                     directions = Exclude(directions, "right");
  167.                 }
  168.  
  169.                 if (maze[x - 1, y] == ' ')
  170.                 {
  171.                     directions = Exclude(directions, "left");
  172.                 }
  173.  
  174.                 if (maze[x, y + 1] == ' ')
  175.                 {
  176.                     directions = Exclude(directions, "down");
  177.                 }
  178.  
  179.                 if (maze[x, y - 1] == ' ')
  180.                 {
  181.                     directions =  Exclude(directions, "up");
  182.                 }
  183.  
  184.                 if (x - 1 <= 0)
  185.                 {
  186.                     directions = Exclude(directions, "left");
  187.                 }
  188.  
  189.                 if (x + 1 >= n - 1)
  190.                 {
  191.                     directions = Exclude(directions, "right");
  192.                 }
  193.  
  194.                 if (y - 1 <= 0)
  195.                 {
  196.                     directions = Exclude(directions, "up");
  197.                 }
  198.  
  199.                 if (y + 1 >= n - 1)
  200.                 {
  201.                     directions = Exclude(directions, "down");
  202.                 }
  203.  
  204.                 if ((y + 2 < n) && (maze[x, y + 2] == ' '))
  205.                 {
  206.                     directions = Exclude(directions, "down");
  207.                 }
  208.  
  209.                 if ((y - 2 >= 0) && (maze[x, y - 2] == ' '))
  210.                 {
  211.                     directions = Exclude(directions, "up");
  212.                 }
  213.  
  214.                 if ((x + 2 < n) && (maze[x + 2, y] == ' '))
  215.                 {
  216.                     directions = Exclude(directions, "right");
  217.                 }
  218.  
  219.                 if ((x - 2 >= 0) && (maze[x - 2, y] == ' '))
  220.                 {
  221.                     directions = Exclude(directions, "left");
  222.                 }
  223.  
  224.                 if (directions != null)
  225.                 {
  226.                     int r = rand.Next(0, directions.Length);
  227.                     return directions[r];
  228.                 }
  229.                 return "error";
  230.  
  231.             }
  232.             else
  233.             {
  234.                 return "error";
  235.             }            
  236.         }
  237.  
  238.         static string[] Exclude(string[] array, string element)
  239.         {
  240.             if (array == null)
  241.             {
  242.                 return null;
  243.             }
  244.             int n = -1;
  245.             for (int i = 0; i < array.Length; i++)
  246.             {
  247.                 if (Equals(array[i], element))
  248.                 {
  249.                     n = i;
  250.                 }
  251.             }
  252.             if (n != -1)
  253.             {
  254.                 if (array.Length != 1)
  255.                 {
  256.                     for (int i = n; i < array.Length - 1; i++)
  257.                     {
  258.                         array[i] = array[i + 1];
  259.                     }
  260.  
  261.                     Array.Resize(ref array, array.Length - 1);
  262.                 }
  263.                 else
  264.                 {
  265.                     return null;
  266.                 }
  267.             }
  268.  
  269.             return array;
  270.         }
  271.  
  272.         static void Dig(string direction)
  273.         {
  274.             switch (direction)
  275.             {
  276.                 case "left":
  277.                     maze[x - 1, y] = ' ';
  278.                     x--;
  279.                     break;
  280.                 case "right":
  281.                     maze[x + 1, y] = ' ';
  282.                     x++;
  283.                     break;
  284.                 case "up":
  285.                     maze[x, y - 1] = ' ';
  286.                     y--;
  287.                     break;
  288.                 case "down":
  289.                     maze[x, y + 1] = ' ';
  290.                     y++;
  291.                     break;
  292.             }
  293.         }
  294.     }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement