-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
710 lines (659 loc) · 25.7 KB
/
main.c
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <math.h>
// Variables that we'll want to remain static or mostly static.
static int FPS = 30;
int height = 640; //We can use argv to modify these, and otherwise set a default
int width = 480;
int y_speed; // how fast is current tetromino moving? This value should be updating.
static int DX = 54; // pixels necessary to move from column to column/row to row
static int DY = 54;
static int starting_x = 218; // Aligns with column five.
static int starting_y = 43; // aligns with row 0.
int score = 0;
int total_cleared = 0;
int level = 0;
typedef struct Block { // generic struct for the blocks that make up a tetromino
int x;
int y;
int type;
} Block;
typedef struct Tetromino {
struct Block *blocks[4];
int arrangement; // Range from 0 to 3, to cover all rotations.
int type; // Range 0 to 6, so we know shape when rotating.
int x1; // low end of x range
int x2; // high end of x range
int y; // lowest point of the tet
} Tetromino;
// Declaration of various functions.
void move_left(struct Block *board[21][10], struct Tetromino *current);
void move_right(struct Block *board[21][10], struct Tetromino *current);
void drop(struct Tetromino *current, struct Block *board[21][10]);
void rotate(struct Tetromino *current, int direction, struct Block *board[21][10]);
bool rebalance(struct Tetromino *current, struct Block *board[21][10]);
bool is_game_over(struct Tetromino *current, struct Block *board[21][10]);
bool rotation_is_legal(struct Tetromino *new, struct Block *board[21][10]);
/* default_movement calls create_tet if current has settled, and returns pointer
to whatever our current tet is. */
struct Tetromino* default_movement(struct Block *board[21][10],
struct Tetromino *current);
int clear_lines(struct Block *board[21][10]);
void draw_screen(struct Block *board[21][10], struct Tetromino *current,
ALLEGRO_BITMAP *background, ALLEGRO_BITMAP *shapes[7]);
struct Tetromino* create_tetromino();
int main(int argc, char *argv[]) {
//create miscellaneous variables!
srand(time(NULL));
bool running = true;
bool draw = true;
struct Tetromino *current;
// Various initializations, confirmations that everything is working. //
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *queue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_KEYBOARD_STATE state;
if (!al_init()) {
printf("Error: failed to initialize Allegro.\n");
return 1;
}
timer = al_create_timer(1.0 / FPS);
if (!timer) {
printf("Error: failed to initialize timer.\n");
return 1;
}
display = al_create_display(width, height);
if (!display) {
printf("Error: failed to initialize display.\n");
return 1;
}
al_install_keyboard();
al_init_image_addon();
// Loading images of the various shapes. //
ALLEGRO_BITMAP *shapes[7];
ALLEGRO_BITMAP *red = al_load_bitmap("red.png");
ALLEGRO_BITMAP *orange = al_load_bitmap("orange.png");
ALLEGRO_BITMAP *yellow = al_load_bitmap("yellow.png");
ALLEGRO_BITMAP *green = al_load_bitmap("green.png");
ALLEGRO_BITMAP *blue = al_load_bitmap("blue.png");
ALLEGRO_BITMAP *indigo = al_load_bitmap("indigo.png");
ALLEGRO_BITMAP *violet = al_load_bitmap("violet.png");
ALLEGRO_BITMAP *background = al_load_bitmap("background.png");
shapes[0] = red;
shapes[1] = orange;
shapes[2] = yellow;
shapes[3] = green;
shapes[4] = blue;
shapes[5] = indigo;
shapes[6] = violet;
// Create event queue, register event sources, start timer.
queue = al_create_event_queue();
al_register_event_source(queue, al_get_timer_event_source(timer));
al_register_event_source(queue, al_get_display_event_source(display));
al_register_event_source(queue, al_get_keyboard_event_source());
al_start_timer(timer);
// Create an initial tetromino and a board.
current = create_tetromino();
struct Block *board[21][10]; // 21 rows height, 10 columns wide. Top row is
// just a buffer, but causes game over if used.
for (int y = 0; y < 21; ++y) {
for (int x = 0; x < 10; ++x) {
board[y][x] = NULL;
}
}
while (running) {
ALLEGRO_EVENT event;
ALLEGRO_TIMEOUT timeout;
al_init_timeout(&timeout, 0.06);
bool get_event = al_wait_for_event_until(queue, &event, &timeout);
al_get_keyboard_state(&state);
if (get_event) {
switch (event.type) {
case ALLEGRO_EVENT_TIMER:
draw = true;
break;
case ALLEGRO_EVENT_DISPLAY_CLOSE:
running = false;
break;
case ALLEGRO_EVENT_KEY_DOWN:
switch (event.keyboard.keycode) {
case ALLEGRO_KEY_LEFT:
move_left(board, current);
break;
case ALLEGRO_KEY_RIGHT:
move_right(board, current);
break;
case ALLEGRO_KEY_UP:
drop(current, board);
/* normally default_movement calls create_tet, but drop
circumvents default movement so we call create_tet manually. */
current = create_tetromino();
break;
case ALLEGRO_KEY_Q:
running = false;
break;
case ALLEGRO_KEY_A:
rotate(current, -1, board);
break;
case ALLEGRO_KEY_S:
rotate(current, 1, board);
default:
break;
}
default:
break;
}
if (al_key_down(&state, ALLEGRO_KEY_DOWN)) { // if down key is held, boost speed
y_speed += 10;
current = default_movement(board, current);
y_speed -= 10;
}
}
if (draw && al_is_event_queue_empty(queue)) {
draw = false;
draw_screen(board, current, background, shapes);
current = default_movement(board, current);
}
//update game variables - score, level, speed, etc.
int lines_cleared = clear_lines(board);
total_cleared += lines_cleared;
score += pow(2, lines_cleared) * 100;
level = 1 + total_cleared / 10;
y_speed = 5 + level;
if (is_game_over(current, board)) {
running = false;
}
}
al_destroy_bitmap(red);
al_destroy_bitmap(orange);
al_destroy_bitmap(yellow);
al_destroy_bitmap(green);
al_destroy_bitmap(blue);
al_destroy_bitmap(indigo);
al_destroy_bitmap(violet);
al_destroy_bitmap(background);
al_destroy_display(display);
al_destroy_event_queue(queue);
return 0;
}
bool is_game_over(struct Tetromino *current, struct Block *board[21][10]) {
if (current->blocks[0]->y == starting_y) { // if we're at the start and in a block's
for (int i = 0; i < 4; i++) { // space, we have lost and the game ends.
if (board[0][current->blocks[i]->x / DX]) {
return true;
}
}
}
/* The below solution is cleaner, but currently doesn't work. Problem is
board[20] never gets accessed. We need to adjust such that the top line of
the grid is actually board[1], not board[0], and board [0] functionally
becomes a "negative" row. Then change the if loop below to if (board[0][i])
and voila. */
// for (int i = 0; i < 10; ++i) {
// if(board[20][i]) {
// return false;
// }
// }
return false;
}
struct Tetromino* default_movement(struct Block *board[21][10],
struct Tetromino *current) {
current->y += y_speed;
bool should_create_new_tet = rebalance(current, board);
if (should_create_new_tet) {
free(current);
current = create_tetromino();
}
return current;
}
/* Checks if a block has landed on another block, in which case we rebalance the
current tet to neatly fit the grid */
bool rebalance(struct Tetromino *current, struct Block *board[21][10]) {
bool should_rebalance = false;
struct Block *cur_block;
int delta;
for (int i = 0; i < 4; ++i) {
cur_block = current->blocks[i];
cur_block->y += y_speed;
if (current->y > 0) {
if (board[cur_block->y / DY][cur_block->x / DX]) {
delta = cur_block->y - board[cur_block->y / DY][cur_block->x / DX]->y;
should_rebalance = true;
} else if (cur_block->y > 20 * DY) {
delta = cur_block-> y - (starting_y + 20 * DY);
should_rebalance = true;
}
}
}
if (should_rebalance) {
for (int i = 0; i < 4; ++i) {
cur_block = current->blocks[i];
cur_block->y -= delta + DY;
int x = cur_block->x / DX;
int y = cur_block->y / DY;
struct Block *new;
new = (struct Block*) malloc(sizeof(struct Block));
new->x = cur_block->x;
new->y = cur_block->y;
new->type = cur_block->type;
if (y >= 0 && y < 20) {
board[y][x] = new;
}
free(current->blocks[i]);
}
}
return should_rebalance;
}
void draw_screen(struct Block *board[21][10], struct Tetromino *current,
ALLEGRO_BITMAP *background, ALLEGRO_BITMAP *shapes[7]) {
struct Block *cur_block;
for (int i = 0; i < 4; ++i) {
cur_block = current->blocks[i];
al_draw_bitmap(shapes[cur_block->type], cur_block->x, cur_block->y, 0);
}
al_draw_bitmap(background, 10, 50, 0);
for (int y = 0; y < 20; ++y) {
for (int x = 0; x < 10; ++x) {
if (board[y][x]) {
cur_block = board[y][x];
al_draw_bitmap(shapes[cur_block->type], cur_block->x,
cur_block->y, 0);
}
}
}
al_flip_display();
al_clear_to_color(al_map_rgb(180, 180, 180));
}
void move_left(struct Block *board[21][10], struct Tetromino *current) {
if ((current->x1 / DX) <= 0) {
return;
}
for (int i = 0; i < 4; ++i) {
struct Block *cur_block = current->blocks[i];
if (board[cur_block->y / DY][cur_block->x / DX - 1]) {
return;
}
}
for (int i = 0; i < 4; ++i) {
current->blocks[i]->x -= DX;
}
current->x1 -= DX;
current->x2 -= DX;
}
void move_right(struct Block *board[21][10], struct Tetromino *current) {
if (current->x2 / DX >= 9) {
return;
}
for (int i = 0; i < 4; ++i) {
struct Block *cur_block = current->blocks[i];
if (board[cur_block->y / DY][cur_block->x / DX + 1]) {
return;
}
}
for (int i = 0; i < 4; ++i) {
current->blocks[i]->x += DX;
}
current->x1 += DX;
current->x2 += DX;
}
void drop(struct Tetromino *current, struct Block *board[21][10]) {
bool should_drop = true;
struct Block *cur_block;
while (should_drop) {
for (int i = 0; i < 4; i++) {
cur_block = current->blocks[i];
cur_block->y += y_speed;
if (board[cur_block->y / DY][cur_block->x / DX]) {
should_drop = false;
} else if (cur_block->y > 20 * DY) {
should_drop = false;
}
}
current->y += DY;
}
rebalance(current, board);
}
int clear_lines(struct Block *board[21][10]) {
int cleared = 0;
bool clear_line;
for (int y = 1; y < 20; y++) {
clear_line = true;
for (int x = 0; x < 10; x++) {
if (!board[y][x]) {
clear_line = false;
break;
}
}
if (clear_line) {
cleared++;
for (int i = y; i > 0; i--) {
for (int j = 0; j < 10; j++) {
if (board[i-1][j]) {
board[i][j] = board[i-1][j];
board[i][j]->y += DY;
} else {
board[i][j] = NULL;
}
}
}
for (int i = 0; i < 10; i++) {
board[0][i] = NULL;
}
}
}
return cleared;
}
struct Tetromino* create_tetromino() {
struct Tetromino *new_tet;
new_tet = (struct Tetromino*) malloc(sizeof(struct Tetromino));
int type = rand() % 7; // Determine which of the seven types of tet we get.
new_tet->type = type;
new_tet->arrangement = 0;
/* create blocks, put them into the new_tet.blocks arr. set their x and y
coordinates. Track total tet x and y ranges, so we know to stop if any
of them fall out of that range. */
switch (type) {
case 0: // stick block
for (int i = 0; i < 4; ++i) {
struct Block *new_block;
new_block = (struct Block*) malloc(sizeof(struct Block));
new_block->x = starting_x;
new_block->y = starting_y - (i * DY);
new_tet->blocks[i] = new_block;
}
break;
case 1: // square.
for (int i = 0; i < 4; ++i) {
struct Block *new_block;
new_block = (struct Block*) malloc(sizeof(struct Block));
new_tet->blocks[i] = new_block;
}
new_tet->blocks[0]->x = starting_x;
new_tet->blocks[0]->y = starting_y;
new_tet->blocks[1]->x = starting_x + DX;
new_tet->blocks[1]->y = starting_y;
new_tet->blocks[2]->x = starting_x;
new_tet->blocks[2]->y = starting_y - DY;
new_tet->blocks[3]->x = starting_x + DX;
new_tet->blocks[3]->y = starting_y - DY;
break;
case 2: // T block;
for (int i = 0; i < 4; ++i) {
struct Block *new_block;
new_block = (struct Block*) malloc(sizeof(struct Block));
new_tet->blocks[i] = new_block;
}
new_tet->blocks[0]->x = starting_x;
new_tet->blocks[0]->y = starting_y;
new_tet->blocks[1]->x = starting_x;
new_tet->blocks[1]->y = starting_y - DY;
new_tet->blocks[2]->x = starting_x - DX;
new_tet->blocks[2]->y = starting_y - DY;
new_tet->blocks[3]->x = starting_x + DX;
new_tet->blocks[3]->y = starting_y - DY;
break;
case 3: // El block;
for (int i = 0; i < 4; ++i) {
struct Block *new_block;
new_block = (struct Block*) malloc(sizeof(struct Block));
new_tet->blocks[i] = new_block;
}
new_tet->blocks[0]->x = starting_x;
new_tet->blocks[0]->y = starting_y;
new_tet->blocks[1]->x = starting_x + DX;
new_tet->blocks[1]->y = starting_y;
new_tet->blocks[2]->x = starting_x;
new_tet->blocks[2]->y = starting_y - DY;
new_tet->blocks[3]->x = starting_x;
new_tet->blocks[3]->y = starting_y - 2 * DY;
break;
case 4: // reverse el block;
for (int i = 0; i < 4; ++i) {
struct Block *new_block;
new_block = (struct Block*) malloc(sizeof(struct Block));
new_tet->blocks[i] = new_block;
}
new_tet->blocks[0]->x = starting_x;
new_tet->blocks[0]->y = starting_y;
new_tet->blocks[1]->x = starting_x - DX;
new_tet->blocks[1]->y = starting_y;
new_tet->blocks[2]->x = starting_x;
new_tet->blocks[2]->y = starting_y - DY;
new_tet->blocks[3]->x = starting_x;
new_tet->blocks[3]->y = starting_y - 2 * DY;
break;
case 5: // dog block
for (int i = 0; i < 4; ++i) {
struct Block *new_block;
new_block = (struct Block*) malloc(sizeof(struct Block));
new_tet->blocks[i] = new_block;
}
new_tet->blocks[0]->x = starting_x;
new_tet->blocks[0]->y = starting_y;
new_tet->blocks[1]->x = starting_x - DX;
new_tet->blocks[1]->y = starting_y;
new_tet->blocks[2]->x = starting_x;
new_tet->blocks[2]->y = starting_y - DY;
new_tet->blocks[3]->x = starting_x + DX;
new_tet->blocks[3]->y = starting_y - DY;
break;
case 6: // reverse dog block
for (int i = 0; i < 4; ++i) {
struct Block *new_block;
new_block = (struct Block*) malloc(sizeof(struct Block));
new_tet->blocks[i] = new_block;
}
new_tet->blocks[0]->x = starting_x;
new_tet->blocks[0]->y = starting_y;
new_tet->blocks[1]->x = starting_x + DX;
new_tet->blocks[1]->y = starting_y;
new_tet->blocks[2]->x = starting_x;
new_tet->blocks[2]->y = starting_y - DY;
new_tet->blocks[3]->x = starting_x - DX;
new_tet->blocks[3]->y = starting_y - DY;
break;
default:
printf("Error: illegal tetromino type selection.\n");
exit (1);
break;
}
for (int i = 0; i < 4; ++i) {
new_tet->blocks[i]->type = type;
}
new_tet->x1 = new_tet->x2 = new_tet->blocks[0]->x; // This guarantees x1, etc.
new_tet->y = new_tet->blocks[0]->y; // will actually update.
for (int i = 0; i < 4; ++i) {
new_tet->x1 = (new_tet->x1 > new_tet->blocks[i]->x) ?
new_tet->blocks[i]->x : new_tet->x1;
new_tet->x2 = (new_tet->x2 < new_tet->blocks[i]->x) ?
new_tet->blocks[i]->x : new_tet->x2;
new_tet->y = (new_tet->y > new_tet->blocks[i]->y) ?
new_tet->blocks[i]->y : new_tet->y;
}
return new_tet;
}
void rotate(struct Tetromino *current, int direction, struct Block *board[21][10]) {
/* First we create a copy of current to apply rotations to. This way we can
make changes, see if they are legal, and only then apply them to the
current tetromino. */
struct Tetromino *new;
new = (struct Tetromino*) malloc(sizeof(struct Tetromino));
new->type = current->type;
new->x1 = current->x1;
new->x2 = current->x2;
new->y = current->y;
new->arrangement = (current->arrangement + direction) % 4;
if (new->arrangement < 0) { // % is remainder in C, not modulo, so we can
new->arrangement += 4; // end up with negative values.
}
for (int i = 0; i < 4; ++i) {
struct Block *new_block;
new_block = (struct Block*) malloc(sizeof(struct Block));
new_block->x = current->blocks[i]->x;
new_block->y = current->blocks[i]->y;
new_block->type = current->blocks[i]->type;
new->blocks[i] = new_block;
}
struct Block *cur_block; // Lets us look at individual blocks.
int cur_y = new->blocks[0]->y; // Determine X and Y of center block,
int cur_x = new->blocks[0]->x; // to modulate other blocks around.
switch (new->type) {
case 0: // line. Unfortunately lines work kind of wonky, because the central
// block is the second one. This makes it rotate slightly nicer IMO,
// but does lead to some slightly wonky arithmetic.
switch (new->arrangement) {
case 0: case 2:
for (int i = 0; i < 4; i++) {
cur_block = new->blocks[i];
cur_block->x = cur_x + DX;
cur_block->y = cur_y - (i * DY);
}
break;
case 1: case 3:
for (int i = 0; i < 4; i++) {
cur_block = new->blocks[i];
cur_block->y = cur_y;
cur_block->x = cur_x + (i - 1) * DX;
}
break;
default:
printf("Illegal arrangement of line block\n");
break;
}
break;
case 1: // square
break;
case 2: // T block
cur_block = new->blocks[0];
int possible_x[4] = {cur_block->x - DX, cur_block->x,
cur_block->x + DX, cur_block->x};
int possible_y[4] = {cur_block->y, cur_block->y + DY,
cur_block->y, cur_block->y - DY};
for (int i = 0; i < 3; i++) {
new->blocks[i+1]->x = possible_x[(i+new->arrangement) % 4];
new->blocks[i+1]->y = possible_y[(i+new->arrangement) % 4];
}
break;
case 3: case 4: // Cover both el blocks with ternary.
switch (new->arrangement) {
case 0:
new->blocks[1]->y = cur_y;
new->blocks[1]->x = (new->type == 3) ?
cur_x + DX : cur_x - DX;
new->blocks[2]->y = cur_y - DY;
new->blocks[2]->x = cur_x;
new->blocks[3]->y = cur_y - 2 * DY;
new->blocks[3]->x = cur_x;
break;
case 1:
new->blocks[1]->y = (new->type == 3) ?
cur_y + DY : cur_y - DY;
new->blocks[1]->x = cur_x;
new->blocks[2]->y = cur_y;
new->blocks[2]->x = cur_x + DX;
new->blocks[3]->y = cur_y;
new->blocks[3]->x = cur_x + 2 * DX;
break;
case 2:
new->blocks[1]->y = cur_y;
new->blocks[1]->x = (new->type == 3) ?
cur_x - DX : cur_x + DX;
new->blocks[2]->y = cur_y + DY;
new->blocks[2]->x = cur_x;
new->blocks[3]->y = cur_y + 2 * DY;
new->blocks[3]->x = cur_x;
break;
case 3:
new->blocks[1]->y = (new->type == 3) ?
cur_y - DY : cur_y + DY;
new->blocks[1]->x = cur_x;
new->blocks[2]->y = cur_y;
new->blocks[2]->x = cur_x - DX;
new->blocks[3]->y = cur_y;
new->blocks[3]->x = cur_x - 2 * DX;
break;
default:
printf("Error: Illegal arrangement of el block\n");
break;
}
break;
case 5: case 6: // dog blocks, both. Swap X, then Y, of 1/3.
switch (new->arrangement) {
case 0: case 2:
new->blocks[1]->x = (new->type == 5) ?
cur_x - DX : cur_x + DX;
new->blocks[1]->y = cur_y;
new->blocks[2]->x = cur_x;
new->blocks[2]->y = cur_y - DY;
new->blocks[3]->x = (new->type == 5) ?
cur_x + DX : cur_x - DX;
new->blocks[3]->y = cur_y - DY;
break;
case 1: case 3:
new->blocks[1]->x = cur_x;
new->blocks[1]->y = (new->type == 5) ?
cur_y - DY : cur_y + DY;
new->blocks[2]->x = cur_x + DX;
new->blocks[2]->y = cur_y;
new->blocks[3]->x = cur_x + DX;
new->blocks[3]->y = (current->type == 5) ?
cur_y + DY : cur_y - DY;
break;
default:
printf("Error: illegal dog block arrangement\n");
break;
}
break;
default:
printf("Additional rotation commands TK\n");
break;
}
if (rotation_is_legal(new, board)) {
// Update x and y ranges for current tet.
current->arrangement = new->arrangement;
current->x1 = current->y = 10000; // arbitrarily high/low values to
current->x2 = 0; // guarantee correct updates.
for (int i = 0; i < 4; i++) {
cur_block = current->blocks[i];
cur_block->x = new->blocks[i]->x;
cur_block->y = new->blocks[i]->y;
}
for (int i = 0; i < 4; i++) {
cur_block = current->blocks[i];
current->x1 = (current->x1 < cur_block->x) ?
current->x1 : cur_block-> x;
current->x2 = (current->x2 > cur_block->x) ?
current->x2 : cur_block-> x;
current->y = (current->y < cur_block->y) ?
current->y : cur_block-> y;
}
while (current->x1 < 0) {
for (int i = 0; i < 4; i++) {
current->blocks[i]->x += DX;
}
current->x1 += DX;
}
while (current->x2 > 10 * DX) {
for (int i = 0; i < 4; i++) {
current->blocks[i]->x -= DX;
}
current->x2 -= DX;
}
free(new);
}
}
bool rotation_is_legal(struct Tetromino *new, struct Block *board[21][10]) {
struct Block *cur_block;
for (int i = 0; i < 4; ++i) {
cur_block = new->blocks[i];
int x = cur_block->x / DX;
int y = cur_block->y / DY;
if (board[y][x]) {
return false;
}
}
return true;
}