

All we know is that we've got a shape and we can interact with it. More information about this class in the next section.Īs far as we are concerned, we don't care what the actual shape is. The game asks the shape factory for a shape. So how do things actually work? Let's quickly go over the game “Flow”. This gives us a simple mechanism for checking all imaginable shapes moves as long as we have a way to get the current shapes position (its coordinates on the board) and its new desired position. Otherwise we can't move the shape to its new location and return false indicating no changes have been made to the original game’s board.

If we succeed doing so, the move is legit and we overwrite the game’s board with the copy we've made. What we do within the LegitMove method is create a copy of the game’s board and “Cut out” the current shape from it so it won't take any space, then we try to paste the shape to its new position. The method gets the current shape position coordinates and the desired coordinates (where the shape wishes to move).
FALLING BLOCKS PATTERN CODE
Let’s have a look at the board’s class Move method:Ĭopy Code public bool Move(Point currentPos, Point desiredPos) Fortunately there's a quicker way to do this. Sure we can implement a complex check for each shape, but this would take too long. One question that came up is how do we know if a certain shape’s move (right, left, down, rotate) is possible? There’s much to this class so I'll point out only few things that I find interesting. It seems logical to put all this responsibility in one place. Updating the boolean values of the game board matrix.Checking if it’s possible to reposition a given shape.

The board class manages the game’s board by:
FALLING BLOCKS PATTERN FREE
Think of the game as a two dimensional Boolean grid that has width and height, a filled space will be marked as true, free space will be set to false. I've been babbling about coordinates for a while now. I wasn't so sure about what’s the right way to turn each shape, so I've come up with my own way. Rotating a shape is just a matter of figuring out how the shape should be laid out after rotation and where our top left coordinate should go, then all that's left is reconstructing the shape based on this new top left coordinate. Once we understand this key concept of shapes representation and movement, creating new shapes is easy. We still have to check if the move we just made is legit but will address this problem later on. Let's have a quick look at the shape’s code:Ĭords = new Point(left, top) // is our new top left positionīased on this point, we construct the rest of the square. But just before that, we'll have to create an abstract class named Shape which all the actual shapes will inherit from. So I thought a good place to start would be the simplest shape there is - the cube - a four by four square. I wanted to figure out things the hard way. As an objective, I decided not to look at any open source falling blocks code or any tutorial on the subject. When I started writing the game, I didn't actually know where to start. I assume you have all played the most famous falling blocks game 'Tetris' at some point in your life, so there’s no reason to explain the rules of the game. Provide an interface for creating families of related or dependent objects without specifying their concrete classes." I'm not going to get into details about the pattern, but I would recommend reading about it. I've been looking into some design patterns on DoFactory and as a good practice, I decided to write a Tetris game that uses the Factory pattern.
