private boolean mergeSpaces ( ArrayList spaces ) { for ( int source = 0 ; source < spaces . size ( ) ; source ++ ) { Space a = ( Space ) spaces . get ( source ) ; for ( int target = source + 1 ; target < spaces . size ( ) ; target ++ ) { Space b = ( Space ) spaces . get ( target ) ; if ( a . canMerge ( b ) ) { spaces . remove ( a ) ; spaces . remove ( b ) ; spaces . add ( a . merge ( b ) ) ; return true ; } } } return false ; }
Merge the spaces that have been created to optimize out anywhere we can .