Advertisement
Guest User

Untitled

a guest
Oct 25th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.28 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Runtime.InteropServices;
  4.  
  5. namespace ConsoleApplication1
  6. {
  7.     class Program
  8.     {
  9.         public struct Coord
  10.         {
  11.             public int Coord1, Coord2;
  12.  
  13.             public Coord(int x, int y)
  14.             {
  15.                 this.Coord1 = x;
  16.                 this.Coord2 = y;
  17.             }
  18.         }
  19.  
  20.         static public void step(int a)
  21.         {
  22.             System.Threading.Thread.Sleep(a);
  23.             //Console.Clear();
  24.         }
  25.  
  26.         public static Random _random = new Random();
  27.        
  28.         public static bool[,] fill(bool[,] M, bool[,] V, Coord T, bool a)
  29.         {
  30.  
  31.             if (((T.Coord1 < 0) || (T.Coord2 < 0)) || ((T.Coord1 > M.GetLength(0) - 1) || (T.Coord2 > M.GetLength(1) - 1)))
  32.             {
  33.                 return V;
  34.             }
  35.  
  36.             if (V[T.Coord1, T.Coord2])
  37.             {
  38.                 return V;
  39.             }
  40.  
  41.             if (M[T.Coord1, T.Coord2] != a)
  42.             {
  43.                 return V;
  44.             }
  45.  
  46.             V[T.Coord1, T.Coord2] = true;
  47.  
  48.             if (a)
  49.             {
  50.                 Console.ForegroundColor = ConsoleColor.Blue;
  51.             }
  52.             else
  53.             {
  54.                 Console.ForegroundColor = ConsoleColor.Red;
  55.             }
  56.            
  57.             Console.SetCursorPosition(T.Coord2 * 2, T.Coord1);
  58.             Console.Write("██");
  59.             Console.SetCursorPosition(0, 0);
  60.             step(1);
  61.  
  62.             V = fill(M, V, new Coord(T.Coord1 - 1, T.Coord2), a);
  63.             V = fill(M, V, new Coord(T.Coord1, T.Coord2 - 1), a);
  64.             V = fill(M, V, new Coord(T.Coord1 + 1, T.Coord2), a);
  65.             V = fill(M, V, new Coord(T.Coord1, T.Coord2 + 1), a);
  66.            
  67.             return V;
  68.         }
  69.  
  70.         [DllImport("kernel32.dll")]
  71.         static extern IntPtr GetConsoleWindow();
  72.  
  73.         [DllImport("user32.dll")]
  74.         static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  75.  
  76.         const int SW_HIDE = 0;
  77.         const int SW_MAX = 3;
  78.  
  79.         static void Main(string[] args)
  80.         {
  81.             int x = 0, y = 0, t = 0, m = 0, SIZE = 31;
  82.             bool[,] map = new bool[SIZE, SIZE];
  83.             bool[,] vist = new bool[SIZE, SIZE];
  84.             bool[,] vist2 = new bool[SIZE, SIZE];
  85.             bool testd = false, pass = true;
  86.            
  87.             Random rnd = new Random();
  88.             var handle = GetConsoleWindow();
  89.             ShowWindow(handle, 3);
  90.             Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
  91.             Console.CursorVisible = false;
  92.             Console.BackgroundColor = ConsoleColor.White;
  93.             Console.ForegroundColor = ConsoleColor.Black;
  94.             Console.Clear();
  95.            
  96.             for (x = 0; x < SIZE; x++)
  97.             {
  98.                 for (y = 0; y < SIZE; y++)
  99.                 {
  100.                     map[x, y] = rnd.Next(2) == 1;
  101.                     vist[x, y] = false;
  102.                     vist2[x, y] = false;
  103.                 }
  104.             }
  105.            
  106.  
  107.             while (pass)
  108.             {
  109.                 for (x = 0; x < SIZE; x++)
  110.                 {
  111.                     for (y = 0; y < SIZE; y++)
  112.                     {
  113.                         if (x == 0)
  114.                         {
  115.                             map[x, y] = false;
  116.                         }
  117.                         if (x == SIZE - 1)
  118.                         {
  119.                             map[x, y] = false;
  120.                         }
  121.                         if (y == 0)
  122.                         {
  123.                             map[x, y] = false;
  124.                         }
  125.                         if (y == SIZE - 1)
  126.                         {
  127.                             map[x, y] = false;
  128.                         }
  129.                     }
  130.                 }
  131.  
  132.                 pass = false;
  133.                 for (x = 1; x < SIZE; x++)
  134.                 {
  135.                     for (y = 1; y < SIZE; y++)
  136.                     {
  137.                         if (map[x - 1, y - 1] && map[x - 1, y] && map[x, y - 1] && map[x, y])
  138.                         {
  139.                             pass = true;
  140.                             testd = rnd.Next(2) == 1;
  141.                             switch (rnd.Next(2))
  142.                             {
  143.                                 case 0:
  144.                                     map[x - 1, y - 1] = testd;
  145.                                     map[x - 1, y] = testd;
  146.  
  147.                                     map[x, y - 1] = !testd;
  148.                                     map[x, y] = !testd;
  149.  
  150.                                     break;
  151.                                 case 1:
  152.                                     map[x - 1, y - 1] = !testd;
  153.                                     map[x, y - 1] = !testd;
  154.  
  155.                                     map[x - 1, y] = testd;
  156.                                     map[x, y] = testd;
  157.  
  158.                                     break;
  159.                             }
  160.                         }
  161.                         else
  162.                         {
  163.                             if (!(map[x - 1, y - 1] || map[x - 1, y] || map[x, y - 1] || map[x, y]))
  164.                             {
  165.                                 pass = true;
  166.                                 testd = rnd.Next(2) == 1;
  167.                                 switch (rnd.Next(2))
  168.                                 {
  169.                                     case 0:
  170.                                         map[x - 1, y - 1] = testd;
  171.                                         map[x - 1, y] = testd;
  172.  
  173.                                         map[x, y - 1] = !testd;
  174.                                         map[x, y] = !testd;
  175.                                         break;
  176.                                     case 1:
  177.                                         map[x - 1, y - 1] = !testd;
  178.                                         map[x, y - 1] = !testd;
  179.  
  180.                                         map[x - 1, y] = testd;
  181.                                         map[x, y] = testd;
  182.                                         break;
  183.                                 }
  184.                             }
  185.                             else
  186.                             {
  187.                                 if ((map[x - 1, y - 1] && map[x, y]) && (!map[x - 1, y] && !map[x, y - 1]))
  188.                                 {
  189.                                     pass = true;
  190.                                     switch (rnd.Next(4))
  191.                                     {
  192.                                         case 0:
  193.                                             map[x - 1, y] = true;
  194.                                             break;
  195.                                         case 1:
  196.                                             map[x, y - 1] = true;
  197.                                             break;
  198.                                         case 2:
  199.                                             map[x - 1, y - 1] = false;
  200.                                             map[x - 1, y] = true;
  201.                                             break;
  202.                                         case 3:
  203.                                             map[x, y] = false;
  204.                                             map[x, y - 1] = true;
  205.                                             break;
  206.                                     }
  207.                                 }
  208.                                 else
  209.                                 {
  210.                                     if ((map[x - 1, y] && map[x, y - 1]) && (!map[x - 1, y - 1] && !map[x, y]))
  211.                                     {
  212.                                         pass = true;
  213.                                         switch (rnd.Next(4))
  214.                                         {
  215.                                             case 0:
  216.                                                 map[x - 1, y - 1] = true;
  217.                                                 break;
  218.                                             case 1:
  219.                                                 map[x, y] = true;
  220.                                                 break;
  221.                                             case 2:
  222.                                                 map[x - 1, y - 1] = true;
  223.                                                 map[x - 1, y] = false;
  224.                                                 break;
  225.                                             case 3:
  226.                                                 map[x, y] = true;
  227.                                                 map[x, y - 1] = false;
  228.                                                 break;
  229.                                         }
  230.                                     }
  231.                                     else
  232.                                     {
  233.                                         if (x == SIZE - 1)
  234.                                         {
  235.                                             t = SIZE - 2;
  236.                                         }
  237.                                         else
  238.                                         {
  239.                                             t = x;
  240.                                         }
  241.                                         if (y == SIZE - 1)
  242.                                         {
  243.                                             m = SIZE - 2;
  244.                                         }
  245.                                         else
  246.                                         {
  247.                                             m = y;
  248.                                         }
  249.                                         if (map[t - 1, m - 1] && map[t - 1, m] && map[t - 1, m + 1] && map[t, m - 1] && map[t, m + 1] && map[t + 1, m - 1] && map[t + 1, m] && map[t - 1, m + 1] && !map[t, m])
  250.                                         {
  251.                                             pass = true;
  252.                                             switch (rnd.Next(4))
  253.                                             {
  254.                                                 case 0:
  255.                                                     map[t - 1, m] = false;
  256.                                                     break;
  257.                                                 case 1:
  258.                                                     map[t, m - 1] = false;
  259.                                                     break;
  260.                                                 case 2:
  261.                                                     map[t, m + 1] = false;
  262.                                                     break;
  263.                                                 case 3:
  264.                                                     map[t + 1, m] = false;
  265.                                                     break;
  266.                                             }
  267.                                         }
  268.                                         else
  269.                                         {
  270.                                             if (!map[t - 1, m - 1] && !map[t - 1, m] && !map[t - 1, m + 1] && !map[t, m - 1] && !map[t, m + 1] && !map[t + 1, m - 1] && !map[t + 1, m] && !map[t - 1, m + 1] && map[t, m])
  271.                                             {
  272.                                                 pass = true;
  273.                                                 switch (rnd.Next(4))
  274.                                                 {
  275.                                                     case 0:
  276.                                                         map[t - 1, m] = true;
  277.                                                         break;
  278.                                                     case 1:
  279.                                                         map[t, m - 1] = true;
  280.                                                         break;
  281.                                                     case 2:
  282.                                                         map[t, m + 1] = true;
  283.                                                         break;
  284.                                                     case 3:
  285.                                                         map[t + 1, m] = true;
  286.                                                         break;
  287.                                                 }
  288.                                             }
  289.                                         }
  290.                                     }
  291.                                 }
  292.                             }
  293.                         }
  294.                     }
  295.                 }
  296.             }
  297.            
  298.             vist2 = fill(map, vist2, new Coord(0, 0), false);
  299.  
  300.             pass = true;
  301.             while (pass)
  302.             {
  303.                 pass = false;
  304.                 for (x = 2; x < SIZE - 2; x++)
  305.                 {
  306.                     for (y = 2; y < SIZE-2; y++)
  307.                     {
  308.                         if (!map[x, y] && vist2[x, y] && map[x + 1, y] && !map[x + 2, y] && !vist2[x + 2, y])
  309.                         {
  310.                             map[x + 1, y] = false;
  311.                             vist2 = fill(map, vist2, new Coord(x + 1, y), false);
  312.                             pass = true;
  313.                         }
  314.                         else
  315.                         {
  316.                             if (!map[x, y] && vist2[x, y] && map[x - 1, y] && !map[x - 2, y] && !vist2[x - 2, y])
  317.                             {
  318.                                 map[x - 1, y] = false;
  319.                                 vist2 = fill(map, vist2, new Coord(x - 1, y), false);
  320.                                 pass = true;
  321.                             }
  322.                             else
  323.                             {
  324.                                 if (!map[x, y] && vist2[x, y] && map[x, y+1] && !map[x, y+2] && !vist2[x, y+2])
  325.                                 {
  326.                                     map[x, y+1] = false;
  327.                                     vist2 = fill(map, vist2, new Coord(x, y+1), false);
  328.                                     pass = true;
  329.                                 }
  330.                                 else
  331.                                 {
  332.                                     if (!map[x, y] && vist2[x, y] && map[x, y - 1] && !map[x, y - 2] && !vist2[x, y - 2])
  333.                                     {
  334.                                         map[x, y - 1] = false;
  335.                                         vist2 = fill(map, vist2, new Coord(x, y - 1), false);
  336.                                         pass = true;
  337.                                     }
  338.                                 }
  339.                             }
  340.                         }
  341.                     }
  342.                 }
  343.             }
  344.  
  345.             vist = fill(map, vist, new Coord(1, 1), true);
  346.  
  347.             pass = true;
  348.             while (pass)
  349.             {
  350.                 pass = false;
  351.                 for (x = 2; x < SIZE - 2; x++)
  352.                 {
  353.                     for (y = 2; y < SIZE-2; y++)
  354.                     {
  355.                         if (map[x, y] && vist[x, y] && !map[x + 1, y] && map[x + 2, y] && !vist[x + 2, y])
  356.                         {
  357.                             map[x + 1, y] = true;
  358.                             vist = fill(map, vist, new Coord(x + 1, y), true);
  359.                             pass = true;
  360.                         }
  361.                         else
  362.                         {
  363.                             if (map[x, y] && vist[x, y] && !map[x - 1, y] && map[x - 2, y] && !vist[x - 2, y])
  364.                             {
  365.                                 map[x - 1, y] = true;
  366.                                 vist = fill(map, vist, new Coord(x - 1, y), true);
  367.                                 pass = true;
  368.                             }
  369.                             else
  370.                             {
  371.                                 if (map[x, y] && vist[x, y] && !map[x, y+1] && map[x, y+2] && !vist[x, y+2])
  372.                                 {
  373.                                     map[x, y+1] = true;
  374.                                     vist = fill(map, vist, new Coord(x, y+1), true);
  375.                                     pass = true;
  376.                                 }
  377.                                 else
  378.                                 {
  379.                                     if (map[x, y] && vist[x, y] && !map[x, y - 1] && map[x, y - 2] && !vist[x, y - 2])
  380.                                     {
  381.                                         map[x, y - 1] = true;
  382.                                         vist = fill(map, vist, new Coord(x, y - 1), true);
  383.                                         pass = true;
  384.                                     }
  385.                                 }
  386.                             }
  387.                         }
  388.                     }
  389.                 }
  390.             }
  391.  
  392.             map[0, 1]=true;
  393.             map[SIZE - 1, SIZE-2] = true;
  394.             Console.BackgroundColor = ConsoleColor.White;
  395.             Console.ForegroundColor = ConsoleColor.Black;
  396.  
  397.             for (x = 0; x < SIZE; x++)
  398.             {
  399.                 for (y = 0; y < SIZE; y++)
  400.                 {
  401.                     if (map[x, y])
  402.                     {
  403.                         Console.Write("  ");
  404.                     }
  405.                     else
  406.                     {
  407.                         Console.Write("██");
  408.                     }
  409.  
  410.                     if (y == SIZE - 1)
  411.                     {
  412.                         Console.WriteLine("");
  413.                     }
  414.                 }
  415.             }
  416.             Console.Read();
  417.         }
  418.     }
  419. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement