Advertisement
Vehitsel

Untitled

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