Guest User

Level Generator

a guest
May 7th, 2026
546
0
Never
5
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class LevelGenerator
  6. {
  7. public int minLen = 2;
  8. public int maxLen = 5;
  9. private int gridW, gridH;
  10. private System.Random rng = new System.Random();
  11.  
  12. private readonly int[] dx = { 1, -1, 0, 0 };
  13. private readonly int[] dy = { 0, 0, 1, -1 };
  14.  
  15. public LevelGenerator(int w, int h)
  16. {
  17. gridW = w;
  18. gridH = h;
  19. }
  20.  
  21. public List<ArrowData> Generate(List<int> zoneIndices)
  22. {
  23. if (zoneIndices == null || zoneIndices.Count < minLen) return null;
  24.  
  25. LevelData currentLevel = new LevelData { GridXSize = gridW, GridYSize = gridH };
  26. HashSet<int> occupied = new HashSet<int>();
  27. HashSet<int> zone = new HashSet<int>(zoneIndices);
  28.  
  29. bool addedInPass = true;
  30.  
  31. while (addedInPass)
  32. {
  33. addedInPass = false;
  34. List<int> freeCells = zone.Where(idx => !occupied.Contains(idx)).ToList();
  35.  
  36. if (freeCells.Count < minLen) break;
  37.  
  38. ShuffleList(freeCells);
  39.  
  40. foreach (int startCell in freeCells)
  41. {
  42. for (int targetLen = maxLen; targetLen >= minLen; targetLen--)
  43. {
  44. ArrowData bestArrow = null;
  45.  
  46. int attemptsToFindHardArrow = 4;
  47. for (int attempt = 0; attempt < attemptsToFindHardArrow; attempt++)
  48. {
  49. ArrowData potentialArrow = TryCreateSnakeArrow(startCell, targetLen, occupied, zone);
  50.  
  51. if (potentialArrow != null)
  52. {
  53. currentLevel.Arrows.Add(potentialArrow);
  54.  
  55. if (currentLevel.CanSolveLevel())
  56. {
  57. bestArrow = potentialArrow;
  58.  
  59. if (IsExitBlocked(potentialArrow, occupied))
  60. {
  61. currentLevel.Arrows.RemoveAt(currentLevel.Arrows.Count - 1);
  62. break;
  63. }
  64. }
  65.  
  66. currentLevel.Arrows.RemoveAt(currentLevel.Arrows.Count - 1);
  67. }
  68. }
  69.  
  70. if (bestArrow != null)
  71. {
  72. currentLevel.Arrows.Add(bestArrow);
  73. foreach (int idx in bestArrow.Indices) occupied.Add(idx);
  74. addedInPass = true;
  75. break;
  76. }
  77. }
  78.  
  79. if (addedInPass) break;
  80. }
  81. }
  82.  
  83. return currentLevel.Arrows;
  84. }
  85.  
  86. private bool IsExitBlocked(ArrowData arrow, HashSet<int> occupied)
  87. {
  88. int headIdx = arrow.Indices[0];
  89. int neckIdx = arrow.Indices[1];
  90.  
  91. int hx = headIdx % gridW;
  92. int hy = headIdx / gridW;
  93. int nx = neckIdx % gridW;
  94. int ny = neckIdx / gridW;
  95.  
  96. int dx = hx - nx;
  97. int dy = hy - ny;
  98.  
  99. int cx = hx + dx;
  100. int cy = hy + dy;
  101.  
  102. while (cx >= 0 && cx < gridW && cy >= 0 && cy < gridH)
  103. {
  104. int checkIdx = cy * gridW + cx;
  105.  
  106. if (occupied.Contains(checkIdx)) return true;
  107.  
  108. cx += dx;
  109. cy += dy;
  110. }
  111.  
  112. return false;
  113. }
  114.  
  115. private ArrowData TryCreateSnakeArrow(int startIdx, int targetLen, HashSet<int> occupied, HashSet<int> zone)
  116. {
  117. List<int> currentPath = new List<int>();
  118. HashSet<int> pathVisited = new HashSet<int>();
  119.  
  120. if (DFS_Snake(startIdx, targetLen, occupied, zone, currentPath, pathVisited))
  121. {
  122. currentPath.Reverse();
  123. return new ArrowData { Indices = currentPath, ColorIndex = rng.Next(0, 6) };
  124. }
  125.  
  126. return null;
  127. }
  128.  
  129. private bool DFS_Snake(int currIdx, int targetLen, HashSet<int> occupied, HashSet<int> zone, List<int> currentPath, HashSet<int> pathVisited)
  130. {
  131. currentPath.Add(currIdx);
  132. pathVisited.Add(currIdx);
  133.  
  134. if (currentPath.Count == targetLen) return true;
  135.  
  136. int[] dIndices = { 0, 1, 2, 3 };
  137. Shuffle(dIndices);
  138.  
  139. int cx = currIdx % gridW;
  140. int cy = currIdx / gridW;
  141.  
  142. foreach (int i in dIndices)
  143. {
  144. int nx = cx + dx[i];
  145. int ny = cy + dy[i];
  146. int nextIdx = ny * gridW + nx;
  147.  
  148. if (nx >= 0 && nx < gridW && ny >= 0 && ny < gridH &&
  149. zone.Contains(nextIdx) &&
  150. !occupied.Contains(nextIdx) &&
  151. !pathVisited.Contains(nextIdx))
  152. {
  153. if (DFS_Snake(nextIdx, targetLen, occupied, zone, currentPath, pathVisited))
  154. return true;
  155. }
  156. }
  157.  
  158. currentPath.RemoveAt(currentPath.Count - 1);
  159. pathVisited.Remove(currIdx);
  160. return false;
  161. }
  162.  
  163. private void Shuffle(int[] array)
  164. {
  165. int n = array.Length;
  166. while (n > 1)
  167. {
  168. int k = rng.Next(n--);
  169. int value = array[k];
  170. array[k] = array[n];
  171. array[n] = value;
  172. }
  173. }
  174.  
  175. private void ShuffleList<T>(IList<T> list)
  176. {
  177. int n = list.Count;
  178. while (n > 1)
  179. {
  180. int k = rng.Next(n--);
  181. T value = list[k];
  182. list[k] = list[n];
  183. list[n] = value;
  184. }
  185. }
  186. }
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment