@@ -146,100 +146,39 @@ private boolean checkRoom(int xStart, int yStart, int xLen, int yLen) {
146146 private void placeMaze () {
147147 ArrayList <int []> q = new ArrayList <>();
148148
149- // fill in fixed cells on odd / odd coordinates
150149 for (int i = 1 ; i < n ; i += 2 ) {
151150 for (int j = 1 ; j < m ; j += 2 ) {
151+ // fill in fixed cells on odd / odd coordinates
152152 if (map .getGround (i , j ) == WALL ) {
153153 map .setGround (i , j , FLOOR );
154154 cc .makeSet (map .getCell (i , j ));
155-
156- // neighbours
157- if (i + 2 < n )
158- q .add (new int [] { i , j , i + 2 , j });
159- if (j + 2 < m )
160- q .add (new int [] { i , j , i , j + 2 });
161155 }
156+
157+ // queue neighbours when one corner is not a room
158+ if (i + 2 < n && map .getGround (i + 1 , j ) != ROOM )
159+ q .add (new int [] { i , j , i + 2 , j });
160+ if (j + 2 < m && map .getGround (i , j + 1 ) != ROOM )
161+ q .add (new int [] { i , j , i , j + 2 });
162162 }
163163 }
164164
165+ // choose connector in a random order
165166 Collections .shuffle (q );
166167
167- // try every cell if it would connect two components into one
168168 for (int [] e : q ) {
169169 // rename array
170170 final int x1 = e [0 ], y1 = e [1 ], x2 = e [2 ], y2 = e [3 ];
171171
172+ // check if two cells are already connected
172173 if (cc .findSet (map .getCell (x1 , y1 )) == cc .findSet (map .getCell (x2 , y2 )))
173174 continue ;
174175
176+ // merge two components by adding a connector
175177 cc .union (map .getCell (x1 , y1 ), map .getCell (x2 , y2 ));
176178 map .setGround ((x1 + x2 ) / 2 , (y1 + y2 ) / 2 , FLOOR );
177179 }
178180 }
179181
180- /**
181- * connects border cells of a room with the maze
182- *
183- * @param xStart
184- * @param yStart
185- * @param xLen
186- * @param yLen
187- * @return
188- */
189- @ SuppressWarnings ("unused" )
190- private void connectRoom (int xStart , int xLen , int yStart , int yLen ) {
191- // declarations
192- int k = 0 , candidates [][] = new int [(xLen + 2 ) * (yLen + 2 )][2 ];
193-
194- // connector from horizontal borders
195- for (int i = 0 ; i < xLen ; i ++) {
196- if (map .getGround (xStart + i , yStart - 2 ) == FLOOR ) {
197- candidates [k ][0 ] = xStart + i ;
198- candidates [k ][1 ] = yStart - 1 ;
199- k ++;
200- }
201-
202- if (map .getGround (xStart + i , yStart + yLen + 1 ) == FLOOR ) {
203- candidates [k ][0 ] = xStart + i ;
204- candidates [k ][1 ] = yStart + yLen ;
205- k ++;
206- }
207- // debug vis
208- // lvl.setValue(xStart + i, yStart - 1, 3);
209- // lvl.setValue(xStart + i, yStart + yLen, 3);
210- }
211-
212- // connector from vertical borders
213- for (int i = 0 ; i < yLen ; i ++) {
214- if (map .getGround (xStart - 2 , yStart + i ) == FLOOR ) {
215- candidates [k ][0 ] = xStart - 1 ;
216- candidates [k ][1 ] = yStart + i ;
217- k ++;
218- }
219-
220- if (map .getGround (xStart + xLen + 1 , yStart + i ) == FLOOR ) {
221- candidates [k ][0 ] = xStart + xLen ;
222- candidates [k ][1 ] = yStart + i ;
223- k ++;
224- }
225-
226- // debug vis
227- // lvl.setValue(xStart - 1 , yStart + i, 3);
228- // lvl.setValue(xStart + xLen, yStart + i, 3);
229- }
230-
231- // create new connectors
232- // 1 <= num <= k, expected value is around 1.3 per room
233- do {
234- if (k == 0 ) {
235- // System.out.println("fail");
236- break ;
237- }
238- int l = rand .nextInt (k );
239- map .setGround (candidates [l ][0 ], candidates [l ][1 ], FLOOR );
240- } while (rand .nextDouble () < 0.3 );
241- }
242-
243182 /**
244183 * internal function to remove the dead ends of the maze
245184 */
0 commit comments