Tetris for Intro/Intermediate Programmers (Fall 2018 update)
Step 7: Removing Full Rows and Keeping Score
Our game is close to being complete -- or at least complete enough that it can be fun to play. All that remains is removing full rows (since filling rows is the goal of Tetris) and keeping score.
Writing the removeFullRows() function:
This function will clear any full rows (rows that do not contain the empty color) from the board, move the rows above them down, and fill the top with empty rows. To do this, we'll create a new board and will only copy into it rows from the old board that are not full (in the correct order, of course). We'll also keep track of the number of full rows that are removed in a variable, fullRows. Once all the non-full rows are copied over, we'll add empty-color rows to the top of the new board until it's the right size.
Hint: Watch the gif below carefully, and note that it is not sufficient to just check the bottom row for fullness. Any row could potentially be completed and need to be removed, so make sure you test for that when you play the game! Also, sometimes multiple rows will need to be removed when a piece is placed, so make sure to remove all full rows at the same time.
We can then call removeFullRows in placeFallingPiece, so that full rows are cleared and the board is updated at the first possible opportunity. Now we're playing Tetris!
Now that we are removing rows, we should also keep score. To do that, we will introduce a data value "score" (which is set to zero in the init function). In our removeFullRows function, we will increment the score by the square of total number of full rows removed in order to reward removing multiple lines at once.
Now that we are keeping score, we should also display the score. To do this, add a function drawScore which is called by our draw function and displays the current score at the top of the board.
At the end of this stage, your Tetris game should be complete! Nice work!
David Kosbie
Carnegie Mellon University koz@cmu.edu