Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Runtime.InteropServices;
- namespace ConsoleApplication1
- {
- class Program
- {
- public struct Coord
- {
- public int Coord1, Coord2;
- public Coord(int x, int y)
- {
- this.Coord1 = x;
- this.Coord2 = y;
- }
- }
- static public void step(int a)
- {
- System.Threading.Thread.Sleep(a);
- //Console.Clear();
- }
- public static Random _random = new Random();
- public static bool[,] fill(bool[,] M, bool[,] V, Coord T, bool a)
- {
- if (((T.Coord1 < 0) || (T.Coord2 < 0)) || ((T.Coord1 > M.GetLength(0) - 1) || (T.Coord2 > M.GetLength(1) - 1)))
- {
- return V;
- }
- if (V[T.Coord1, T.Coord2])
- {
- return V;
- }
- if (M[T.Coord1, T.Coord2] != a)
- {
- return V;
- }
- V[T.Coord1, T.Coord2] = true;
- if (a)
- {
- Console.ForegroundColor = ConsoleColor.Blue;
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- }
- Console.SetCursorPosition(T.Coord2 * 2, T.Coord1);
- Console.Write("██");
- Console.SetCursorPosition(0, 0);
- step(1);
- V = fill(M, V, new Coord(T.Coord1 - 1, T.Coord2), a);
- V = fill(M, V, new Coord(T.Coord1, T.Coord2 - 1), a);
- V = fill(M, V, new Coord(T.Coord1 + 1, T.Coord2), a);
- V = fill(M, V, new Coord(T.Coord1, T.Coord2 + 1), a);
- return V;
- }
- [DllImport("kernel32.dll")]
- static extern IntPtr GetConsoleWindow();
- [DllImport("user32.dll")]
- static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
- const int SW_HIDE = 0;
- const int SW_MAX = 3;
- static void Main(string[] args)
- {
- int x = 0, y = 0, t = 0, m = 0, SIZE = 31;
- bool[,] map = new bool[SIZE, SIZE];
- bool[,] vist = new bool[SIZE, SIZE];
- bool[,] vist2 = new bool[SIZE, SIZE];
- bool testd = false, pass = true;
- Random rnd = new Random();
- var handle = GetConsoleWindow();
- ShowWindow(handle, 3);
- Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
- Console.CursorVisible = false;
- Console.BackgroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.Black;
- Console.Clear();
- for (x = 0; x < SIZE; x++)
- {
- for (y = 0; y < SIZE; y++)
- {
- map[x, y] = rnd.Next(2) == 1;
- vist[x, y] = false;
- vist2[x, y] = false;
- }
- }
- while (pass)
- {
- for (x = 0; x < SIZE; x++)
- {
- for (y = 0; y < SIZE; y++)
- {
- if (x == 0)
- {
- map[x, y] = false;
- }
- if (x == SIZE - 1)
- {
- map[x, y] = false;
- }
- if (y == 0)
- {
- map[x, y] = false;
- }
- if (y == SIZE - 1)
- {
- map[x, y] = false;
- }
- }
- }
- pass = false;
- for (x = 1; x < SIZE; x++)
- {
- for (y = 1; y < SIZE; y++)
- {
- if (map[x - 1, y - 1] && map[x - 1, y] && map[x, y - 1] && map[x, y])
- {
- pass = true;
- testd = rnd.Next(2) == 1;
- switch (rnd.Next(2))
- {
- case 0:
- map[x - 1, y - 1] = testd;
- map[x - 1, y] = testd;
- map[x, y - 1] = !testd;
- map[x, y] = !testd;
- break;
- case 1:
- map[x - 1, y - 1] = !testd;
- map[x, y - 1] = !testd;
- map[x - 1, y] = testd;
- map[x, y] = testd;
- break;
- }
- }
- else
- {
- if (!(map[x - 1, y - 1] || map[x - 1, y] || map[x, y - 1] || map[x, y]))
- {
- pass = true;
- testd = rnd.Next(2) == 1;
- switch (rnd.Next(2))
- {
- case 0:
- map[x - 1, y - 1] = testd;
- map[x - 1, y] = testd;
- map[x, y - 1] = !testd;
- map[x, y] = !testd;
- break;
- case 1:
- map[x - 1, y - 1] = !testd;
- map[x, y - 1] = !testd;
- map[x - 1, y] = testd;
- map[x, y] = testd;
- break;
- }
- }
- else
- {
- if ((map[x - 1, y - 1] && map[x, y]) && (!map[x - 1, y] && !map[x, y - 1]))
- {
- pass = true;
- switch (rnd.Next(4))
- {
- case 0:
- map[x - 1, y] = true;
- break;
- case 1:
- map[x, y - 1] = true;
- break;
- case 2:
- map[x - 1, y - 1] = false;
- map[x - 1, y] = true;
- break;
- case 3:
- map[x, y] = false;
- map[x, y - 1] = true;
- break;
- }
- }
- else
- {
- if ((map[x - 1, y] && map[x, y - 1]) && (!map[x - 1, y - 1] && !map[x, y]))
- {
- pass = true;
- switch (rnd.Next(4))
- {
- case 0:
- map[x - 1, y - 1] = true;
- break;
- case 1:
- map[x, y] = true;
- break;
- case 2:
- map[x - 1, y - 1] = true;
- map[x - 1, y] = false;
- break;
- case 3:
- map[x, y] = true;
- map[x, y - 1] = false;
- break;
- }
- }
- else
- {
- if (x == SIZE - 1)
- {
- t = SIZE - 2;
- }
- else
- {
- t = x;
- }
- if (y == SIZE - 1)
- {
- m = SIZE - 2;
- }
- else
- {
- m = y;
- }
- 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])
- {
- pass = true;
- switch (rnd.Next(4))
- {
- case 0:
- map[t - 1, m] = false;
- break;
- case 1:
- map[t, m - 1] = false;
- break;
- case 2:
- map[t, m + 1] = false;
- break;
- case 3:
- map[t + 1, m] = false;
- break;
- }
- }
- else
- {
- 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])
- {
- pass = true;
- switch (rnd.Next(4))
- {
- case 0:
- map[t - 1, m] = true;
- break;
- case 1:
- map[t, m - 1] = true;
- break;
- case 2:
- map[t, m + 1] = true;
- break;
- case 3:
- map[t + 1, m] = true;
- break;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- vist2 = fill(map, vist2, new Coord(0, 0), false);
- pass = true;
- while (pass)
- {
- pass = false;
- for (x = 2; x < SIZE - 2; x++)
- {
- for (y = 2; y < SIZE-2; y++)
- {
- if (!map[x, y] && vist2[x, y] && map[x + 1, y] && !map[x + 2, y] && !vist2[x + 2, y])
- {
- map[x + 1, y] = false;
- vist2 = fill(map, vist2, new Coord(x + 1, y), false);
- pass = true;
- }
- else
- {
- if (!map[x, y] && vist2[x, y] && map[x - 1, y] && !map[x - 2, y] && !vist2[x - 2, y])
- {
- map[x - 1, y] = false;
- vist2 = fill(map, vist2, new Coord(x - 1, y), false);
- pass = true;
- }
- else
- {
- if (!map[x, y] && vist2[x, y] && map[x, y+1] && !map[x, y+2] && !vist2[x, y+2])
- {
- map[x, y+1] = false;
- vist2 = fill(map, vist2, new Coord(x, y+1), false);
- pass = true;
- }
- else
- {
- if (!map[x, y] && vist2[x, y] && map[x, y - 1] && !map[x, y - 2] && !vist2[x, y - 2])
- {
- map[x, y - 1] = false;
- vist2 = fill(map, vist2, new Coord(x, y - 1), false);
- pass = true;
- }
- }
- }
- }
- }
- }
- }
- vist = fill(map, vist, new Coord(1, 1), true);
- pass = true;
- while (pass)
- {
- pass = false;
- for (x = 2; x < SIZE - 2; x++)
- {
- for (y = 2; y < SIZE-2; y++)
- {
- if (map[x, y] && vist[x, y] && !map[x + 1, y] && map[x + 2, y] && !vist[x + 2, y])
- {
- map[x + 1, y] = true;
- vist = fill(map, vist, new Coord(x + 1, y), true);
- pass = true;
- }
- else
- {
- if (map[x, y] && vist[x, y] && !map[x - 1, y] && map[x - 2, y] && !vist[x - 2, y])
- {
- map[x - 1, y] = true;
- vist = fill(map, vist, new Coord(x - 1, y), true);
- pass = true;
- }
- else
- {
- if (map[x, y] && vist[x, y] && !map[x, y+1] && map[x, y+2] && !vist[x, y+2])
- {
- map[x, y+1] = true;
- vist = fill(map, vist, new Coord(x, y+1), true);
- pass = true;
- }
- else
- {
- if (map[x, y] && vist[x, y] && !map[x, y - 1] && map[x, y - 2] && !vist[x, y - 2])
- {
- map[x, y - 1] = true;
- vist = fill(map, vist, new Coord(x, y - 1), true);
- pass = true;
- }
- }
- }
- }
- }
- }
- }
- map[0, 1]=true;
- map[SIZE - 1, SIZE-2] = true;
- Console.BackgroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.Black;
- for (x = 0; x < SIZE; x++)
- {
- for (y = 0; y < SIZE; y++)
- {
- if (map[x, y])
- {
- Console.Write(" ");
- }
- else
- {
- Console.Write("██");
- }
- if (y == SIZE - 1)
- {
- Console.WriteLine("");
- }
- }
- }
- Console.Read();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement