Time for action – generating new pieces
Add the
GenerateNewPieces()
method to the GameBoard class:public void GenerateNewPieces(bool dropSquares) { if (dropSquares) { for (int x = 0; x < GameBoard.GameBoardWidth; x++) { for (int y = GameBoard.GameBoardHeight - 1; y >= 0; y--) { if (GetSquare(x, y) == "Empty") { FillFromAbove(x, y); } } } } for (int y = 0; y < GameBoard.GameBoardHeight; y++) for (int x = 0; x < GameBoard.GameBoardWidth; x++) { if (GetSquare(x, y) == "Empty") { RandomPiece(x, y); } } }
What just happened?
When GenerateNewPieces()
is called with "true" passed as dropSquares
, the looping logic processes one column at a time from the bottom up. When it finds an empty square it calls FillFromAbove()
to pull a filled square from above into that location...