Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. private static bool[,] InitMarked(int size)
  2. {
  3. bool[,] marked = new bool[size, size];
  4. for (int i = 0; i < size; i++)
  5. {
  6. for (int j = 0; j < size; j++)
  7. marked[i, j] = false;
  8. }
  9.  
  10. return marked;
  11. }
  12.  
  13. // Checks if the cell at index [i,j] is already in the frontier
  14. private static bool Exists(List<(int, int)> frontier, int i, int j)
  15. {
  16. for (int index = 0; index < frontier.Count; index++)
  17. {
  18. if (frontier[index].Item1 == i && frontier[index].Item2 == j)
  19. return true;
  20. }
  21. return false;
  22. }
  23.  
  24. private static void Mark(bool[,] marked, List<(int, int)> frontier, int i, int j)
  25. {
  26. //TODO
  27. // Marks the cell [i, j] as visited and add its non visited neighbour to the frontier
  28. marked[i,j] = true;
  29. if (i> 0 && !marked[i-1,j] && !Exists(frontier,i-1,j))
  30. {
  31. frontier.Add((i-1,j));
  32. }
  33. if (j < marked.GetLength(0)-1 && !marked[i,j+1] && !Exists(frontier,i,j+1))
  34. {
  35. frontier.Add((i,j+1));
  36. }
  37.  
  38. if (i < marked.GetLength(0)-1 && !marked[i+1,j] && !Exists(frontier,i+1,j))
  39. {
  40. frontier.Add((i+1,j));
  41. }
  42. if (j> 0 && !marked[i,j-1] && !Exists(frontier,i,j-1))
  43. {
  44. frontier.Add((i,j-1));
  45. }
  46. }
  47.  
  48. private static Maze.Direction FindNeighbours(bool[,] marked, int i, int j)
  49. {
  50. //TODO
  51. // Finds the directions in which marked neighbours exists
  52. // Chooses one randomly and returns it
  53. // If none exists, return the NULL direction
  54.  
  55. List<(int,int)> l = new List<(int,int)>();
  56. if (i>0 && marked[i-1,j])
  57. {
  58. l.Add((i-1,j));
  59. }
  60. if (j<marked.GetLength(0)-1 && marked[i,j+1])
  61. {
  62. l.Add((i,j+1));
  63. }
  64. if (i<marked.GetLength(0)-1 && marked[i+1,j])
  65. {
  66. l.Add((i+1,j));
  67. }
  68. if (j>0 && marked[i,j-1])
  69. {
  70. l.Add((i,j-1));
  71. }
  72. Random a = new Random();
  73. int b = a.Next(l.Count);
  74. int x = l[b].Item1;
  75. int y = l[b].Item2;
  76. if (x<i)
  77. {
  78. return Maze.Direction.NORTH;
  79. }
  80. else if (x>i)
  81. {
  82. return (Maze.Direction.SOUTH);
  83. }
  84. else if ( y> j)
  85. {
  86. return (Maze.Direction.EAST);
  87. }
  88. else if ( y< j)
  89. {
  90. return (Maze.Direction.WEST);
  91. }
  92. else
  93. {
  94. return Maze.Direction.NULL;
  95. }
  96.  
  97. }
  98.  
  99. public static Maze GenPrimMaze(int size)
  100. {
  101.  
  102. //TODO
  103. // Generates a maze using Prim's algorithm:
  104. // 1. Choose a random cell
  105. // 2. Mark it and add its neighbours to the frontier
  106. // 3. Choose a random cell in the frontier and remove it from the frontier
  107. // 4. Carve path from this cell to an already marked cell
  108. // 5. Repeat from step 3 until nothing is left in the frontier
  109. Maze maze = new Maze(size);
  110. Random x = new Random();
  111. int i = x.Next(size );
  112. int j = x.Next(size );
  113. List<(int, int)> frontier = new List<(int, int)>();
  114. bool[,] marked = InitMarked(size);
  115. Mark(marked, frontier, i, j);
  116. while (frontier.Count > 0)
  117. {
  118. int g = x.Next(frontier.Count);
  119. int m = frontier[g].Item1;
  120. int n = frontier[g].Item2;
  121.  
  122. maze.CarvePath(m, n, FindNeighbours(marked, m, n));
  123. Mark(marked, frontier, m, n);
  124. frontier.Remove((m, n));
  125. }
  126.  
  127. return maze;
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement