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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
|
/*******************************************************************************************
*
* raylib - sample game: tetris
*
* Sample game Marc Palau and Ramon Santamaria
*
* This game has been created using raylib v1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
//----------------------------------------------------------------------------------
// Some Defines
//----------------------------------------------------------------------------------
#define SQUARE_SIZE 20
#define GRID_HORIZONTAL_SIZE 12
#define GRID_VERTICAL_SIZE 20
#define LATERAL_SPEED 10
#define TURNING_SPEED 12
#define FAST_FALL_AWAIT_COUNTER 30
#define FADING_TIME 33
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef enum GridSquare { EMPTY, MOVING, FULL, BLOCK, FADING } GridSquare;
//------------------------------------------------------------------------------------
// Global Variables Declaration
//------------------------------------------------------------------------------------
static int screenWidth = 800;
static int screenHeight = 450;
static bool gameOver = false;
static bool pause = false;
// Matrices
static GridSquare grid [GRID_HORIZONTAL_SIZE][GRID_VERTICAL_SIZE];
static GridSquare piece [4][4];
static GridSquare incomingPiece [4][4];
// Theese variables keep track of the active piece position
static int piecePositionX = 0;
static int piecePositionY = 0;
// Game parameters
static Color fadingColor;
//static int fallingSpeed; // In frames
static bool beginPlay = true; // This var is only true at the begining of the game, used for the first matrix creations
static bool pieceActive = false;
static bool detection = false;
static bool lineToDelete = false;
// Statistics
static int level = 1;
static int lines = 0;
// Counters
static int gravityMovementCounter = 0;
static int lateralMovementCounter = 0;
static int turnMovementCounter = 0;
static int fastFallMovementCounter = 0;
static int fadeLineCounter = 0;
// Based on level
static int gravitySpeed = 30;
//------------------------------------------------------------------------------------
// Module Functions Declaration (local)
//------------------------------------------------------------------------------------
static void InitGame(void); // Initialize game
static void UpdateGame(void); // Update game (one frame)
static void DrawGame(void); // Draw game (one frame)
static void UnloadGame(void); // Unload game
static void UpdateDrawFrame(void); // Update and Draw (one frame)
// Additional module functions
static bool Createpiece();
static void GetRandompiece();
static void ResolveFallingMovement();
static bool ResolveLateralMovement();
static bool ResolveTurnMovement();
static void CheckDetection();
static void CheckCompletition();
static void DeleteCompleteLines();
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "sample game: tetris");
InitGame();
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateGame();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
DrawGame();
//----------------------------------------------------------------------------------
}
#endif
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadGame(); // Unload loaded data (textures, sounds, models...)
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//--------------------------------------------------------------------------------------
// Game Module Functions Definition
//--------------------------------------------------------------------------------------
// Initialize game variables
void InitGame(void)
{
// Initialize game statistics
level = 1;
lines = 0;
fadingColor = GRAY;
piecePositionX = 0;
piecePositionY = 0;
pause = false;
beginPlay = true;
pieceActive = false;
detection = false;
lineToDelete = false;
// Counters
gravityMovementCounter = 0;
lateralMovementCounter = 0;
turnMovementCounter = 0;
fastFallMovementCounter = 0;
fadeLineCounter = 0;
gravitySpeed = 30;
// Initialize grid matrices
for (int i = 0; i < GRID_HORIZONTAL_SIZE; i++)
{
for (int j = 0; j < GRID_VERTICAL_SIZE; j++)
{
if ((j == GRID_VERTICAL_SIZE - 1) || (i == 0) || (i == GRID_HORIZONTAL_SIZE - 1)) grid[i][j] = BLOCK;
else grid[i][j] = EMPTY;
}
}
// Initialize incoming piece matrices
for (int i = 0; i < 4; i++)
{
for (int j = 0; j< 4; j++)
{
incomingPiece[i][j] = EMPTY;
}
}
}
// Update game (one frame)
void UpdateGame(void)
{
if (!gameOver)
{
if (IsKeyPressed('P')) pause = !pause;
if (!pause)
{
if (!lineToDelete)
{
if (!pieceActive)
{
// Get another piece
pieceActive = Createpiece();
// We leave a little time before starting the fast falling down
fastFallMovementCounter = 0;
}
else // Piece falling
{
// Counters update
fastFallMovementCounter++;
gravityMovementCounter++;
lateralMovementCounter++;
turnMovementCounter++;
// We make sure to move if we've pressed the key this frame
if (IsKeyPressed(KEY_LEFT) || IsKeyPressed(KEY_RIGHT)) lateralMovementCounter = LATERAL_SPEED;
if (IsKeyPressed(KEY_UP)) turnMovementCounter = TURNING_SPEED;
// Fall down
if (IsKeyDown(KEY_DOWN) && (fastFallMovementCounter >= FAST_FALL_AWAIT_COUNTER))
{
// We make sure the piece is going to fall this frame
gravityMovementCounter += gravitySpeed;
}
if (gravityMovementCounter >= gravitySpeed)
{
// Basic falling movement
CheckDetection(&detection);
// Check if the piece has collided with another piece or with the boundings
ResolveFallingMovement(&detection, &pieceActive);
// Check if we fullfilled a line and if so, erase the line and pull down the the lines above
CheckCompletition(&lineToDelete);
gravityMovementCounter = 0;
}
// Move laterally at player's will
if (lateralMovementCounter >= LATERAL_SPEED)
{
// Update the lateral movement and if success, reset the lateral counter
if (!ResolveLateralMovement()) lateralMovementCounter = 0;
}
// Turn the piece at player's will
if (turnMovementCounter >= TURNING_SPEED)
{
// Update the turning movement and reset the turning counter
if (ResolveTurnMovement()) turnMovementCounter = 0;
}
}
// Game over logic
for (int j = 0; j < 2; j++)
{
for (int i = 1; i < GRID_HORIZONTAL_SIZE - 1; i++)
{
if (grid[i][j] == FULL)
{
gameOver = true;
}
}
}
}
else
{
// Animation when deleting lines
fadeLineCounter++;
if (fadeLineCounter%8 < 4) fadingColor = MAROON;
else fadingColor = GRAY;
if (fadeLineCounter >= FADING_TIME)
{
DeleteCompleteLines();
fadeLineCounter = 0;
lineToDelete = false;
lines++;
}
}
}
}
else
{
if (IsKeyPressed(KEY_ENTER))
{
InitGame();
gameOver = false;
}
}
}
// Draw game (one frame)
void DrawGame(void)
{
BeginDrawing();
ClearBackground(RAYWHITE);
if (!gameOver)
{
// Draw gameplay area
Vector2 offset;
offset.x = screenWidth/2 - (GRID_HORIZONTAL_SIZE*SQUARE_SIZE/2) - 50;
offset.y = screenHeight/2 - ((GRID_VERTICAL_SIZE - 1)*SQUARE_SIZE/2) + SQUARE_SIZE*2;
offset.y -= 50; // NOTE: Harcoded position!
int controller = offset.x;
for (int j = 0; j < GRID_VERTICAL_SIZE; j++)
{
for (int i = 0; i < GRID_HORIZONTAL_SIZE; i++)
{
// Draw each square of the grid
if (grid[i][j] == EMPTY)
{
DrawLine(offset.x, offset.y, offset.x + SQUARE_SIZE, offset.y, LIGHTGRAY );
DrawLine(offset.x, offset.y, offset.x, offset.y + SQUARE_SIZE, LIGHTGRAY );
DrawLine(offset.x + SQUARE_SIZE, offset.y, offset.x + SQUARE_SIZE, offset.y + SQUARE_SIZE, LIGHTGRAY );
DrawLine(offset.x, offset.y + SQUARE_SIZE, offset.x + SQUARE_SIZE, offset.y + SQUARE_SIZE, LIGHTGRAY );
offset.x += SQUARE_SIZE;
}
else if (grid[i][j] == FULL)
{
DrawRectangle(offset.x, offset.y, SQUARE_SIZE, SQUARE_SIZE, GRAY);
offset.x += SQUARE_SIZE;
}
else if (grid[i][j] == MOVING)
{
DrawRectangle(offset.x, offset.y, SQUARE_SIZE, SQUARE_SIZE, DARKGRAY);
offset.x += SQUARE_SIZE;
}
else if (grid[i][j] == BLOCK)
{
DrawRectangle(offset.x, offset.y, SQUARE_SIZE, SQUARE_SIZE, LIGHTGRAY);
offset.x += SQUARE_SIZE;
}
else if (grid[i][j] == FADING)
{
DrawRectangle(offset.x, offset.y, SQUARE_SIZE, SQUARE_SIZE, fadingColor);
offset.x += SQUARE_SIZE;
}
}
offset.x = controller;
offset.y += SQUARE_SIZE;
}
// Draw incoming piece (hardcoded)
offset.x = 500;
offset.y = 45;
int controler = offset.x;
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 4; i++)
{
if (incomingPiece[i][j] == EMPTY)
{
DrawLine(offset.x, offset.y, offset.x + SQUARE_SIZE, offset.y, LIGHTGRAY );
DrawLine(offset.x, offset.y, offset.x, offset.y + SQUARE_SIZE, LIGHTGRAY );
DrawLine(offset.x + SQUARE_SIZE, offset.y, offset.x + SQUARE_SIZE, offset.y + SQUARE_SIZE, LIGHTGRAY );
DrawLine(offset.x, offset.y + SQUARE_SIZE, offset.x + SQUARE_SIZE, offset.y + SQUARE_SIZE, LIGHTGRAY );
offset.x += SQUARE_SIZE;
}
else if (incomingPiece[i][j] == MOVING)
{
DrawRectangle(offset.x, offset.y, SQUARE_SIZE, SQUARE_SIZE, GRAY);
offset.x += SQUARE_SIZE;
}
}
offset.x = controler;
offset.y += SQUARE_SIZE;
}
DrawText("INCOMING:", offset.x, offset.y - 100, 10, GRAY);
DrawText(FormatText("LINES: %04i", lines), offset.x, offset.y + 20, 10, GRAY);
if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
}
else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
EndDrawing();
}
// Unload game variables
void UnloadGame(void)
{
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
}
// Update and Draw (one frame)
void UpdateDrawFrame(void)
{
UpdateGame();
DrawGame();
}
//--------------------------------------------------------------------------------------
// Additional module functions
//--------------------------------------------------------------------------------------
static bool Createpiece()
{
piecePositionX = (int)((GRID_HORIZONTAL_SIZE - 4)/2);
piecePositionY = 0;
// If the game is starting and you are going to create the first piece, we create an extra one
if (beginPlay)
{
GetRandompiece();
beginPlay = false;
}
// We assign the incoming piece to the actual piece
for (int i = 0; i < 4; i++)
{
for (int j = 0; j< 4; j++)
{
piece[i][j] = incomingPiece[i][j];
}
}
// We assign a random piece to the incoming one
GetRandompiece();
// Assign the piece to the grid
for (int i = piecePositionX; i < piecePositionX + 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (piece[i - (int)piecePositionX][j] == MOVING) grid[i][j] = MOVING;
}
}
return true;
}
static void GetRandompiece()
{
srand(time(NULL));
int random = rand() % 7;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
incomingPiece[i][j] = EMPTY;
}
}
switch(random)
{
case 0: { incomingPiece[1][1] = MOVING; incomingPiece[2][1] = MOVING; incomingPiece[1][2] = MOVING; incomingPiece[2][2] = MOVING; } break; //Cube
case 1: { incomingPiece[1][0] = MOVING; incomingPiece[1][1] = MOVING; incomingPiece[1][2] = MOVING; incomingPiece[2][2] = MOVING; } break; //L
case 2: { incomingPiece[1][2] = MOVING; incomingPiece[2][0] = MOVING; incomingPiece[2][1] = MOVING; incomingPiece[2][2] = MOVING; } break; //L inversa
case 3: { incomingPiece[0][1] = MOVING; incomingPiece[1][1] = MOVING; incomingPiece[2][1] = MOVING; incomingPiece[3][1] = MOVING; } break; //Recta
case 4: { incomingPiece[1][0] = MOVING; incomingPiece[1][1] = MOVING; incomingPiece[1][2] = MOVING; incomingPiece[2][1] = MOVING; } break; //Creu tallada
case 5: { incomingPiece[1][1] = MOVING; incomingPiece[2][1] = MOVING; incomingPiece[2][2] = MOVING; incomingPiece[3][2] = MOVING; } break; //S
case 6: { incomingPiece[1][2] = MOVING; incomingPiece[2][2] = MOVING; incomingPiece[2][1] = MOVING; incomingPiece[3][1] = MOVING; } break; //S inversa
}
}
static void ResolveFallingMovement(bool *detection, bool *pieceActive)
{
// If we finished moving this piece, we stop it
if (*detection)
{
for (int j = GRID_VERTICAL_SIZE - 2; j >= 0; j--)
{
for (int i = 1; i < GRID_HORIZONTAL_SIZE - 1; i++)
{
if (grid[i][j] == MOVING)
{
grid[i][j] = FULL;
*detection = false;
*pieceActive = false;
}
}
}
}
// We move down the piece
else
{
for (int j = GRID_VERTICAL_SIZE - 2; j >= 0; j--)
{
for (int i = 1; i < GRID_HORIZONTAL_SIZE - 1; i++)
{
if (grid[i][j] == MOVING)
{
grid[i][j+1] = MOVING;
grid[i][j] = EMPTY;
}
}
}
piecePositionY++;
}
}
static bool ResolveLateralMovement()
{
bool collision = false;
// Move left
if (IsKeyDown(KEY_LEFT))
{
// Check if is possible to move to left
for (int j = GRID_VERTICAL_SIZE - 2; j >= 0; j--)
{
for (int i = 1; i < GRID_HORIZONTAL_SIZE - 1; i++)
{
if (grid[i][j] == MOVING)
{
// Check if we are touching the left wall or we have a full square at the left
if ((i-1 == 0) || (grid[i-1][j] == FULL)) collision = true;
}
}
}
// If able, move left
if (!collision)
{
for (int j = GRID_VERTICAL_SIZE - 2; j >= 0; j--)
{
for (int i = 1; i < GRID_HORIZONTAL_SIZE - 1; i++) // We check the matrix from left to right
{
// Move everything to the left
if (grid[i][j] == MOVING)
{
grid[i-1][j] = MOVING;
grid[i][j] = EMPTY;
}
}
}
piecePositionX--;
}
}
// Move right
else if (IsKeyDown(KEY_RIGHT))
{
// Check if is possible to move to right
for (int j = GRID_VERTICAL_SIZE - 2; j >= 0; j--)
{
for (int i = 1; i < GRID_HORIZONTAL_SIZE - 1; i++)
{
if (grid[i][j] == MOVING)
{
// Check if we are touching the right wall or we have a full square at the right
if ((i+1 == GRID_HORIZONTAL_SIZE - 1) || (grid[i+1][j] == FULL))
{
collision = true;
}
}
}
}
// If able move right
if (!collision)
{
for (int j = GRID_VERTICAL_SIZE - 2; j >= 0; j--)
{
for (int i = GRID_HORIZONTAL_SIZE - 1; i >= 1; i--) // We check the matrix from right to left
{
// Move everything to the right
if (grid[i][j] == MOVING)
{
grid[i+1][j] = MOVING;
grid[i][j] = EMPTY;
}
}
}
piecePositionX++;
}
}
return collision;
}
static bool ResolveTurnMovement()
{
// Input for turning the piece
if (IsKeyDown(KEY_UP))
{
int aux;
bool checker = false;
// Check all turning possibilities
if ((grid[piecePositionX + 3][piecePositionY] == MOVING) &&
(grid[piecePositionX][piecePositionY] != EMPTY) &&
(grid[piecePositionX][piecePositionY] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX + 3][piecePositionY + 3] == MOVING) &&
(grid[piecePositionX + 3][piecePositionY] != EMPTY) &&
(grid[piecePositionX + 3][piecePositionY] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX][piecePositionY + 3] == MOVING) &&
(grid[piecePositionX + 3][piecePositionY + 3] != EMPTY) &&
(grid[piecePositionX + 3][piecePositionY + 3] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX][piecePositionY] == MOVING) &&
(grid[piecePositionX][piecePositionY + 3] != EMPTY) &&
(grid[piecePositionX][piecePositionY + 3] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX + 1][piecePositionY] == MOVING) &&
(grid[piecePositionX][piecePositionY + 2] != EMPTY) &&
(grid[piecePositionX][piecePositionY + 2] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX + 3][piecePositionY + 1] == MOVING) &&
(grid[piecePositionX + 1][piecePositionY] != EMPTY) &&
(grid[piecePositionX + 1][piecePositionY] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX + 2][piecePositionY + 3] == MOVING) &&
(grid[piecePositionX + 3][piecePositionY + 1] != EMPTY) &&
(grid[piecePositionX + 3][piecePositionY + 1] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX][piecePositionY + 2] == MOVING) &&
(grid[piecePositionX + 2][piecePositionY + 3] != EMPTY) &&
(grid[piecePositionX + 2][piecePositionY + 3] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX + 2][piecePositionY] == MOVING) &&
(grid[piecePositionX][piecePositionY + 1] != EMPTY) &&
(grid[piecePositionX][piecePositionY + 1] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX + 3][piecePositionY + 2] == MOVING) &&
(grid[piecePositionX + 2][piecePositionY] != EMPTY) &&
(grid[piecePositionX + 2][piecePositionY] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX + 1][piecePositionY + 3] == MOVING) &&
(grid[piecePositionX + 3][piecePositionY + 2] != EMPTY) &&
(grid[piecePositionX + 3][piecePositionY + 2] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX][piecePositionY + 1] == MOVING) &&
(grid[piecePositionX + 1][piecePositionY + 3] != EMPTY) &&
(grid[piecePositionX + 1][piecePositionY + 3] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX + 1][piecePositionY + 1] == MOVING) &&
(grid[piecePositionX + 1][piecePositionY + 2] != EMPTY) &&
(grid[piecePositionX + 1][piecePositionY + 2] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX + 2][piecePositionY + 1] == MOVING) &&
(grid[piecePositionX + 1][piecePositionY + 1] != EMPTY) &&
(grid[piecePositionX + 1][piecePositionY + 1] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX + 2][piecePositionY + 2] == MOVING) &&
(grid[piecePositionX + 2][piecePositionY + 1] != EMPTY) &&
(grid[piecePositionX + 2][piecePositionY + 1] != MOVING))
{
checker = true;
}
if ((grid[piecePositionX + 1][piecePositionY + 2] == MOVING) &&
(grid[piecePositionX + 2][piecePositionY + 2] != EMPTY) &&
(grid[piecePositionX + 2][piecePositionY + 2] != MOVING))
{
checker = true;
}
if (!checker)
{
aux = piece[0][0];
piece[0][0] = piece[3][0];
piece[3][0] = piece[3][3];
piece[3][3] = piece[0][3];
piece[0][3] = aux;
aux = piece[1][0];
piece[1][0] = piece[3][1];
piece[3][1] = piece[2][3];
piece[2][3] = piece[0][2];
piece[0][2] = aux;
aux = piece[2][0];
piece[2][0] = piece[3][2];
piece[3][2] = piece[1][3];
piece[1][3] = piece[0][1];
piece[0][1] = aux;
aux = piece[1][1];
piece[1][1] = piece[2][1];
piece[2][1] = piece[2][2];
piece[2][2] = piece[1][2];
piece[1][2] = aux;
}
for (int j = GRID_VERTICAL_SIZE - 2; j >= 0; j--)
{
for (int i = 1; i < GRID_HORIZONTAL_SIZE - 1; i++)
{
if (grid[i][j] == MOVING)
{
grid[i][j] = EMPTY;
}
}
}
for (int i = piecePositionX; i < piecePositionX + 4; i++)
{
for (int j = piecePositionY; j < piecePositionY + 4; j++)
{
if (piece[i - piecePositionX][j - piecePositionY] == MOVING)
{
grid[i][j] = MOVING;
}
}
}
return true;
}
return false;
}
static void CheckDetection(bool *detection)
{
for (int j = GRID_VERTICAL_SIZE - 2; j >= 0; j--)
{
for (int i = 1; i < GRID_HORIZONTAL_SIZE - 1; i++)
{
if ((grid[i][j] == MOVING) && ((grid[i][j+1] == FULL) || (grid[i][j+1] == BLOCK))) *detection = true;
}
}
}
static void CheckCompletition(bool *lineToDelete)
{
int calculator;
for (int j = GRID_VERTICAL_SIZE - 2; j >= 0; j--)
{
calculator = 0;
for (int i = 1; i < GRID_HORIZONTAL_SIZE - 1; i++)
{
// Count each square of the line
if (grid[i][j] == FULL)
{
calculator++;
}
// Check if we completed the whole line
if (calculator == GRID_HORIZONTAL_SIZE - 2)
{
*lineToDelete = true;
calculator = 0;
// points++;
// Mark the completed line
for (int z = 1; z < GRID_HORIZONTAL_SIZE - 1; z++)
{
grid[z][j] = FADING;
}
}
}
}
}
static void DeleteCompleteLines()
{
// erase the completed line
for (int j = GRID_VERTICAL_SIZE - 2; j >= 0; j--)
{
while (grid[1][j] == FADING)
{
for (int i = 1; i < GRID_HORIZONTAL_SIZE - 1; i++)
{
grid[i][j] = EMPTY;
}
for (int j2 = j-1; j2 >= 0; j2--)
{
for (int i2 = 1; i2 < GRID_HORIZONTAL_SIZE - 1; i2++)
{
if (grid[i2][j2] == FULL)
{
grid[i2][j2+1] = FULL;
grid[i2][j2] = EMPTY;
}
else if (grid[i2][j2] == FADING)
{
grid[i2][j2+1] = FADING;
grid[i2][j2] = EMPTY;
}
}
}
}
}
}
|