@@ -177,6 +177,27 @@ class Field {
177
177
}
178
178
return false ;
179
179
}
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
+ }
180
201
181
202
private:
182
203
uint16_t blocks[FIELD_ROWS][FIELD_COLS]; // Black color means no block
@@ -252,7 +273,22 @@ void LetrisApp::run() {
252
273
dx = 1 ;
253
274
} else if (state.up .justPressed ) {
254
275
// Користувач натиснув вгору
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
+ }
256
292
} else if ((state.down .justPressed || state.a .justPressed ) && !fastDrop) {
257
293
// Користувач натиснув вниз
258
294
fastDrop = true ;
0 commit comments