Confused about what newPiece should exactly do


The description for the newPiece method in TetrisGame says, that it should bring a new Piece into the game and the return value should be true in the next piece can be added. But, the description in the Project description says, that the newPiece method adds a new Piece to the board and randomly selects a next piece.

Now this confused me. From my understanding, the newPiece method checks for comnpleted rows, then it adds the current piece(the one that is moving down) to the board and then sets the current piece to the next piece and generates an new random next piece.
But this doesn’t work correctly and I am still confused by the return value of the method. The Test testNewPiece also doesn’t work as a result, because it wants, that the newPiece method adds the next piece directly to the board, because it calls newPiece on a newly created TetrisGame, where the currentpiece is still empty, which doesn’t make sense in my eyes.

What am I overlooking here?

Thanks in advance!

The method newPiece() adds a new piece (nextPiece) to the board and randomly selects a next

You have to distinguish the different meanings of new and next in the sentences.
newPiece sets the already chosen but not yet used (that is not on the board yet) nextPiece to the current piece and chooses a random piece for the nextPiece (the piece that is thrown at the player after the current piece landed).

The testNewPiece test checks whether after the call of newPiece there is indeed a piece on the board to play with.
In other words: If a piece is thrown at the player, there should be a piece to play with.

Keep in mind how pieces are handled and when they are added to (that is written into) the board.

But why should there be a piece placed on the board if it hasn’t landed and the board as well as the current piece were empty before. Shouldn’t just the current piece be not null and have the coordinates (2, board.getNumberOfColumns()/2) ?

Hi there!

The point of the newPiece method is that it’s only called when a new piece needs to be added to the board. Which happens when the current Piece has landed. At this point, the current piece should be
on the board, in its landing position. It then can’t be moved anymore, and there is also a possibility of rows having been completed by said piece. Those rows then need to be removed, if they exist. Since you’re done with the current Piece after that, you make the nextPiece into the currentPiece, place that new currentPiece on the board, and then you make a new random piece for your nextPiece.

So, if I have understood this correctly, newPiece first handles the completed rows, then places the currentPiece on the Board, so it is in the Board 2d array and then generates a new nextPiece.
So the piece currently falling is always placed on the board and removed, when it has to be moved?
Also i’m still not sure, what to do, if currentPiece is empty. Should I not have a Piece variable currentPiece that stores the current piece?

Yes, the pieces are added and removed when moved.

You have a current and a next piece.
The two pieces can be accessed using getCurrentPiece and getNextPiece.

If there is no piece, you have to generate one.

I got it working now. I thought the piece would be drawn seperate from the field.