public void GeneratePathToEnd(Map map, int maxIterations, Room startRoom) { Room room = startRoom; int counter = 2; int iteration = 0; while (iteration < maxIterations) { IEnumerable<Room> neighbours = map.GetNeighbours(room); if (neighbours.Any(n => n.X == map.EndX && n.Y == map.EndY)) { room.Id = 99; map.ReachedEnd = true; break; } foreach (Room neighbour in neighbours.OrderBy(x => _rand.Next())) { if (neighbour.Id == 0 && map.CountNeighbours(neighbour) == 1) { neighbour.Id = counter; room = neighbour; counter++; break; } } iteration++; } map.RoomsInPath = counter - 2; }
private void RemoveUnconnectedRooms(Map map) { for (int y = 0; y < map.Height; y++) { for (int x = 0; x < map.Width; x++) { if (map.CountNeighbours(map[x, y]) == 0) map[x, y] = new Room { X = x, Y = y }; else if (RoomHasOnlySideRoomConnections(map, map[x, y])) map[x, y] = new Room { X = x, Y = y }; } } }