-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetrispiece.cpp
40 lines (37 loc) · 1.08 KB
/
tetrispiece.cpp
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
#include "tetrispiece.h"
TetrisPiece::TetrisPiece(TetrisShape shape) :
pieceShape{shape}
{}
/* left rotation: x -> y
* y -> -x
*
* right rotation: x -> -y
* y -> x
*/
TetrisPiece TetrisPiece::rotate(Direction dir)
{
for(int i = 0; i < 4; ++i) {
std::swap(coordinates[i][0], coordinates[i][1]);
coordinates[i][dir] *= -1;
}
return *this;
}
void TetrisPiece::initShape(TetrisShape shape)
{
// all existing shapes
constexpr qint8 shapes[8][4][2]{
{{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }},
{{ 0, 1 }, { 0, 0 }, { 0,-1 }, { 1,-1 }},
{{ 0, 1 }, { 0, 0 }, { 0,-1 }, {-1,-1 }},
{{-1, 1 }, { 0, 1 }, { 0, 0 }, { 1, 0 }},
{{ 1, 1 }, { 0, 1 }, { 0, 0 }, {-1, 0 }},
{{ 0, 2 }, { 0, 1 }, { 0, 0 }, { 0,-1 }},
{{ 0, 0 }, { 0, 1 }, { 1, 1 }, { 1, 0 }},
{{-1, 0 }, { 0, 0 }, { 1, 0 }, { 0, 1 }}
};
for(int i = 0; i < 4; ++i) {
coordinates[i][0] = shapes[shape][i][0];
coordinates[i][1] = shapes[shape][i][1];
}
pieceShape = shape;
}