Time for action – generating new pieces

  1. 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 ...

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.