Time for action – filling in the gaps

  1. Add the FillFromAbove() method to the GameBoard class.
    public void FillFromAbove(int x, int y)
    {
        int rowLookup = y - 1;
    
        while (rowLookup >= 0)
        {
            if (GetSquare(x, rowLookup) != "Empty")
            {
                SetSquare(x, y,
                  GetSquare(x, rowLookup));
                SetSquare(x, rowLookup, "Empty");
                rowLookup = -1;
            }
            rowLookup--;
        }
    }

What just happened?

Given a square to fill, FillFromAbove() looks at the piece directly above to see if it is marked as Empty. If it is, the method will subtract one from rowLookup and start over until it reaches the top of the board. If no non-empty pieces are found when the top of the board is reached, the method does nothing and exits.

When a non-empty piece is found, it is copied to the destination square, and the ...

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.