Tetris Help

I am trying to start a project that makes the game, Tetris. For this part, I need to generate seven unique 2-dimensional arrays that will represent Tetriminos (Tetris pieces), and also 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 got have a start, but I don't know how to put in the three functions. 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
  #include <iostream>

using namespace std;

/* Array containing tetromino pieces and their rotations */
const int PIECES[NUM_PIECES][NUM_ROTATIONS][NUM_BLOCKS][2] = {
	{
		/*
		 *	TETROMINO_I
		 *	0000
		 *	1111
		 *	0000
		 *	0000
		 */
		{{1, 0}, {1, 1}, {1, 2}, {1, 3}},
		{{0, 2}, {1, 2}, {2, 2}, {3, 2}},
		{{2, 0}, {2, 1}, {2, 2}, {2, 3}},
		{{0, 1}, {1, 1}, {2, 1}, {3, 1}}
	},
	{
		/*
		 *	TETROMINO_L
		 *	0010
		 *	1110
		 *	0000
		 *	0000
		 */
		{{1, 0}, {1, 1}, {1, 2}, {0, 2}},
		{{0, 1}, {1, 1}, {2, 1}, {2, 2}},
		{{1, 0}, {1, 1}, {1, 2}, {2, 0}},
		{{0, 0}, {0, 1}, {1, 1}, {2, 1}}
	},
	{
		/*
		 *	TETROMINO_J
		 *	1000
		 *	1110
		 *	0000
		 *	0000
		 */
		{{0, 0}, {1, 0}, {1, 1}, {1, 2}},
		{{0, 1}, {0, 2}, {1, 1}, {2, 1}},
		{{1, 0}, {1, 1}, {1, 2}, {2, 2}},
		{{0, 1}, {1, 1}, {2, 0}, {2, 1}}
	},
	{
		/*
		 *	TETROMINO_O
		 *	0110
		 *	0110
		 *	0000
		 *	0000
		 */
		{{0, 1}, {0, 2}, {1, 1}, {1, 2}},
		{{0, 1}, {0, 2}, {1, 1}, {1, 2}},
		{{0, 1}, {0, 2}, {1, 1}, {1, 2}},
		{{0, 1}, {0, 2}, {1, 1}, {1, 2}}
	},
	{
		/*
		 *	TETROMINO_S
		 *	0110
		 *	1100
		 *	0000
		 *	0000
		 */
		{{0, 1}, {0, 2}, {1, 0}, {1, 1}},
		{{0, 1}, {1, 1}, {1, 2}, {2, 2}},
		{{1, 1}, {1, 2}, {2, 0}, {2, 1}},
		{{0, 0}, {1, 0}, {1, 1}, {2, 1}},
	},
	{
		/*
		 *	TETROMINO_T
		 *	0100
		 *	1110
		 *	0000
		 *	0000
		 */
		{{0, 1}, {1, 0}, {1, 1}, {1, 2}},
		{{0, 1}, {1, 1}, {1, 2}, {2, 1}},
		{{1, 0}, {1, 1}, {1, 2}, {2, 1}},
		{{0, 1}, {1, 0}, {1, 1}, {2, 1}}
	},
	{
		/*
		 *	TETROMINO_Z
		 *	1100
		 *	0110
		 *	0000
		 *	0000
		 */
		{{0, 0}, {0, 1}, {1, 1}, {1, 2}},
		{{0, 2}, {1, 1}, {1, 2}, {2, 1}},
		{{1, 0}, {1, 1}, {2, 1}, {2, 2}},
		{{0, 1}, {1, 0}, {1, 1}, {2, 0}}
	}
};
one way to do it FAST is to have the rotated shapes stored as well. eg Z and N both, instead of just Z and trying to rotate it. If you want to apply a rotation matrix instead, you can do that, and yours is one of the most simple (I think 90 degrees will just be 1s and zeros, but its been a while). I don't recommend trying to rotate using ascii-art 'graphics' though, unless you just want to learn how to do it.

Shifting it is just going to be a redraw. The clunky way is to keep all the text in a big array and update that, clear the screen and re-draw it. This causes screen flash because the clear screen and redraw are just too slow. The clean way to do it is to draw over what was there last time with cursor positioning functions (nonstandard c++, look at gotoxy and ansi tools, ncurses etc) which make use of 3rd party libraries to do these kinds of things.
Currently at work so limited access to much but consider

bool T[16]{
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0
}

bool rotateLeft[16]{
T[ 4],T[ 8],T[12],T[16],
T[ 3],T[ 7],T[11],T[15],
T[ 2],T[ 6],T[10],T[14],
T[ 1],T[ 5],T[ 9],T[13]
}

rotate left would move all items into the 2nd array which can then just be copied into original. Hope that makes sense, when get home can try and explain a little better.

--Ignore that, rotate left is in incorrect ordering.... left up to give an approach, again will update once home.
Last edited on
yea that would work.
and for some shapes, left and right are going to be identical anyway. For others you need both.
Last edited on
Got home and looking at it, the rotate left would work, guess that shows you shouldn't mess about on internet while at work.

As for left and right being identical, it wouldn't except for the square piece if all 3 centred.

Take the bar/long piece,

say we got
0000
1111
0000
0000

going left it would become
0100
0100
0100
0100

right
0010
0010
0010
0010

Maxster, that enough for you to work out? If not ask and I will try and explain a little further how I would go about it in such a method.
I am just struggling on how to code it and put it into the functions. I just hope you can help me with that.
There are probably better ways to do this but here's the simplest way to do it while using functions.

First you have to note that all pieces are 4x4 square matrices.
Now, think about how you would copy elements from one 4x4 matrix to another 4x4 matrix exactly.

You would use two for-loops like so:
1
2
3
for( int i = 0; i < rows; i++)
   for( int j = 0; i < columns; j++)
      array_1[i][j] = array_2[i][j];


Did that give you a hint already?

Okay so what if you were to do something like this instead:
1
2
3
for( int i = 0; i < rows; i++)
   for( int j = 0; i < columns; j++)
      array_1[i][j] = array_2[rows-i-1][columns-j-1];


If you look at it closely, you will observe that the elements are being copied vertically backwards! That's a 180* flip (how would you do a horizontal flip then)! But of course that's not what we want.

What would happen if we wrote:
1
2
3
for( int i = 0; i < rows; i++)
   for( int j = 0; i < columns; j++)
      array_1[i][j] = array_2[j][i];

I think you'll figure it out ;) but of course if you don't then you can come back here.
jonnin wrote:
one way to do it FAST is to have the rotated shapes stored as well

Aren't the rotated shapes already stored in the given data?
Am I missing something here?
Yeah Dutch is right, I just realised from reading the array indexes

Then what is the function supposed to do? Seems to me that a class/structure would be suitable here and the function just changes the objects value.
Last edited on
I can't use classes to make the Tetris game. I have that code because it was the only way for me to figure out. I am struggling on this assignment. As well as, not having enough time to code this assignment. And it is literally due tomorrow at 10 am.
Topic archived. No new replies allowed.