How can I generate a list of all Tetrominos? Or, more generally, how can I generate a subset of the polyominoes restricted to a number of cells?
There are many ways to do this. One option that I've found works well is to think about it recursively and more generally. In particular:
- A single rectangle is a 1-omino.
- For any n-omino, you can create an (n+1)-omino by putting a block adjacent to any of the blocks from an n-omino.
This gives you a recursive way of listing all possible n-ominos. You need to be careful, though, since this will generate multiple rotations and translations of the same n-ominos. You can fix this by picking some reference coordinate system and then translating the n-omino so that it is always flush with the axes of that system. Once you've got that working, you can generate all rotations by just rotating the resulting n-omino around the axes, then translating it back to the canonical position.
The biggest grid you need for an N-omino is NxN. Generate all then exclude rotations and translations.
You can think of it as a path-following algorithm in N-1 steps. If you start from the top-left cell of the grid and only move right or down from there, you will avoid most translations and rotations. If I'm not mistaken, the only ones that will be left are the isomorphic N-ominones. E.G. the tetromino made by the path Right-Down-Left is the same as Down-Right-Up.
Tetrominos are shapes. Computers do not have direct representation of shapes, they have representations of numbers as binary signals. You, the programmer, determine how you want to represent a shape as a number. They can be stored as bitmaps, strings, an enum...
You'll have to write a much clearer question if you want specific help.
I'm also curious why you want to generate a list of 7 known, unchanging shapes. If you were writing a Tetris game, you would hardcode those 7 shapes somewhere, as variables, constants or images, not generate them.
来源:https://stackoverflow.com/questions/4778493/how-can-i-generate-a-list-of-all-tetrominos