@@ -59,49 +59,61 @@ public static Optional<Slice> rightStep(Pizza pizza, Slice slice) {
5959 }
6060
6161 /**
62- * Step as an action it's a process, when a slice adding to itself a subset of a pizza cells and remains rectangular
63- * and valid.
62+ * For each slice find all available steps. We DON'T change the pizza on this stage
6463 *
6564 * @param pizza given pizza
66- * @param output given slice in the pizza
65+ * @param output given slices in the pizza
6766 * @return available steps
6867 */
69- List <Step > getAvailableSteps (Pizza pizza , List <Slice > output ) {
70- //TODO implement.For each slice find all available steps. We DON'T change the pizza on this stage
71- List <Step > allSteps = new ArrayList <Step >();
68+ public static List <Step > getAvailableSteps (Pizza pizza , List <Slice > output ) {
69+ List <Step > steps = new ArrayList <>();
7270 for (Slice slice : output ) {
73- Slice left = slice .generateLeft (slice );
74- Slice right = slice .generateRight (slice );
75- Slice abowe = slice .generateAbowe (slice );
76- Slice belowe = slice .generateBelow (slice );
77- if (pizza .cotnainsAllCells (left ))allSteps .add (new Step (slice , left ));
78- if (pizza .cotnainsAllCells (right ))allSteps .add (new Step (slice , right ));
79- if (pizza .cotnainsAllCells (abowe ))allSteps .add (new Step (slice , abowe ));
80- if (pizza .cotnainsAllCells (belowe ))allSteps .add (new Step (slice , belowe ));
81- }
82- return allSteps ;
71+ Slice left = slice .generateLeft (slice );
72+ Slice right = slice .generateRight (slice );
73+ Slice above = slice .generateAbowe (slice );
74+ Slice below = slice .generateBelow (slice );
75+ if (pizza .cotnainsAllCells (left )) steps .add (new Step (slice , left ));
76+ if (pizza .cotnainsAllCells (right )) steps .add (new Step (slice , right ));
77+ if (pizza .cotnainsAllCells (above )) steps .add (new Step (slice , above ));
78+ if (pizza .cotnainsAllCells (below )) steps .add (new Step (slice , below ));
79+ }
80+ LOGGER .info ("available steps for" +
81+ "\n pizza: " + pizza
82+ + "\n slices: " + output
83+ + "\n steps: " + steps );
84+ return steps ;
8385 }
8486
85- Slice performStep (Pizza pizza , List <Step > steps ) {
87+ public static Slice performStep (Pizza pizza , List <Step > steps ) {
8688 //TODO pick-ups a step with a minimal steps number, execute it(cut it from the pizza, and a slice)
8789 return null ;
8890 }
8991
90- List <Slice > cutAllStartPositions (Pizza pizza ) {
91- //TODO pick-ups a step with a minimal steps number, execute it(cut it from the pizza, and a slice). The pizza is a mutable object
92- List <Cell > currentPizza = pizza .getCells ();
93- List <Slice > starts = new ArrayList <Slice >();
94- Iterator <Cell > iter = currentPizza .iterator ();
95- while (iter .hasNext ()){
96- Cell cell = iter .next ();
97- if (cell .ingredient == Ingredient .MUSHROOM ){
98- Slice slice = new Slice ();
99- slice .cells .add (cell );
100- starts .add (slice );
101- iter .remove ();
102- }
103- }
104- return starts ;
92+ /**
93+ * Finds a cells type with minimal cells numbers and generates one cell slices from them
94+ *
95+ * @param pizza given pizza
96+ * @return slices that are start positions for future slicing
97+ */
98+ public static List <Slice > cutAllStartPositions (Pizza pizza ) {
99+ List <Cell > mushrooms = pizza .getCells ().stream ()
100+ .filter (cell -> cell .ingredient .equals (Ingredient .MUSHROOM ))
101+ .collect (Collectors .toList ());
102+ List <Cell > tomatoes = pizza .getCells ().stream ()
103+ .filter (cell -> cell .ingredient .equals (Ingredient .TOMATO ))
104+ .collect (Collectors .toList ());
105+ LOGGER .info ("cutAllStartPositions for pizza: " + pizza
106+ + "\n mushrooms number: " + mushrooms .size ()
107+ + "\n tomatoes number: " + tomatoes .size ());
108+ if (mushrooms .size () > tomatoes .size ()) {
109+ return tomatoes .stream ()
110+ .map (Slice ::new )
111+ .collect (Collectors .toList ());
112+ } else {
113+ return mushrooms .stream ()
114+ .map (Slice ::new )
115+ .collect (Collectors .toList ());
116+ }
105117 }
106118
107119}
0 commit comments