public RoadPiece[] getRoad(Level level) { Vector2 start = level.getStart(); Vector2 end = level.getFinish(); if (level.getPos(start) != null || level.getPos(end) != null) { return(null); } List <Vector2> previous = new List <Vector2>(); while (true) { previous.Add(start); if (start.x == end.x && start.y == end.y) { return(makeRoadPieces(ref previous)); } Vector2[] neighbors = getNeighbors(level, start, ref previous); if (neighbors.Length != 1) { return(null); } start = neighbors[0]; } }
private Vector2[] getNeighbors(Level level, Vector2 pos, ref List <Vector2> previous) { List <Vector2> array = new List <Vector2>(); Vector2[] checks = { pos + left, pos + up, pos + right, pos + down }; foreach (Vector2 check in checks) { if (isValidPos((int)check.x, (int)check.y, ref level) && level.getPos(check) == null && !search(check, ref previous)) { array.Add(check); } } return(array.ToArray()); }