Skip to content

Commit b1aad74

Browse files
authored
Merge pull request #216 from St3p40/st3p40/fix_letris
Letris: Implement limitation for figure rotation
2 parents ec597a4 + 7d00b50 commit b1aad74

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

firmware/keira/src/apps/demos/letris.cpp

+37-1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,27 @@ class Field {
177177
}
178178
return false;
179179
}
180+
bool canRotate(const Shape* shape) {
181+
// Створюємо новий масив для фігури після повороту
182+
int newShapeData[4][4];
183+
for (int i = 0; i < 4; i++) {
184+
for (int j = 0; j < 4; j++) {
185+
newShapeData[i][j] = shape->shapeData[3 - j][i];
186+
}
187+
}
188+
// Перевіряємо, чи нова позиція фігури після повороту зіткнеться з іншими блоками
189+
for (int yy = 0; yy < 4; yy++) {
190+
for (int xx = 0; xx < 4; xx++) {
191+
if (newShapeData[yy][xx]) {
192+
if (shape->x + xx < 0 || shape->x + xx >= FIELD_COLS || shape->y + yy >= FIELD_ROWS ||
193+
this->blocks[shape->y + yy][shape->x + xx]) {
194+
return false; // Поворот неможливий
195+
}
196+
}
197+
}
198+
}
199+
return true; // Поворот можливий
200+
}
180201

181202
private:
182203
uint16_t blocks[FIELD_ROWS][FIELD_COLS]; // Black color means no block
@@ -252,7 +273,22 @@ void LetrisApp::run() {
252273
dx = 1;
253274
} else if (state.up.justPressed) {
254275
// Користувач натиснув вгору
255-
shape.rotate();
276+
auto shapeX = shape.x;
277+
auto quitRotation = false;
278+
for (auto i = 0; i <= 2; i++) { // check x + - i
279+
for (auto j = 0; j <= 1; j++) {
280+
auto tmpShape = shape;
281+
auto sign = j == 0 ? 1 : -1; // check left-right
282+
tmpShape.x = shapeX + i * sign;
283+
if (field.canRotate(&tmpShape)) {
284+
shape = tmpShape;
285+
shape.rotate();
286+
quitRotation = true;
287+
break;
288+
}
289+
}
290+
if (quitRotation) break;
291+
}
256292
} else if ((state.down.justPressed || state.a.justPressed) && !fastDrop) {
257293
// Користувач натиснув вниз
258294
fastDrop = true;

0 commit comments

Comments
 (0)