Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Xml.Xsl;
- namespace Maze
- {
- class Program
- {
- static Random rand = new Random();
- static char[,] maze;
- static int x = 1;
- static int y = 1;
- static int n = 0;
- static bool exit = false;
- static int count = 0;
- static void Main(string[] args)
- {
- if (int.TryParse(Console.ReadLine(), out n))
- {
- maze = new char[n, n];
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- maze[i, j] = 'X';
- }
- }
- maze[1, 1] = ' ';
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- Console.Write("{0} ", maze[j, i]);
- }
- Console.WriteLine(" ");
- }
- CreateMaze();
- Console.WriteLine(" ");
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- Console.Write("{0} ", maze[j, i]);
- }
- Console.WriteLine(" ");
- }
- }
- Console.ReadKey();
- }
- static void CreateMaze()
- {
- CreateWay();
- while (!CheckComplite())
- {
- x = 1;
- y = 1;
- count++;
- while (GenerateCoords())
- {
- int x1 = x;
- int y1 = y;
- for (int i = 0; i < 3; i++)
- {
- CreateWay();
- x = x1;
- y = y1;
- }
- }
- }
- maze[0, 1] = '>';
- for (int i = n - 2; i > 0; i--)
- {
- if (maze[n - 1, i] == ' ')
- {
- maze[n - 1, i] = '<';
- break;
- }
- }
- }
- static bool CheckComplite()
- {
- int countWalls = 0;
- int countAir = 0;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (maze[i, j] == ' ')
- {
- countAir++;
- }
- else
- {
- countWalls++;
- }
- }
- }
- if ((countWalls - countAir < n*3)||(count > 100000))
- {
- return true;
- }
- return false;
- }
- static bool GenerateCoords()
- {
- int x1 = x;
- int y1 = y;
- x = rand.Next(1, n - 1);
- y = rand.Next(1, n - 1);
- if ((ChooseDirection() != "error") && (maze[x, y] != '#'))
- {
- return true;
- }
- x = x1;
- y = y1;
- return false;
- }
- static void CreateWay()
- {
- while (Way()) { }
- }
- static bool Way()
- {
- string direction = ChooseDirection();
- if (direction != "error")
- {
- Dig(direction);
- return true;
- }
- else
- {
- return false;
- }
- }
- static string ChooseDirection()
- {
- bool pre_end = false ;
- if ((x - 1 >= 0) && (y - 1 >= 0) && (x + 1 < n) && (y + 1 < n))
- {
- string[] directions = { "left", "right", "up", "down" };
- if ((maze[x - 1, y - 1] == ' '))
- {
- directions = Exclude(directions, "left");
- directions = Exclude(directions, "up");
- }
- if (maze[x - 1, y + 1] == ' ')
- {
- directions = Exclude(directions, "left");
- directions = Exclude(directions, "down");
- }
- if (maze[x + 1, y - 1] == ' ')
- {
- directions = Exclude(directions, "right");
- directions = Exclude(directions, "up");
- }
- if (maze[x + 1, y + 1] == ' ')
- {
- directions = Exclude(directions, "right");
- directions = Exclude(directions, "down");
- }
- if (maze[x + 1, y] == ' ')
- {
- directions = Exclude(directions, "right");
- }
- if (maze[x - 1, y] == ' ')
- {
- directions = Exclude(directions, "left");
- }
- if (maze[x, y + 1] == ' ')
- {
- directions = Exclude(directions, "down");
- }
- if (maze[x, y - 1] == ' ')
- {
- directions = Exclude(directions, "up");
- }
- if (x - 1 <= 0)
- {
- directions = Exclude(directions, "left");
- }
- if ((exit == false) && (x + 1 >= n - 1))
- {
- pre_end = true;
- }
- else if (x + 1 >= n - 1)
- {
- directions = Exclude(directions, "right");
- }
- if (y - 1 <= 0)
- {
- directions = Exclude(directions, "up");
- }
- if (y + 1 >= n - 1)
- {
- directions = Exclude(directions, "down");
- }
- if (y + 2 < n)
- {
- if (maze[x, y + 2] == ' ')
- {
- directions = Exclude(directions, "down");
- }
- if ((x + 2 < n) && (maze[x + 2, y + 2] == ' '))
- {
- directions = Exclude(directions, "down");
- directions = Exclude(directions, "right");
- }
- if ((x - 2 >= 0) && (maze[x - 2, y + 2] == ' '))
- {
- directions = Exclude(directions, "down");
- directions = Exclude(directions, "left");
- }
- }
- if (y - 2 >= 0)
- {
- if (maze[x, y - 2] == ' ')
- {
- directions = Exclude(directions, "up");
- }
- if ((x + 2 < n) && (maze[x + 2, y - 2] == ' '))
- {
- directions = Exclude(directions, "up");
- directions = Exclude(directions, "right");
- }
- if ((x - 2 >= 0) && (maze[x - 2, y - 2] == ' '))
- {
- directions = Exclude(directions, "up");
- directions = Exclude(directions, "left");
- }
- }
- if (x + 2 < n)
- {
- if (maze[x + 2, y] == ' ')
- {
- directions = Exclude(directions, "right");
- }
- if ((y + 2 < n) && (maze[x + 2, y + 2] == ' '))
- {
- directions = Exclude(directions, "down");
- directions = Exclude(directions, "right");
- }
- if ((y - 2 >= 0) && (maze[x + 2, y - 2] == ' '))
- {
- directions = Exclude(directions, "up");
- directions = Exclude(directions, "right");
- }
- }
- if (x - 2 >= 0)
- {
- if (maze[x - 2, y] == ' ')
- {
- directions = Exclude(directions, "left");
- }
- if ((y - 2 >= 0) && (maze[x - 2, y - 2] == ' '))
- {
- directions = Exclude(directions, "up");
- directions = Exclude(directions, "left");
- }
- if ((y + 2 < n) && (maze[x - 2, y + 2] == ' '))
- {
- directions = Exclude(directions, "down");
- directions = Exclude(directions, "left");
- }
- }
- if (directions != null)
- {
- int r = rand.Next(0, directions.Length);
- if ((directions[r] == "right") && (pre_end == true))
- {
- exit = true;
- }
- return directions[r];
- }
- return "error";
- }
- else
- {
- return "error";
- }
- }
- static string[] Exclude(string[] array, string element)
- {
- if (array == null)
- {
- return null;
- }
- int n = -1;
- for (int i = 0; i < array.Length; i++)
- {
- if (Equals(array[i], element))
- {
- n = i;
- }
- }
- if (n != -1)
- {
- if (array.Length != 1)
- {
- for (int i = n; i < array.Length - 1; i++)
- {
- array[i] = array[i + 1];
- }
- Array.Resize(ref array, array.Length - 1);
- }
- else
- {
- return null;
- }
- }
- return array;
- }
- static void Dig(string direction)
- {
- switch (direction)
- {
- case "left":
- maze[x - 1, y] = ' ';
- x--;
- break;
- case "right":
- maze[x + 1, y] = ' ';
- x++;
- break;
- case "up":
- maze[x, y - 1] = ' ';
- y--;
- break;
- case "down":
- maze[x, y + 1] = ' ';
- y++;
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement