@@ -56,14 +56,49 @@ public int maxY() {
5656 return Collections .max (cells , Comparator .comparingInt (Cell ::getX )).y ;
5757 }
5858
59+
60+ /**
61+ * Coordinates are like in a 2D array
62+ *
63+ * @param y - row number, 0..max row number
64+ * @param x - column number,0..max column number
65+ * @return a pizza cell with specified coordinated
66+ */
67+ public Optional <Cell > getCell (int y , int x ) {
68+ return cells .stream ().filter (cell -> cell .x == x && cell .y == y ).findFirst ();
69+ }
70+
5971 @ Override
6072 public String toString () {
61- return cells .toString ();
73+ StringBuilder stringBuilder = new StringBuilder ();
74+ stringBuilder .append ("slice : \n " );
75+ //output coordinates
76+ int columnsCount = cells .stream ().max (Comparator .comparingInt (Cell ::getX )).get ().getX ();
77+ int rowsCount = cells .stream ().max (Comparator .comparingInt (Cell ::getY )).get ().getY ();
78+ //output columns coordinates
79+ stringBuilder .append (" " );
80+ for (int column = 0 ; column < columnsCount + 1 ; column ++) {
81+ stringBuilder .append (" " ).append (column );
82+ }
83+ stringBuilder .append ("\n " );
84+ for (int row = 0 ; row < rowsCount + 1 ; row ++) {
85+ //output rows coordinates
86+ stringBuilder .append (row ).append (" " );
87+ for (int column = 0 ; column < columnsCount + 1 ; column ++) {
88+ if (this .getCell (row , column ).isPresent ()) {
89+ stringBuilder .append (this .getCell (row , column ).get ().toString ()).append (" " );
90+ } else {
91+ stringBuilder .append (" " ).append (" " );
92+ }
93+ }
94+ stringBuilder .append ("\n " );
95+ }
96+ return stringBuilder .toString ().trim ();
6297 }
6398
64-
6599 /**
66100 * check if slice valid for current pizza.
101+ *
67102 * @param pizza
68103 * @return
69104 */
@@ -88,53 +123,96 @@ public boolean isValid(Pizza pizza) {
88123
89124 //region generate steps
90125
91-
92- public Slice generateStepDeltaAbove () {
93- List <Cell > delta = new ArrayList <>();
126+ public Step generateStepAbove (Pizza pizza ) {
127+ Slice delta = new Slice ();
94128 for (int x = this .minX (); x <= this .maxX (); x ++) {
95- Cell cell = new Cell (this .minY () - 1 , x , Ingredient .TOMATO );
96- delta .add (cell );
129+ //try to get a cell
130+ Optional <Cell > cell = pizza .getCell (this .minY () - 1 , x );
131+ if (cell .isPresent ()) {
132+ delta .cells .add (cell .get ());
133+ } else {
134+ LOGGER .info ("cant perform step left !" );
135+ return null ;
136+ }
137+ }
138+ LOGGER .debug ("generateStepLeft"
139+ + "\n step left delta: " + delta .toString ());
140+ Step step = new Step (this , delta );
141+ if (step .isValid (pizza )) {
142+ return step ;
143+ } else {
144+ LOGGER .info ("step is invalid !" );
145+ return null ;
97146 }
98- LOGGER .info ("generateStepDeltaAbove"
99- + "\n slice :" + this .toString ()
100- + "\n step above delta: " + delta .toString ());
101- return new Slice (delta );
102147 }
103148
104- public Slice generateStepDeltaBelow ( ) {
105- List < Cell > delta = new ArrayList <> ();
149+ public Step generateStepBelow ( Pizza pizza ) {
150+ Slice delta = new Slice ();
106151 for (int x = this .minX (); x <= this .maxX (); x ++) {
107- Cell cell = new Cell (this .maxY () + 1 , x , Ingredient .TOMATO );
108- delta .add (cell );
152+ //try to get a cell
153+ Optional <Cell > cell = pizza .getCell (this .maxY () + 1 , x );
154+ if (cell .isPresent ()) {
155+ delta .cells .add (cell .get ());
156+ } else {
157+ LOGGER .info ("cant perform step left !" );
158+ return null ;
159+ }
160+ }
161+ LOGGER .debug ("generateStepLeft"
162+ + "\n step left delta: " + delta .toString ());
163+ Step step = new Step (this , delta );
164+ if (step .isValid (pizza )) {
165+ return step ;
166+ } else {
167+ LOGGER .info ("step is invalid !" );
168+ return null ;
109169 }
110- LOGGER .info ("generateStepDeltaBelow"
111- + "\n slice :" + this .toString ()
112- + "\n step below delta: " + delta .toString ());
113- return new Slice (delta );
114170 }
115171
116- public Slice generateStepDeltaLeft ( ) {
117- List < Cell > delta = new ArrayList <> ();
172+ public Step generateStepLeft ( Pizza pizza ) {
173+ Slice delta = new Slice ();
118174 for (int y = this .minY (); y <= this .maxY (); y ++) {
119- Cell cell = new Cell (y , minX () - 1 , Ingredient .TOMATO );
120- delta .add (cell );
175+ //try to get a cell
176+ Optional <Cell > cell = pizza .getCell (y , minX () - 1 );
177+ if (cell .isPresent ()) {
178+ delta .cells .add (cell .get ());
179+ } else {
180+ LOGGER .info ("cant perform step left !" );
181+ return null ;
182+ }
121183 }
122- LOGGER .info ("generateStepDeltaLeft"
123- + "\n slice :" + this .toString ()
184+ LOGGER .debug ("generateStepLeft"
124185 + "\n step left delta: " + delta .toString ());
125- return new Slice (delta );
186+ Step step = new Step (this , delta );
187+ if (step .isValid (pizza )) {
188+ return step ;
189+ } else {
190+ LOGGER .info ("step is invalid !" );
191+ return null ;
192+ }
126193 }
127194
128- public Slice generateStepDeltaRight ( ) {
129- List < Cell > delta = new ArrayList <> ();
195+ public Step generateStepRight ( Pizza pizza ) {
196+ Slice delta = new Slice ();
130197 for (int y = this .minY (); y <= this .maxY (); y ++) {
131- Cell cell = new Cell (y , maxX () + 1 , Ingredient .TOMATO );
132- delta .add (cell );
198+ //try to get a cell
199+ Optional <Cell > cell = pizza .getCell (y , maxX ()+1 );
200+ if (cell .isPresent ()) {
201+ delta .cells .add (cell .get ());
202+ } else {
203+ LOGGER .info ("cant perform step left !" );
204+ return null ;
205+ }
206+ }
207+ LOGGER .debug ("generateStepLeft"
208+ + "\n step left delta: " + delta .toString ());
209+ Step step = new Step (this , delta );
210+ if (step .isValid (pizza )) {
211+ return step ;
212+ } else {
213+ LOGGER .info ("step is invalid !" );
214+ return null ;
133215 }
134- LOGGER .info ("generateStepDeltaRight"
135- + "\n slice :" + this .toString ()
136- + "\n step right delta: " + delta .toString ());
137- return new Slice (delta );
138216 }
139217 //endregion
140218
0 commit comments