Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class LevelGenerator
- {
- public int minLen = 2;
- public int maxLen = 5;
- private int gridW, gridH;
- private System.Random rng = new System.Random();
- private readonly int[] dx = { 1, -1, 0, 0 };
- private readonly int[] dy = { 0, 0, 1, -1 };
- public LevelGenerator(int w, int h)
- {
- gridW = w;
- gridH = h;
- }
- public List<ArrowData> Generate(List<int> zoneIndices)
- {
- if (zoneIndices == null || zoneIndices.Count < minLen) return null;
- LevelData currentLevel = new LevelData { GridXSize = gridW, GridYSize = gridH };
- HashSet<int> occupied = new HashSet<int>();
- HashSet<int> zone = new HashSet<int>(zoneIndices);
- bool addedInPass = true;
- while (addedInPass)
- {
- addedInPass = false;
- List<int> freeCells = zone.Where(idx => !occupied.Contains(idx)).ToList();
- if (freeCells.Count < minLen) break;
- ShuffleList(freeCells);
- foreach (int startCell in freeCells)
- {
- for (int targetLen = maxLen; targetLen >= minLen; targetLen--)
- {
- ArrowData bestArrow = null;
- int attemptsToFindHardArrow = 4;
- for (int attempt = 0; attempt < attemptsToFindHardArrow; attempt++)
- {
- ArrowData potentialArrow = TryCreateSnakeArrow(startCell, targetLen, occupied, zone);
- if (potentialArrow != null)
- {
- currentLevel.Arrows.Add(potentialArrow);
- if (currentLevel.CanSolveLevel())
- {
- bestArrow = potentialArrow;
- if (IsExitBlocked(potentialArrow, occupied))
- {
- currentLevel.Arrows.RemoveAt(currentLevel.Arrows.Count - 1);
- break;
- }
- }
- currentLevel.Arrows.RemoveAt(currentLevel.Arrows.Count - 1);
- }
- }
- if (bestArrow != null)
- {
- currentLevel.Arrows.Add(bestArrow);
- foreach (int idx in bestArrow.Indices) occupied.Add(idx);
- addedInPass = true;
- break;
- }
- }
- if (addedInPass) break;
- }
- }
- return currentLevel.Arrows;
- }
- private bool IsExitBlocked(ArrowData arrow, HashSet<int> occupied)
- {
- int headIdx = arrow.Indices[0];
- int neckIdx = arrow.Indices[1];
- int hx = headIdx % gridW;
- int hy = headIdx / gridW;
- int nx = neckIdx % gridW;
- int ny = neckIdx / gridW;
- int dx = hx - nx;
- int dy = hy - ny;
- int cx = hx + dx;
- int cy = hy + dy;
- while (cx >= 0 && cx < gridW && cy >= 0 && cy < gridH)
- {
- int checkIdx = cy * gridW + cx;
- if (occupied.Contains(checkIdx)) return true;
- cx += dx;
- cy += dy;
- }
- return false;
- }
- private ArrowData TryCreateSnakeArrow(int startIdx, int targetLen, HashSet<int> occupied, HashSet<int> zone)
- {
- List<int> currentPath = new List<int>();
- HashSet<int> pathVisited = new HashSet<int>();
- if (DFS_Snake(startIdx, targetLen, occupied, zone, currentPath, pathVisited))
- {
- currentPath.Reverse();
- return new ArrowData { Indices = currentPath, ColorIndex = rng.Next(0, 6) };
- }
- return null;
- }
- private bool DFS_Snake(int currIdx, int targetLen, HashSet<int> occupied, HashSet<int> zone, List<int> currentPath, HashSet<int> pathVisited)
- {
- currentPath.Add(currIdx);
- pathVisited.Add(currIdx);
- if (currentPath.Count == targetLen) return true;
- int[] dIndices = { 0, 1, 2, 3 };
- Shuffle(dIndices);
- int cx = currIdx % gridW;
- int cy = currIdx / gridW;
- foreach (int i in dIndices)
- {
- int nx = cx + dx[i];
- int ny = cy + dy[i];
- int nextIdx = ny * gridW + nx;
- if (nx >= 0 && nx < gridW && ny >= 0 && ny < gridH &&
- zone.Contains(nextIdx) &&
- !occupied.Contains(nextIdx) &&
- !pathVisited.Contains(nextIdx))
- {
- if (DFS_Snake(nextIdx, targetLen, occupied, zone, currentPath, pathVisited))
- return true;
- }
- }
- currentPath.RemoveAt(currentPath.Count - 1);
- pathVisited.Remove(currIdx);
- return false;
- }
- private void Shuffle(int[] array)
- {
- int n = array.Length;
- while (n > 1)
- {
- int k = rng.Next(n--);
- int value = array[k];
- array[k] = array[n];
- array[n] = value;
- }
- }
- private void ShuffleList<T>(IList<T> list)
- {
- int n = list.Count;
- while (n > 1)
- {
- int k = rng.Next(n--);
- T value = list[k];
- list[k] = list[n];
- list[n] = value;
- }
- }
- }
Advertisement