Tetris Help v2

I am trying to start a project that makes the game, Tetris. For this part, I already have seven unique 2-dimensional arrays that will represent Tetriminos (Tetris pieces), and I need help to create two functions to rotate those 2-dimensional arrays: rotateLeft() and rotateRight(). It will rotate 90 degrees in the specified direction. E.g. RotateLeft() would move row 1 values to col 1; row 2 to row 2; etc. And to have a function called printTetrimino to print the contents of a 2-d tetrimino array to the console window. I am just having problems on how to finish the rotateRight function and to start on the rotateLeft function. Please let me know how to do this. Thank you! Code is below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  #include <iostream>

using namespace std;

// function implementations
void rotateRight();

int main() {

};

int tetromino[7][4][4][4] =
{
    // Square piece
    {
    {
    { 1,1,0,0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1, 1, 0, 0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,0,0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,0,0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    },
    // I piece
    {
    {
    { 1,1,1,1 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1, 0, 0, 0 },
    { 1,0,0,0 },
    { 1,0,0,0 },
    { 1,0,0,0 },
    },
    {
    { 1,1,1,1 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,0,0,0 },
    { 1,0,0,0 },
    { 1,0,0,0 },
    { 1,0,0,0 },
    },
    },
    // L piece
    {
    {
    { 1,0,0,0 },
    { 1,0,0,0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,1,0 },
    { 1,0,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,0,0 },
    { 0,1,0,0 },
    { 0,1,0,0 },
    { 0,0,0,0 },
    },
    {
    { 0,0,1,0 },
    { 1,1,1,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    },
    // J piece
    {
    {
    { 0,1,0,0 },
    { 0,1,0,0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,0,0,0 },
    { 1,1,1,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,0,0 },
    { 1,0,0,0 },
    { 1,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,1,0 },
    { 0,0,1,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    },
    // S piece
    {
    {
    { 0,1,1,0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,0,0,0 },
    { 1,1,0,0 },
    { 0,1,0,0 },
    { 0,0,0,0 },
    },
    {
    { 0,1,1,0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,0,0,0 },
    { 1,1,0,0 },
    { 0,1,0,0 },
    { 0,0,0,0 },
    },
    },
    // N piece
    {
    {
    { 0,1,0,0 },
    { 1,1,0,0 },
    { 1,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,0,0 },
    { 0,1,1,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 0,1,0,0 },
    { 1,1,0,0 },
    { 1,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,0,0 },
    { 0,1,1,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    },
    // T piece
    {
    {
    { 1,0,0,0 },
    { 1,1,0,0 },
    { 1,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,1,0 },
    { 0,1,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 0,1,0,0 },
    { 1,1,0,0 },
    { 0,1,0,0 },
    { 0,0,0,0 },
    },
    {
    { 0,1,0,0 },
    { 1,1,1,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    },
};

void rotateRight() {
    for (int i = 0; i < 16; i++) {
        int temp = tetromino[i][0];
        tetromino[i][0] = tetromino[i][1];
        tetromino[i][1] = -temp;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <vector>
using namespace std;

const int SIZE = 4;
using matrix = vector< vector<int> >;

class Tetromino;
class Screen;
Tetromino rotate( const Tetromino &T, int quarterTurns = 0, bool shift = false );

//======================================================================

class Tetromino
{
public:
   matrix A;

   Tetromino( char C = ' ' );
   void rotateL() { *this = rotate( *this, 1, true ); }
   void rotateR() { *this = rotate( *this, 3, true ); }
};

Tetromino::Tetromino( char C )
{
   A = matrix( SIZE, vector<int>( SIZE, 0 ) );

   switch( C )
   {
      case 'Q':   A[0][0] = A[1][0] = A[2][0] = A[3][0] = 1;   break;
      case 'I':   A[0][0] = A[0][1] = A[0][2] = A[0][3] = 1;   break;
      case 'L':   A[0][0] = A[1][0] = A[2][0] = A[2][1] = 1;   break;
      case 'J':   A[2][0] = A[2][1] = A[1][1] = A[0][1] = 1;   break;
      case 'S':   A[1][0] = A[1][1] = A[0][1] = A[0][2] = 1;   break;
      case 'N':   A[2][0] = A[1][0] = A[1][1] = A[0][1] = 1;   break;
      case 'T':   A[2][0] = A[1][0] = A[0][0] = A[1][1] = 1;   break;
   }
}

//======================================================================

class Screen
{
public:
   matrix A;
   int rows, cols;

   Screen( int r, int c ) : rows( r ), cols( c ) { clear(); };
   void addTetromino( const Tetromino &T, int ipos = 0, int jpos = 0 );
   void clear() { A = matrix( rows, vector<int>( cols, 0 ) ); };
   void print( char ch = '*', char blank = ' ' );
};

void Screen::addTetromino( const Tetromino &tet, int ipos, int jpos )
{
   for ( int i = 0; i < SIZE; i++ )
   {
      for ( int j = 0; j < SIZE; j++ )
      {
         int ii = ipos + i;
         int jj = jpos + j;
         if ( ii >= 0 && ii < rows && jj >= 0 && jj < cols ) A[ii][jj] = tet.A[i][j];
      }
   }
}

void Screen::print( char ch, char blank )
{
   for ( auto &row : A )
   {
      for ( auto e : row ) cout << ( e ? ch : blank );
      cout << '\n';
   }
}

//======================================================================

Tetromino rotate( const Tetromino &T, int quarterTurns, bool shift )
{
   Tetromino result;

   // Straightforward rotate in a SIZE x SIZE square
   int ii, jj;
   for ( int i = 0; i < SIZE; i++ )
   {
       for ( int j = 0; j < SIZE; j++ )
       {
          switch( quarterTurns )
          {
             case 0:   ii = i           ;   jj = j           ;   break;
             case 1:   ii = SIZE - 1 - j;   jj = i           ;   break;
             case 2:   ii = SIZE - 1 - i;   jj = SIZE - 1 - j;   break;
             case 3:   ii = j           ;   jj = SIZE - 1 - i;   break;
          }
          result.A[ii][jj] = T.A[i][j];
       }
   }
   if ( !shift ) return result;


   // Shift back toward top-left (hmmm; this is a PAIN!)
   int ipos = SIZE, jpos = SIZE;
   for ( int i = 0; i < SIZE; i++ )
   {
      for ( int j = 0; j < SIZE; j++ )
      {
         if ( result.A[i][j] )
         {
            if ( i < ipos ) ipos = i;
            if ( j < jpos ) jpos = j;
         }
      }
   }
   Tetromino result2;
   for ( int i = 0, ii = ipos; ii < SIZE; i++, ii++ )
   {
      for ( int j = 0, jj = jpos; jj < SIZE; j++, jj++ ) result2.A[i][j] = result.A[ii][jj];
   }

   return result2;
}

//======================================================================

int main()
{
   const int GAP = 3;
   vector<Tetromino> allTets = { 'Q', 'I', 'L', 'J', 'S', 'N', 'T' };

   Screen S( SIZE, allTets.size() * ( SIZE + GAP) );
   int jpos = 0;
   for ( auto &t : allTets )
   {
      S.addTetromino( t, 0, jpos );
      jpos += SIZE + GAP;
   }
   S.print();


   cout << "\nRotated left: \n";
   S.clear();   jpos = 0;
   for ( auto &t : allTets )
   {
      t.rotateL();
      S.addTetromino( t, 0, jpos );
      jpos += SIZE + GAP;
   }
   S.print();


   cout << "\nRotated back: \n";
   S.clear();   jpos = 0;
   for ( auto &t : allTets )
   {
      t.rotateR();
      S.addTetromino( t, 0, jpos );
      jpos += SIZE + GAP;
   }
   S.print();
}




*      ****   *       *      **     *     *      
*             *       *     **     **     **     
*             **     **            *      *      
*                                                

Rotated left: 
****   *        *    ***    *      **      *     
       *      ***      *    **      **    ***    
       *                     *                   
       *                                         

Rotated back: 
*      ****   *       *      **     *     *      
*             *       *     **     **     **     
*             **     **            *      *      
*                                                
Last edited on
I can't use classes or pointers.
Everything that you can do in C++ can be done in C, and everything done with pointers can be done without pointers, but note that in the end, everything is a pointer, but you could be putting the data into a fixed sized array (which is just a pointer using the [] operator), or a vector (a array on heap), or & reference (just a pointer that can't ever be NULL), to avoid the use of "pointers" AKA a raw address, but that is still internally the same thing as pointers (and sometimes pointers are very convenient to use).

Putting everything in the global scope, without classes and without pointers is more trouble than you think it is, and will probably make you grow a coding style that isn't effective, or readable, but I can see how lastchance's code could be converted, but I don't see why.
It's my assignment's requirements. That's why. I can't use classes and vectors.
Can someone convert lastchance's code without using classes and vectors? Thanks!
Hmmm we thought your assignment was over.. ;p (thank you teacher for the extra time)
And also nobody will do you homework for you, it's your homework after all. But lot of people here are willing to correct you.

How are you going to print the tetriminos on the board?

I think they expect you to use a 2D array as a board.

+ So use a 2D array for board.

It seems to me, because you have the rotate function, that you aren't supposed to store all the possible directions of a piece in an array.

+ You're only supposed to store every tetrimino piece in its original state.

+ Next you need to have a current tetrimino piece which is rotatable by the user. This is a 2D array that will inherit values from the stored tetrimino pieces.

+ Now you create a function inspired by lastchance's logic (if you're not able to understand then I gave a small explanation on how to think up the logic and some examples in your last post) to flip the current tetrimino piece.

Rest is game logic which will heavily depend on comparing the 2D game board (for example to establish whether piece has hit the ground yet).

Last edited on
Can anybody please help me to understand? I am struggling. Thank you!
What part didn't you understand? Did you read my post? Give us your ideas. What's the fun in solving such problems if you don't want to contribute to your own discussion? Don't rely on somebody else writing your homework for you because nobody here will do that.
Topic archived. No new replies allowed.