public MazeMap Generate(int sizeX, int sizeY) { MazeMap map = new MazeMap(sizeX, sizeY); List <MapPos> pending = new List <MapPos>(); pending.Add(new MapPos(map.Width() / 2, map.Height() / 2)); EnumDirection?lastDir = null; while (pending.Count > 0) { MapPos curPos = pending[0]; int curSeg = map.GetSegment(curPos); List <EnumDirection> dirs = GetEmptySides(map, curPos, lastDir); if (lastDir.HasValue) { curSeg |= lastDir.Value.Opposite().BitMask(); map.SetSegment(curPos, curSeg); } if (dirs.Count <= 0) { lastDir = null; pending.RemoveAt(0); pending.Shuffle(rand); } else { EnumDirection nxtDir = dirs[rand.Next(dirs.Count)]; curSeg |= nxtDir.BitMask(); map.SetSegment(curPos, curSeg); lastDir = nxtDir; if (dirs.Count == 1) { pending.RemoveAt(0); } pending.Insert(0, curPos.Offset(nxtDir)); } } return(map); }