
I would appreciate any help in making it look better. If you are interested, I can post the code.īelow is my attempt at pasting my code. And the algorithm is done in Visual Basic for Excel, so other implementations could be much faster.īelow are some examples of its solutions: The above algorithm is very simple but it does manage to solve N=7 cases within 5 seconds.

if the first piece has no other possible positions left to move to), no solution is possible. If all K pieces have been placed, you are done. If piece k cannot be placed, return and see if the previous piece can be moved to its next position. If piece k can be placed, place it and attempt to place the next piece. Make a recursive function which attempts to place piece k at the first possible position where it doesn't overlap an existing piece.Īt each moment, the function must know which positions in the square have already been taken.Given a square size N and given K Tetris pieces, find all possible positions of each Tetris piece in the squareįor each piece, find the coordinates at which its topmost and leftmost part could be located.An "L" piece, lying down with its "head" to the right, would then have coordinates (2,0), (2,1), (1,1), (0,1). I defined each piece as beginning with the coordinates of the topmost and leftmost part of the piece and then giving the relative coordinates of the 3 remaining "parts" of that piece in an array.
#All tetris blocks how to#
If anyone has an idea how to solve this, I'd really appreciate the help.Įdit: I am looking for an algorithm that actually places the pieces in the square.Ī simple algorithm, which I have just now implemented, is the following: This would actually work with some examples, but there are also a lot of possible combinations, where the smallest square possible wouldn't even contain one rectangle inside. I the program would find all the pieces needed for this rectangle, it would know how to continue with those pieces. For doing that I only came up with the idea to define certain pieces, which combine to a rectangle together. The obvious problem now is the algorithm, to place the pieces in the best way possible. My idea now was to try to fit all pieces into this square, and enlarge it by 1 if it doesn't fit. All the pieces are typical Tetris pieces made out of 4 combined blocks. The equation would be: $m = \sqrt$ rounded up, with m being the minimal size and n being the number of pieces you have.

I thought of starting to create the minimal square, in case all the pieces fit in perfectly. In total, there are 19 different possible pieces. I am supposed to write a program to do this, but I think this is more of a mathematical problem. The pieces I get are static, so I am not allowed to turn them and the square is allowed to have holes inside. I need to arrange an uncertain number of Tetris pieces, into the smallest possible square.
