Implemented the piece generation algorithm.