@@ -14,15 +14,19 @@ private DFSMethods() {
1414 }
1515
1616 /**
17- * For each slice find all available steps. We DON'T change the pizza on this stage
18- *
17+ * For each slice find all available steps.<br>
18+ * If founded start position that haven't any steps and it is unvalid ->
19+ * remove this slice from startPositions and add all it's cells to pizza.
1920 * @param pizza given pizza
2021 * @param startPositions given slices in the pizza
2122 * @return available steps
2223 */
2324 public static Map <Slice , List <Step >> getAvailableSteps (Pizza pizza , List <Slice > startPositions ) {
2425 Map <Slice , List <Step >> groupedSteps = new HashMap <>();
25- for (Slice startPosition : startPositions ) {
26+ Iterator iter = startPositions .iterator ();
27+ while (iter .hasNext ()) {
28+ Slice startPosition = (Slice ) iter .next ();
29+
2630 List <Step > steps = new ArrayList <>();
2731 Step stepLeft = startPosition .generateStepLeft (pizza );
2832 Step stepRight = startPosition .generateStepRight (pizza );
@@ -35,7 +39,20 @@ public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice>
3539 steps .add (stepAbove );
3640 steps = steps .stream ().filter (Objects ::nonNull ).collect (Collectors .toList ());
3741
38- groupedSteps .put (startPosition , steps );
42+ if (steps .size () == 0 ) {
43+ if (startPosition .isValid (pizza )) {
44+ // if slice is valid and have'nt any steps -> cut it from
45+ // startPositions
46+ groupedSteps .put (startPosition , steps );
47+ } else {
48+ // if slice isn't valid and have'nt any steps -> return all
49+ // it cells to pizza
50+ pizza .getCells ().addAll (startPosition .cells );
51+ iter .remove ();
52+ }
53+ } else {
54+ groupedSteps .put (startPosition , steps );
55+ }
3956 }
4057 LOGGER .info ("available steps for" +
4158 "\n pizza: " + pizza
0 commit comments