private static void generateRows ( DancingLinks dancer , Piece piece , int width , int height , boolean flip , boolean [ ] row , boolean upperLeft ) { // for each rotation int [ ] rotations = piece . getRotations ( ) ; for ( int rotIndex = 0 ; rotIndex < rotations . length ; ++ rotIndex ) { // get the shape boolean [ ] [ ] shape = piece . getShape ( flip , rotations [ rotIndex ] ) ; // find all of the valid offsets for ( int x = 0 ; x < width ; ++ x ) { for ( int y = 0 ; y < height ; ++ y ) { if ( y + shape . length <= height && x + shape [ 0 ] . length <= width && ( ! upperLeft || ( isSide ( x , shape [ 0 ] . length , width ) && isSide ( y , shape . length , height ) ) ) ) { // clear the columns related to the points on the board for ( int idx = 0 ; idx < width * height ; ++ idx ) { row [ idx ] = false ; } // mark the shape for ( int subY = 0 ; subY < shape . length ; ++ subY ) { for ( int subX = 0 ; subX < shape [ 0 ] . length ; ++ subX ) { row [ ( y + subY ) * width + x + subX ] = shape [ subY ] [ subX ] ; } } dancer . addRow ( row ) ; } } } } }
For a given piece generate all of the potential placements and add them as rows to the model .