-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tetromino.cpp
285 lines (253 loc) · 6.62 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* Tetromino
*
* $Revision$
* $Id$
*
* @see Tetromino.h
*
*/
#include "Tetromino.h"
#include <iostream>
CTetromino::CTetromino(int cellCoordsX[4], int cellCoordsY[4], int maxRotation, CELL_TYPE type) {
m_type = type;
m_x = -10;
m_y = -10;
m_rotationMax = maxRotation;
m_rotation = 0;
for(int i=0; i<4; i++) {
m_cellCoordsX[i] = cellCoordsX[i];
m_cellCoordsY[i] = cellCoordsY[i];
}
board = 0;
m_ghost = false;
m_ghostOnBoard = false;
m_gy = 0;
}
CTetromino::~CTetromino(void) {
}
// ================= METODIT =============================================
bool CTetromino::rotateRight() {
if(!isAttached() || m_rotationMax == 0) return false;
if(canMoveTo(m_x, m_y, getNextRotation())) {
removeFromBoard();
setRotation(getNextRotation());
insertToBoard();
return true;
}
return false;
}
bool CTetromino::rotateLeft() {
if(!isAttached() || m_rotationMax == 0) return false;
if(canMoveTo(m_x, m_y, getPreviousRotation())) {
removeFromBoard();
setRotation(getPreviousRotation());
insertToBoard();
return true;
}
return false;
}
bool CTetromino::isAttached() {
return (board != 0) ? true : false;
}
bool CTetromino::attach(CTetrisBoard *targetBoard) {
return attach(targetBoard, 0, 0);
}
bool CTetromino::attach(CTetrisBoard *targetBoard, int offsetY) {
return attach(targetBoard, 0, offsetY);
}
bool CTetromino::attach(CTetrisBoard *targetBoard, int offsetX, int offsetY) {
if(board != 0)
detach(true);
int x = getBoardCenterX(targetBoard) + offsetX;
int y = targetBoard->getHeight()-1 + offsetY;
if(y < 0) return false;
if(canMoveTo(x, y, m_rotation, targetBoard)) {
m_x = x;
m_y = y;
board = targetBoard;
insertToBoard();
return true;
}
return false;
}
bool CTetromino::detach(bool clear) {
if(clear && board != 0)
removeFromBoard();
board = 0;
return true;
}
bool CTetromino::detach() {
return detach(false);
}
bool CTetromino::moveLeft() {
if(!isAttached()) return false;
if(canMoveTo(m_x-1, m_y, m_rotation)) {
removeFromBoard();
m_x--;
insertToBoard();
return true;
}
return false;
}
bool CTetromino::moveRight() {
if(!isAttached()) return false;
if(canMoveTo(m_x+1, m_y, m_rotation)) {
removeFromBoard();
m_x++;
insertToBoard();
return true;
}
return false;
}
bool CTetromino::moveUp(int n) {
if(!isAttached()) return false;
return moveDown(-n);
}
bool CTetromino::moveDown() {
if(!isAttached()) return false;
return moveDown(1);
}
bool CTetromino::moveDown(int n) {
if(!isAttached()) return false;
if(canMoveTo(m_x, m_y-n, m_rotation)) {
removeFromBoard();
m_y -= n;
insertToBoard();
return true;
}
return false;
}
bool CTetromino::drop() {
if(!isAttached()) return false;
int y = m_y;
while( canMoveTo(m_x, y-1, m_rotation) ) {
y--;
}
if(y == m_y)
return false; // ei voitu tiputtaa yhtään
removeFromBoard();
m_y = y;
insertToBoard();
return true;
}
bool CTetromino::hasLanded() {
if(board == 0) return true;
if( !canMoveTo(m_x, m_y - 1, m_rotation) )
return true;
return false;
}
bool CTetromino::isFullyVisible() {
if(!isAttached()) return false;
// etsi ylin y-koordinaatti ja vertaa sitä laudan ylimpään koordinaattiin
for(int i=0; i<4; i++)
if(m_y + getRelativeY(i, m_rotation) > board->getHeight() - 1)
return false;
return true;
}
// ==================== PRIVATE ====================
int CTetromino::getBoardCenterX(CTetrisBoard *targetBoard) {
int center = (targetBoard->getWidth() - 1) / 2;
return center;
}
bool CTetromino::setRotation(int r) {
if(0 > r || r > m_rotationMax)
return false;
m_rotation = r;
// insertToBoard();
return true;
}
bool CTetromino::canMoveTo(const int x, const int y, const int rotation, CTetrisBoard *targetBoard) {
// tarkistetaan onko tulevan sijainnin kaikki paikat boardissa tyhjiä
for(int i=0; i<4; i++) {
// testataan: a) koordinaatti ei ole oma nykyinen, b) on tyhjä laudalla
int tx = x + getRelativeX(i, rotation);
int ty = y + getRelativeY(i, rotation);
if(!containsBoardCoord(tx, ty))
if(!targetBoard->isEmpty(tx, ty))
return false;
}
return true;
}
bool CTetromino::canMoveTo(const int x, const int y, const int rotation) {
if(!isAttached()) return false;
return canMoveTo(x, y, rotation, board);
}
int CTetromino::getRelativeX(const int x, const int rotation) {
switch(rotation) {
case 0: return m_cellCoordsX[x];
case 3: return -m_cellCoordsY[x];
case 2: return -m_cellCoordsX[x];
case 1: return m_cellCoordsY[x];
}
return -1;
}
int CTetromino::getRelativeY(const int y, const int rotation) {
switch(rotation) {
case 0: return m_cellCoordsY[y];
case 3: return m_cellCoordsX[y];
case 2: return -m_cellCoordsY[y];
case 1: return -m_cellCoordsX[y];
}
return -1;
}
bool CTetromino::containsBoardCoord(int x, int y) {
for(int i=0; i<4; i++)
if(
m_x + getRelativeX(i, m_rotation) == x &&
m_y + getRelativeY(i, m_rotation) == y
) return true;
return false;
}
void CTetromino::insertToBoard() {
if(!isAttached()) return;
for(int i=0; i<4; i++) {
board->setSlot(
m_x + getRelativeX(i, m_rotation),
m_y + getRelativeY(i, m_rotation),
m_type
);
}
insertGhostToBoard();
board->update();
}
void CTetromino::insertGhostToBoard() {
if(!isAttached() || !m_ghost) return;
int gy = m_y;
int gx = m_x;
while(canMoveTo(gx, gy, m_rotation)) {
gy--;
}
gy++;
if(gy != m_y) {
for(int i=0; i<4; i++) {
int rgx = gx + getRelativeX(i, m_rotation);
int rgy = gy + getRelativeY(i, m_rotation);
if(!containsBoardCoord(rgx, rgy))
board->setSlot(rgx, rgy, m_ghostType);
}
m_gy = gy;
m_ghostOnBoard = true;
}
}
void CTetromino::removeFromBoard() {
if(!isAttached()) return;
for(int i=0; i<4; i++) {
board->setSlot(
m_x + getRelativeX(i, m_rotation),
m_y + getRelativeY(i, m_rotation),
EMPTY
);
}
removeGhostFromBoard();
board->update();
}
void CTetromino::removeGhostFromBoard() {
if(!isAttached() || !m_ghost || !m_ghostOnBoard) return;
for(int i=0; i<4; i++) {
int rgx = m_x + getRelativeX(i, m_rotation);
int rgy = m_gy + getRelativeY(i, m_rotation);
if(!containsBoardCoord(rgx, rgy) && board->getSlot(rgx, rgy) == m_ghostType)
board->setSlot(rgx, rgy, EMPTY);
}
}