private bool PickNextPathPart(List <Vector2Int> path) { if (path.Count == data.GetLevelLength()) { return(true); } Vector2Int CurrentPosition = path[path.Count - 1]; ShuffleDirections(); Vector2Int Pos1 = CurrentPosition + Directions[0]; Vector2Int Pos2 = CurrentPosition + Directions[1]; Vector2Int Pos3 = CurrentPosition + Directions[2]; Vector2Int Pos4 = CurrentPosition + Directions[3]; //Try continuing path with pos 1 if (Pos1.x >= 0 && Pos1.x <= data.GetMapSize().x - 1 && Pos1.y >= 0 && Pos1.y <= data.GetMapSize().y - 1 && !path.Contains(Pos1)) { path.Add(Pos1); if (PickNextPathPart(path)) { //Return true if path is viable return(true); } else { path.Remove(Pos1); } } //Try continuing path with pos 2 if (Pos2.x >= 0 && Pos2.x <= data.GetMapSize().x - 1 && Pos2.y >= 0 && Pos2.y <= data.GetMapSize().y - 1 && !path.Contains(Pos2)) { path.Add(Pos2); if (PickNextPathPart(path)) { return(true); } else { path.Remove(Pos2); } } //Try continuing path with pos 3 if (Pos3.x >= 0 && Pos3.x <= data.GetMapSize().x - 1 && Pos3.y >= 0 && Pos3.y <= data.GetMapSize().y - 1 && !path.Contains(Pos3)) { path.Add(Pos3); if (PickNextPathPart(path)) { return(true); } else { path.Remove(Pos3); } } //Try continuing path with pos 4 if (Pos4.x >= 0 && Pos4.x <= data.GetMapSize().x - 1 && Pos4.y >= 0 && Pos3.y <= data.GetMapSize().y - 1 && !path.Contains(Pos4)) { path.Add(Pos4); if (PickNextPathPart(path)) { return(true); } else { path.Remove(Pos4); } } //Return false if no viable path return(false); }