-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetromino.cpp
104 lines (88 loc) · 1.6 KB
/
Tetromino.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
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
#include "Painter.hpp"
#include "Tetromino.hpp"
const char* Tetromino::shapes[7] =
{
" 8 " // I
" 8 "
" 8 "
" 8 ",
" 8 " // J
" 8 "
" 88 "
" ",
" 8 " // L
" 8 "
" 88 "
" ",
" " // O
" 88 "
" 88 "
" ",
" 8 " // S
" 88 "
" 8 "
" ",
" 8 " // Z
" 88 "
" 8 "
" ",
" " // T
" 888"
" 8 "
" "
};
Tetromino::Tetromino(Name name) : _name(name), _x(5), _y(0), _angle(0)
{
}
void Tetromino::paint(Painter& p) const
{
p.paint(*this);
}
unsigned int Tetromino::getName(void) const
{
return _name;
}
bool Tetromino::map(int x, int y) const
{
static const struct
{
int x, y;
} ROTATE[][16] = {
{
{ 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 },
{ 1, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 },
{ 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 },
{ 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 3 }
},
{
{ 3, 0 }, { 2, 0 }, { 1, 0 }, { 0, 0 },
{ 3, 1 }, { 2, 1 }, { 1, 1 }, { 0, 1 },
{ 3, 2 }, { 2, 2 }, { 1, 2 }, { 0, 2 },
{ 3, 3 }, { 2, 3 }, { 1, 3 }, { 0, 3 }
},
{
{ 3, 3 }, { 3, 2 }, { 3, 1 }, { 3, 0 },
{ 2, 3 }, { 2, 2 }, { 2, 1 }, { 2, 0 },
{ 1, 3 }, { 1, 2 }, { 1, 1 }, { 1, 0 },
{ 0, 3 }, { 0, 2 }, { 0, 1 }, { 0, 0 }
},
{
{ 0, 3 }, { 1, 3 }, { 2, 3 }, { 3, 3 },
{ 0, 2 }, { 1, 2 }, { 2, 2 }, { 3, 2 },
{ 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 },
{ 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 }
}
};
bool val = shapes[_name]
[ROTATE[_angle][y * 4 + x].y * 4 + ROTATE[_angle][y * 4 + x].x] != ' ';
return val;
}
void Tetromino::move(int x, int y)
{
_x += x;
_y += y;
}
void Tetromino::rotate(int angle)
{
_angle = (_angle + angle + 4) % 4;
}