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 into that location.

The reason the processing order is important here is that, by filling a lower square from a higher position, that higher position will become empty. It, in turn, will need to be filled from above.

After the holes are filled (or if dropSquares is set to false) GenerateNewPieces() examines each square in boardSquares and asks it to generate random pieces for each square that contains an empty piece.

Water filled pipes

Whether or not a pipe is filled with water is managed separately from its orientation. Rotating a single pipe could change the water-filled status of any number of other pipes without changing their rotation.

Instead of filling and emptying individual pipes, however, it is easier to empty all of the pipes and then refill the pipes that need to be marked as having water in them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.115.120