Finally, we need to be able to check the pile for full rows and remove them. The method removeRow is not that difficult. The argument is the row to remove. So you should once more iterate over all elements in the container. When the y position of an element is equal to the argument ‘row ’, the element should be removed. Otherwise, you have to verify that the y is greater than the argument row. If it is, you move the square down with the method move.
The method checkLines should check for full rows in the pile. If so, those rows are to be removed. The method must return the number of rows deleted, because that is important for the score.
You can copy the code below. Just make sure you understand all the steps.
int checkLines()
{
// Create an array for all rows. Here we provide three additional
// rows. At the time that the game is done, there will be
5 // blocks in the pile that are positioned higher than the actual
playing field
int squaresInRow[ROWS + 3];
// Set all values to zero.
10 REPA(squaresInRow) squaresInRow[i] = 0;
// Iterate the list and raise a row depending on the \verb|y| position
// of the square.
REPA(list)
15{
int row = list[i].getPos().y;
squaresInRow[row]++;
}
20 // Start with 0 full lines.
int completedLines = 0;
// Iterate over all rows.
REPA(squaresInRow)
25{
// If the number of squares in this row is equal to the
// constant SQUARES_PER_ROW, the row is complete.
if(squaresInRow[i] == SQUARES_PER_ROW)
{
30 // We delete this row, but have to account
// for the fact that multiple rows can be deleted
// during this loop. If so, the remaining
// rows are already moved one position down.
removeRow(i - completedLines);
35
// Finally, we adjust the counter of deleted rows.
completedLines++;
}
}
40
// return the number of deleted rows.
return completedLines;
}