0

I am coding Tetris in Qt C++ at the moment. The game is almost completed and the only thing I need to add is the rotation. Now what I am struggling with, is the theory behind the rotation. Tetris has 7 different kind of stones ( I, S, Z, L, J, T, O ). Is there any algorithm or anything similar with the rotations from the different shapes ?

What I prefer not to do is having a switch case for 7 different shapes to handle the rotations. Also if a shape like L is rotated it has 4 different positions, which have to be handled different.

So the only thing I have thought of yet is to ask for the shape and then for the position. This would grant me some switches or else if's in a switch... Means a lot to type and a lot to check for the compiler.

P.S. My Stone structure looks like this:( Steine = german for stone, Art = shape )

struct position
{
    int X;
    int Y;
};   

struct Steine
{
    struct position* Position;
    int Art;
};
Trygve Laugstøl
  • 7,440
  • 2
  • 36
  • 40
FrontMobe
  • 186
  • 1
  • 13
  • 1
    The interesting part would be how the stones are used in the grid model, e.g. how you check for full rows. – jhnnslschnr Mar 22 '15 at 12:52
  • There's already several posts on the topic of Tetris rotation, even in C++ or with Qt [here's one](http://stackoverflow.com/questions/7932723/), [here's another](http://stackoverflow.com/q/233850/211160), one on [gamedev.SE](http://gamedev.stackexchange.com/q/17974). Can you clearly explain where you are stuck that these aren't answering? It would take more code shared...although read [MCVE](http://stackoverflow.com/help/mcve) first! *(Sidenote: in C++ you can just say `position* Position` instead of `struct position* Position`, but where is the position pointed to allocated here?)* – HostileFork says dont trust SE Mar 22 '15 at 13:00
  • `Steine` containing a pointer to a `position` does not look right. It should just contain a `position` directly. And please, try to stick with English in programming code, or at the very least translate everything before you post in an international medium. By the way, "Art" in this context is best translated as "kind", not as "shape". – Christian Hackl Mar 22 '15 at 13:06
  • And the usual naming convention for types is to use the singular form. It should be `Stein`, just like it's `std::string` and not `std::strings`. After all, with the other struct, you did it correctly and did not name it "Positionen". – Christian Hackl Mar 22 '15 at 13:09
  • First of all thanks for the topic advices. Like I already said I almost completed the code, meaning full rows and stuff is done. The only thing I did not code is the rotation ( You can play my game right now, but you can't rotate the stones, meaning that you wont succeed :D ). Well my question is just the theory behind the rotation and not how to code it. In one topic you posted it is said get a point in the middle and rotate around it. However this is what I thaught first but I couldn't figure out how to determe the point with the different shapes. Can You help me there? – FrontMobe Mar 22 '15 at 18:24
  • About the translation and the struct. I am more used to C and I didn't translate it literally, but more what I meant with it. – FrontMobe Mar 22 '15 at 18:26

1 Answers1

1

You could use a 2D array of bool representation for each shape. Then when you rotate some specific array, you rotate that shape (maybe have the code at the initialization generate all the rotations) and check if any pixel is outside the Tetris borders or if the rotated shape should not be rotated because some of it's pixel would be on the same position as some already existing pixel from previous shapes.

Edit: Yeah, like you said yourself, best is to try on paper/paint to check it out (about the middle point for the rotation). For every shape you then end up with a 3x3 or 4x4. for 3x3 you rotate around it's middle point, for 4x4 around 1x1 for example (where index goes from 0 to 3). That is somewhat how I went for my Tetris 9 years ago or so.

iNyuu
  • 62
  • 6
  • `2D array of bool representation` +1. If you currently have a special treatment for every type of stone, change it to this array approach. – deviantfan Mar 22 '15 at 12:54
  • So I didn't really want to talk about the coding, but as you guys always answer in methods how to code it I will share my idea behind my code so far. I got 2 2D fields at the moment. One is filled with 1 and 0 ( could have been bool as well ) 0 for empty 1 for full. The other field does tell me which stone is at the certain places ( I got different colors for different shapes ). I could sum this up to only 1 2D array, but at this point it is like that. – FrontMobe Mar 22 '15 at 19:45
  • So once again, I know how to use this 2D arrays( borders etc. ). My question remains. How can I rotate a shape? Where is the middlepoint to turn around? I really talk about the theory. How can I determine this point? If I got a square it is qute simple to say it is in the middle, but in some cases the middle is in a stone and not around. Is there a technic to determine it? – FrontMobe Mar 22 '15 at 19:47
  • 2
    Thanks, did it on my own now. Determined a point to rotate around for each shape just by trying on a sheet of paper – FrontMobe Mar 23 '15 at 10:42