I have a nephew who is wicked smart. Actually, I have 4 nephews and 4 nieces in total, but this is about one nephew in particular.
Anyway... he's fairly young, but he's always been interested in software development. By software development, I mean games of course. He doesn't just play them though, he's taught himself Java. Then I showed him C#. (Yes, I suppose I could have showed him VB... but he already knew Java, so C# was the logical choice.) Along with C#, was XNA. He really grok'ed XNA.
One of his first XNA projects was the standard breakout/Arkanoid type of game, with power-ups, etc... and particle effects. For a first (or even 3rd or 4th) game, it was pretty darn cool.
Before long, he was taking XNA in new directions. Asking questions I couldn't answer. Eventually, I hear from him and he's built something new. Not a game so much as a tool. Apparently he's big into Second Life. He needed a way to work with his Second Life models outside of the game, so he wrote a viewer in XNA and WinForms. Pretty sweet.
This weekend, I got an email from him. He's working on a Tetris clone and had everything done except the shape rotation. He's storing all the shapes in a 4x4 array and couldn't figure out how to rotate the array left and right.
It's funny how sometimes the hard stuff comes easy to us, while the simple stuff proves vexing. I run into this a lot at work. I think a large part of it comes from not having a CS background. Being largely self taught can be a disadvantage sometimes. Many folks will tell you that most of what you learn in a CS degree program is obsolete, and while that may apply to specific technology, there's a lot of theory and otherwise "common" knowledge that us self-taught folks don't always have when entering the work force. (Stay in School Kids!!)
So, I gave his question some thought and came up with (I hope) a pretty good answer. Assuming this is what he's trying to achieve, how would we go about making it happen?
Part of the problem to solve depends on which direction you are trying to rotate.
In the illustration, you can see the shape array is rotating one step to the right. This means that the first row of the Y axis becomes the farthest column of the X axis. Therefore, the next row is the second farthest column, and so on.
It should be a simple matter of looping through the source array and writing into the destination array.
So, if you're rotating to the right, you have a nested loop (X inside of Y) reading the source array row by row and you want to write to the destination array column by column, where the target column is the inverse of the source row.
On the other hand, if you are rotating to the left, it's actually even easier. The top row becomes the first column, the second row becomes the second column and so on.
The code should look something like this:
' For LEFT turns
For Y = 0 to 3
For X = 0 to 3
Destination(Y,3-X) = Source(X,Y)
' For RIGHT turns
For Y = 0 to 3
For X = 0 to 3
Destination(3-Y,X) = Source(X,Y)
This code makes some assumptions, like knowing you have 4x4 arrays and knowing which way you're turning, and of course actually drawing the resulting shape on the screen, in the proper color. I'll leave all that as an exercise to you, the reader.
The real question is: now that I've figured out how, when the hell would I ever use this other than in a Tetris game?