private SquareGrid assembleGrid ( List < List < SquareNode > > listRows ) { SquareGrid grid = grids . grow ( ) ; grid . reset ( ) ; List < SquareNode > row0 = listRows . get ( 0 ) ; List < SquareNode > row1 = listRows . get ( 1 ) ; int offset = row0 . get ( 0 ) . getNumberOfConnections ( ) == 1 ? 0 : 1 ; grid . columns = row0 . size ( ) + row1 . size ( ) ; grid . rows = listRows . size ( ) ; // initialize grid to null for ( int i = 0 ; i < grid . columns * grid . rows ; i ++ ) { grid . nodes . add ( null ) ; } // fill in the grid for ( int row = 0 ; row < listRows . size ( ) ; row ++ ) { List < SquareNode > list = listRows . get ( row ) ; int startCol = offset - row % 2 == 0 ? 0 : 1 ; // make sure there is the expected number of elements in the row int adjustedLength = grid . columns - startCol ; if ( ( adjustedLength ) - adjustedLength / 2 != list . size ( ) ) { return null ; } int listIndex = 0 ; for ( int col = startCol ; col < grid . columns ; col += 2 ) { grid . set ( row , col , list . get ( listIndex ++ ) ) ; } } return grid ; }
Converts the list of rows into a grid . Since it is a chessboard pattern some of the grid elements will be null .