-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
961 lines (884 loc) · 22.4 KB
/
Copy pathmain.cpp
File metadata and controls
961 lines (884 loc) · 22.4 KB
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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
#include <iostream>
#include <random>
#include <thread>
#include <chrono>
#include <SFML/Graphics.hpp>
constexpr int cellLength = 40;
constexpr int lineWidth = 1;
constexpr int numColumns = 10;
constexpr int numRows = 20;
constexpr int padding = 80;
constexpr int windowX = 2*padding + numColumns*cellLength;
constexpr int windowY = 2*padding + numRows*cellLength;
//Structure representing a (x, y) coordinate pair/
struct position
{
int x, y;
};
//Cells used to compose a cell map making up the game board
//Can be filled or unfilled with an arbitrary color
class cell
{
private:
position p;
bool isFilled = false;
sf::RectangleShape lines[4];
sf::RectangleShape fill;
public:
cell(int x, int y);
void configLines();
void configFill(sf::Color fillColor);
void setIsFilled(bool set);
bool returnIsFilled();
sf::RectangleShape returnLine(int d);
sf::RectangleShape returnFill();
};
//Consturctor initializing the cells position in the cell map and calling the function that configures the cell's borders
cell::cell(int x, int y)
{
p.x = x;
p.y = y;
configLines();
}
//Configures the cell's borders into SFML rectangles
void cell::configLines()
{
for (int i = 0; i < 4; i++)
{
lines[i].setSize(sf::Vector2f(cellLength, lineWidth));
lines[i].setFillColor(sf::Color::White);
if (i == 0 || i == 1)
{
lines[i].setPosition((p.x * cellLength + padding), (p.y * cellLength + padding));
}
if (i == 1 || i == 3)
{
lines[i].setRotation(90.f);
}
}
lines[2].setPosition((p.x * cellLength + padding), ((p.y + 1) * cellLength + padding));
lines[3].setPosition(((p.x + 1) * cellLength + padding), (p.y * cellLength + padding));
}
//Configures the cell's fill into a SFML rectangle of arbitrary length
void cell::configFill (sf::Color fillColor)
{
fill.setSize(sf::Vector2f(cellLength, cellLength));
fill.setFillColor(fillColor);
fill.setPosition((p.x * cellLength + padding), (p.y * cellLength + padding));
}
//Returns the SFML rectangle of the cell's line at direction d
//0 is up, 1 is left, 2 is down, 3 is right
sf::RectangleShape cell::returnLine(int d)
{
return lines[d];
}
//Returns the SFML rectanlge of the cell's fill
sf::RectangleShape cell::returnFill()
{
return fill;
}
//Sets whether or not the fill should be displayed
void cell::setIsFilled(bool set)
{
isFilled = set;
}
//Returns whether or not the fill should eb displayed
bool cell::returnIsFilled()
{
return isFilled;
}
//Blocks that represent each filled cell on the map
class block
{
private:
position p;
//Default color of white
sf::Color blockColor = sf::Color::White;
public:
void move(int dir);
//Set position must be called
void setPosition(int x, int y);
position returnPosition();
void setColor(sf::Color color);
sf::Color returnColor();
};
//Function to move a block in an arbitrary direction
//0 is up, 1 is left, 2 is down, 3 is right
void block::move(int dir)
{
if (dir == 0)
{
p.y--;
}
else if (dir == 1)
{
p.x--;
}
else if (dir == 2)
{
p.y++;
}
else if (dir == 3)
{
p.x++;
}
}
//Sets a blocks position to an arbitrary point
void block::setPosition(int x, int y)
{
p.x = x;
p.y = y;
}
//Returns a block's position object
position block::returnPosition()
{
return p;
}
//Sets a blocks color to an arbitrary value
void block::setColor(sf::Color color)
{
blockColor = color;
}
//Returns a block's color
sf::Color block::returnColor()
{
return blockColor;
}
//Tetrominos are groups of blocks
class tetromino
{
private:
// Block list making up ithe tetromino
std::vector<block> blockList;
position p;
/* Shape IDs
0: I
1: O
2: T
3: J
4: L
5: S
6: Z */
int shape;
/* Rotation values
0: 0 degrees
1: 90 degrees
2: 180 degrees
3: 270 degrees */
int rotation;
public:
tetromino(int setShape);
/* Move directions
0: Up
1: Left
2: Down
3: Right */
void move(int direction);
position returnPosition();
/* Rotate directions
-1: Counter-clockwise
1: Clockwise */
void rotate(int direction);
std::vector<block> decompose(std::vector<block> setBlockList);
void configBlockList();
std::vector<block> returnBlockList();
};
//Constructor setting the shape ID, the x and y coordinates, and the default blockList values
tetromino::tetromino(int setShape)
{
shape = setShape;
p.x = numColumns/2;
p.y = 0;
rotation = 0;
configBlockList();
}
//Move a whole tetromino in an arbitrary direction
//0: Up, 1: Left, 2: Down, 3: Right
void tetromino::move(int direction)
{
try
{
if (direction < 0 || direction > 3)
{
throw std::runtime_error("Move direction out of bounds");
}
if (direction == 0)
{
p.y--;
}
else if (direction == 1)
{
p.x--;
}
else if (direction == 2)
{
p.y++;
}
else
{
p.x++;
}
}
catch(std::exception const &e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
}
//Return the position of the tetromino
position tetromino::returnPosition()
{
return p;
}
//Rotate a whole tetromino in an arbitrary direction
//-1: Counter-clockwise, 1: Clockwise
void tetromino::rotate(int direction)
{
try
{
if (direction != -1 && direction != 1)
{
throw std::runtime_error("Rotation direction out of bounds");
}
rotation += direction;
rotation %= 4;
}
catch(std::exception const &e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
}
//Returns an augmented block list where the give tetromino has decomposed to the blocks that make it up
std::vector<block> tetromino::decompose(std::vector<block> setBlockList)
{
for (int i = 0; i < blockList.size(); i++)
{
block tempBlock = blockList[i];
tempBlock.setPosition(blockList[i].returnPosition().x + p.x, blockList[i].returnPosition().y + p.y);
setBlockList.push_back(tempBlock);
}
return setBlockList;
}
//Configures the whole block list of this tetromino
void tetromino::configBlockList()
{
/* Tetromino Shapes:
0 - I
1 - O
2 - T
3 - J
4 - L
5 - S
6 - Z */
/* Rotation IDs:
0 - 0 degrees
1 - 90 degrees
2 - 180 degrees
3 - 270 degrees */
//Four blocks for each tetromino
block blocks[4];
//I
if (shape == 0)
{
if (rotation == 0)
{
blocks[0].setPosition(-1, 0);
blocks[1].setPosition( 0, 0);
blocks[2].setPosition( 1, 0);
blocks[3].setPosition( 2, 0);
}
else if (rotation == 1)
{
blocks[0].setPosition( 0, -1);
blocks[1].setPosition( 0, 0);
blocks[2].setPosition( 0, 1);
blocks[3].setPosition( 0, 2);
}
else if (rotation == 2)
{
blocks[0].setPosition(-2, 0);
blocks[1].setPosition(-1, 0);
blocks[2].setPosition( 0, 0);
blocks[3].setPosition( 1, 0);
}
else if (rotation == 3)
{
blocks[0].setPosition( 0, -2);
blocks[1].setPosition( 0, -1);
blocks[2].setPosition( 0, 0);
blocks[3].setPosition( 0, 1);
}
blocks[0].setColor(sf::Color::Cyan);
blocks[1].setColor(sf::Color::Cyan);
blocks[2].setColor(sf::Color::Cyan);
blocks[3].setColor(sf::Color::Cyan);
}
//O
else if (shape == 1)
{
if (rotation == 0)
{
blocks[0].setPosition( 0, 0);
blocks[1].setPosition( 1, 0);
blocks[2].setPosition( 0, 1);
blocks[3].setPosition( 1, 1);
}
else if (rotation == 1)
{
blocks[0].setPosition(-1, 0);
blocks[1].setPosition( 0, 0);
blocks[2].setPosition(-1, 1);
blocks[3].setPosition( 0, 1);
}
else if (rotation == 2)
{
blocks[0].setPosition(-1, -1);
blocks[1].setPosition( 0, -1);
blocks[2].setPosition(-1, 0);
blocks[3].setPosition( 0, 0);
}
else if (rotation == 3)
{
blocks[0].setPosition( 0, -1);
blocks[1].setPosition( 1, -1);
blocks[2].setPosition( 0, 0);
blocks[3].setPosition( 1, 0);
}
blocks[0].setColor(sf::Color::Yellow);
blocks[1].setColor(sf::Color::Yellow);
blocks[2].setColor(sf::Color::Yellow);
blocks[3].setColor(sf::Color::Yellow);
}
//T
else if (shape == 2)
{
if (rotation == 0)
{
blocks[0].setPosition( 0, -1);
blocks[1].setPosition( 0, 0);
blocks[2].setPosition( 1, 0);
blocks[3].setPosition( 0, 1);
}
else if (rotation == 1)
{
blocks[0].setPosition(-1, 0);
blocks[1].setPosition( 0, 0);
blocks[2].setPosition( 1, 0);
blocks[3].setPosition( 0, 1);
}
else if (rotation == 2)
{
blocks[0].setPosition( 0, -1);
blocks[1].setPosition( 0, 0);
blocks[2].setPosition(-1, 0);
blocks[3].setPosition( 0, 1);
}
else if (rotation == 3)
{
blocks[0].setPosition( 0, -1);
blocks[1].setPosition(-1, 0);
blocks[2].setPosition( 0, 0);
blocks[3].setPosition( 1, 0);
}
blocks[0].setColor(sf::Color::Magenta);
blocks[1].setColor(sf::Color::Magenta);
blocks[2].setColor(sf::Color::Magenta);
blocks[3].setColor(sf::Color::Magenta);
}
//J
else if (shape == 3)
{
if (rotation == 0)
{
blocks[0].setPosition( 0, -1);
blocks[1].setPosition( 0, 0);
blocks[2].setPosition( 0, 1);
blocks[3].setPosition(-1, 1);
}
else if (rotation == 1)
{
blocks[0].setPosition(-1, -1);
blocks[1].setPosition(-1, 0);
blocks[2].setPosition( 0, 0);
blocks[3].setPosition( 1, 0);
}
else if (rotation == 2)
{
blocks[0].setPosition( 0, -1);
blocks[1].setPosition( 1, -1);
blocks[2].setPosition( 0, 0);
blocks[3].setPosition( 0, 1);
}
else if (rotation == 3)
{
blocks[0].setPosition(-1, 0);
blocks[1].setPosition( 0, 0);
blocks[2].setPosition( 1, 0);
blocks[3].setPosition( 1, 1);
}
blocks[0].setColor(sf::Color::Blue);
blocks[1].setColor(sf::Color::Blue);
blocks[2].setColor(sf::Color::Blue);
blocks[3].setColor(sf::Color::Blue);
}
//L
else if (shape == 4)
{
if (rotation == 0)
{
blocks[0].setPosition( 0, -1);
blocks[1].setPosition( 0, 0);
blocks[2].setPosition( 0, 1);
blocks[3].setPosition( 1, 1);
}
else if (rotation == 1)
{
blocks[0].setPosition(-1, 0);
blocks[1].setPosition( 0, 0);
blocks[2].setPosition( 1, 0);
blocks[3].setPosition(-1, 1);
}
else if (rotation == 2)
{
blocks[0].setPosition(-1, -1);
blocks[1].setPosition( 0, -1);
blocks[2].setPosition( 0, 0);
blocks[3].setPosition( 0, 1);
}
else if (rotation == 3)
{
blocks[0].setPosition( 1, -1);
blocks[1].setPosition(-1, 0);
blocks[2].setPosition( 0, 0);
blocks[3].setPosition( 1, 0);
}
blocks[0].setColor(sf::Color::White);
blocks[1].setColor(sf::Color::White);
blocks[2].setColor(sf::Color::White);
blocks[3].setColor(sf::Color::White);
}
//S
else if (shape == 5)
{
if (rotation == 0)
{
blocks[0].setPosition( 0, -1);
blocks[1].setPosition( 0, 0);
blocks[2].setPosition( 1, 0);
blocks[3].setPosition( 1, 1);
}
else if (rotation == 1)
{
blocks[0].setPosition( 0, 0);
blocks[1].setPosition( 1, 0);
blocks[2].setPosition(-1, 1);
blocks[3].setPosition( 0, 1);
}
else if (rotation == 2)
{
blocks[0].setPosition(-1, -1);
blocks[1].setPosition(-1, 0);
blocks[2].setPosition( 0, 0);
blocks[3].setPosition( 0, 1);
}
else if (rotation == 3)
{
blocks[0].setPosition( 0, -1);
blocks[1].setPosition( 1, -1);
blocks[2].setPosition(-1, 0);
blocks[3].setPosition( 0, 0);
}
blocks[0].setColor(sf::Color::Green);
blocks[1].setColor(sf::Color::Green);
blocks[2].setColor(sf::Color::Green);
blocks[3].setColor(sf::Color::Green);
}
//Z
else if (shape == 6)
{
if (rotation == 0)
{
blocks[0].setPosition( 1, -1);
blocks[1].setPosition( 0, 0);
blocks[2].setPosition( 1, 0);
blocks[3].setPosition( 0, 1);
}
else if (rotation == 1)
{
blocks[0].setPosition(-1, 0);
blocks[1].setPosition( 0, 0);
blocks[2].setPosition( 0, 1);
blocks[3].setPosition( 1, 1);
}
else if (rotation == 2)
{
blocks[0].setPosition( 0, -1);
blocks[1].setPosition(-1, 0);
blocks[2].setPosition( 0, 0);
blocks[3].setPosition(-1, 1);
}
else if (rotation == 3)
{
blocks[0].setPosition(-1, -1);
blocks[1].setPosition( 0, -1);
blocks[2].setPosition( 0, 0);
blocks[3].setPosition( 1, 0);
}
blocks[0].setColor(sf::Color::Red);
blocks[1].setColor(sf::Color::Red);
blocks[2].setColor(sf::Color::Red);
blocks[3].setColor(sf::Color::Red);
}
if (blockList.size() == 0)
{
blockList.push_back(blocks[0]);
blockList.push_back(blocks[1]);
blockList.push_back(blocks[2]);
blockList.push_back(blocks[3]);
}
else if (blockList.size() == 4)
{
blockList[0] = blocks[0];
blockList[1] = blocks[1];
blockList[2] = blocks[2];
blockList[3] = blocks[3];
}
}
//Return the whole block list of this tetromino
std::vector<block> tetromino::returnBlockList()
{
return blockList;
}
//Returns whether or not the active tetromino can move in a specified direction
//0: Up, 1: Left, 2: Down, 3: Right
bool canMove(std::vector<tetromino> tetrominoList, std::vector<block> blockList, int direction)
{
tetromino activeTet = tetrominoList[0];
if (direction == 0)
{
//TODO eventually; currently a piece will never go up
return false;
}
else if (direction == 1)
{
//Check border collision
for (int i = 0; i < activeTet.returnBlockList().size(); i++)
{
if (activeTet.returnBlockList()[i].returnPosition().x + activeTet.returnPosition().x <= 0)
{
return false;
}
}
//Check tetromino collision
for (int i = 0; i < blockList.size(); i++)
{
for (int j = 0; j < activeTet.returnBlockList().size(); j++)
{
if ((activeTet.returnBlockList()[j].returnPosition().x + activeTet.returnPosition().x - 1 == blockList[i].returnPosition().x) && (activeTet.returnBlockList()[j].returnPosition().y + activeTet.returnPosition().y == blockList[i].returnPosition().y))
{
return false;
}
}
}
//Otherwise
return true;
}
else if (direction == 2)
{
//Check border collision
for (int i = 0; i < activeTet.returnBlockList().size(); i++)
{
if (activeTet.returnBlockList()[i].returnPosition().y + activeTet.returnPosition().y >= numRows - 1)
{
return false;
}
}
//Check tetromino collision
for (int i = 0; i < blockList.size(); i++)
{
for (int j = 0; j < activeTet.returnBlockList().size(); j++)
{
if ((activeTet.returnBlockList()[j].returnPosition().y + activeTet.returnPosition().y + 1 == blockList[i].returnPosition().y) && (activeTet.returnBlockList()[j].returnPosition().x + activeTet.returnPosition().x == blockList[i].returnPosition().x))
{
return false;
}
}
}
//Otherwise
return true;
}
else if (direction == 3)
{
//Check border collision
for (int i = 0; i < activeTet.returnBlockList().size(); i++)
{
if (activeTet.returnBlockList()[i].returnPosition().x + activeTet.returnPosition().x >= numColumns - 1)
{
return false;
}
}
//Check tetromino collision
for (int i = 0; i < blockList.size(); i++)
{
for (int j = 0; j < activeTet.returnBlockList().size(); j++)
{
if ((activeTet.returnBlockList()[j].returnPosition().x + activeTet.returnPosition().x + 1 == blockList[i].returnPosition().x) && (activeTet.returnBlockList()[j].returnPosition().y + activeTet.returnPosition().y == blockList[i].returnPosition().y))
{
return false;
}
}
}
//Otherwise
return true;
}
//Called when an invalid direction is passed
else
{
return false;
}
}
//Returns whether or not the active tetromino can rotate in a specified direction
//1: Clockwise, -1: Counter-clockwise
bool canRotate(std::vector<tetromino> tetrominoList, std::vector<block> blockList, int direction)
{
tetromino activeTet = tetrominoList[tetrominoList.size() - 1];
activeTet.rotate(direction);
for (int i = 0; i < blockList.size(); i++)
{
for (int j = 0; j < activeTet.returnBlockList().size(); j++)
{
//If the rotated block crosses a border
if (activeTet.returnBlockList()[j].returnPosition().x + activeTet.returnPosition().x < 0 || activeTet.returnBlockList()[j].returnPosition().x + activeTet.returnPosition().x >= numColumns || activeTet.returnBlockList()[j].returnPosition().y + activeTet.returnPosition().y < 0 || activeTet.returnBlockList()[j].returnPosition().y + activeTet.returnPosition().y >= numRows)
{
return false;
}
//If the rotated block overlaps with any tetromino, return false
if (activeTet.returnBlockList()[j].returnPosition().x + activeTet.returnPosition().x == blockList[i].returnPosition().x && activeTet.returnBlockList()[j].returnPosition().y + activeTet.returnPosition().y == blockList[i].returnPosition().y)
{
return false;
}
}
}
//Otherwise
return true;
}
//Returns whether or not the given row has been filled and is ready to clear
bool isRowComplete(std::vector<block> blockList, int row)
{
bool columnFilled[numColumns];
bool isComplete = true;
//tetrominoList.size()-1 so the active piece isn't included
for (int i = 0; i < blockList.size(); i++)
{
if (blockList[i].returnPosition().y == row)
{
columnFilled[blockList[i].returnPosition().x] = true;
}
}
for (int i = 0; i < numColumns; i++)
{
if (!columnFilled[i])
{
isComplete = false;
}
}
return isComplete;
}
//Returns a new tetromino list in which a given row has been cleared
std::vector<block> clearRow(std::vector<block> blockList, int row)
{
for (int i = 0; i < blockList.size(); i++)
{
if (blockList[i].returnPosition().y == row)
{
blockList.erase(blockList.begin() + i);
}
//Moves lines down
if (blockList[i].returnPosition().y < row)
{
blockList[i].move(2);
}
}
return blockList;
}
int main()
{
sf::RenderWindow window(sf::VideoMode(windowX, windowY), "Tetris Clone");
std::vector<std::vector<cell>> cellMap;
std::vector<block> blockListActive;
std::vector<block> decomposedTetrominos;
std::vector<tetromino> tetrominoList;
bool isPlaying = true;
int score = 0;
//Random number stuff
std::random_device rd;
std::default_random_engine generator;
generator.seed(rd());
std::uniform_int_distribution<int> randomPiece(0, 6);
//Populates cell map
for (int i = 0; i < numColumns; i++)
{
cellMap.push_back(std::vector<cell>());
for (int j = 0; j < numRows; j++)
{
cell newCell(i, j);
cellMap[i].push_back(newCell);
}
}
//Tetromino list with first random tetromino pushed
tetromino firstTet(randomPiece(generator));
tetrominoList.push_back(firstTet);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
else if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Left && isPlaying && canMove(tetrominoList, decomposedTetrominos, 1))
{
tetrominoList[tetrominoList.size() - 1].move(1);
}
else if (event.key.code == sf::Keyboard::Right && isPlaying && canMove(tetrominoList,decomposedTetrominos, 3))
{
tetrominoList[tetrominoList.size() - 1].move(3);
}
else if (event.key.code == sf::Keyboard::Up && isPlaying && canRotate(tetrominoList,decomposedTetrominos, 1))
{
tetrominoList[tetrominoList.size() - 1].rotate(1);
}
else if (event.key.code == sf::Keyboard::Down && isPlaying && canMove(tetrominoList,decomposedTetrominos, 2))
{
tetrominoList[tetrominoList.size() - 1].move(2);
}
else if (event.key.code == sf::Keyboard::Space)
{
if (isPlaying)
{
isPlaying = false;
}
else
{
isPlaying = true;
}
}
}
}
window.clear();
if (isPlaying)
{
//Add tetromino list to displayed block list
for (int i = 0; i < tetrominoList.size(); i++)
{
tetrominoList[i].configBlockList();
for (int j = 0; j < tetrominoList[i].returnBlockList().size(); j++)
{
block tempBlock;
tempBlock.setPosition(tetrominoList[i].returnBlockList()[j].returnPosition().x + tetrominoList[i].returnPosition().x, tetrominoList[i].returnBlockList()[j].returnPosition().y + tetrominoList[i].returnPosition().y);
tempBlock.setColor(tetrominoList[i].returnBlockList()[j].returnColor());
blockListActive.push_back(tempBlock);
}
}
//Add decomposed tetrominos block list to displayed block list
for (int i = 0; i < decomposedTetrominos.size(); i++)
{
blockListActive.push_back(decomposedTetrominos[i]);
}
//Set cells that correspond to each block in the displayed block list active
for (int i = 0; i < blockListActive.size(); i++)
{
if (blockListActive[i].returnPosition().x > -1 && blockListActive[i].returnPosition().x < numColumns && blockListActive[i].returnPosition().y > -1 && blockListActive[i].returnPosition().y < numRows)
{
cellMap[blockListActive[i].returnPosition().x][blockListActive[i].returnPosition().y].configFill(blockListActive[i].returnColor());
cellMap[blockListActive[i].returnPosition().x][blockListActive[i].returnPosition().y].setIsFilled(true);
}
}
//Move down each tick if it is possible
if (canMove(tetrominoList, decomposedTetrominos, 2))
{
tetrominoList[tetrominoList.size() - 1].move(2);
}
//Else spawn a new tetromino and decompose the previous tetromino
else
{
decomposedTetrominos = tetrominoList[0].decompose(decomposedTetrominos);
tetrominoList.pop_back();
tetromino newTet(randomPiece(generator));
tetrominoList.push_back(newTet);
}
//Loss checking
for (int i = 0; i < decomposedTetrominos.size(); i++)
{
if (decomposedTetrominos[i].returnPosition().x == numColumns / 2 && decomposedTetrominos[i].returnPosition().y == 0)
{
score = 0;
decomposedTetrominos.clear();
}
}
//Line checking
std::vector<int> linesCleared;
for (int i = 0; i < numRows; i++)
{
if (isRowComplete(decomposedTetrominos, i))
{
linesCleared.push_back(i);
}
}
if (linesCleared.size() == 0)
{
//This will be true for the majority of the game so I am including it as the first of this if else-if change to save processing time
}
else if (linesCleared.size() == 1)
{
score += 40;
}
else if (linesCleared.size() == 2)
{
score += 100;
}
else if (linesCleared.size() == 3)
{
score += 300;
}
else if (linesCleared.size() == 4)
{
score += 1200;
}
while (linesCleared.size() > 0)
{
decomposedTetrominos = clearRow(decomposedTetrominos, linesCleared[0]);
linesCleared.erase(linesCleared.begin());
}
//Draw filled cells
for (int i = 0; i < numColumns; i++)
{
for (int j = 0; j < numRows; j++)
{
for (int k = 0; k < 4; k++)
{
window.draw(cellMap[i][j].returnLine(k));
}
if (cellMap[i][j].returnIsFilled())
{
window.draw(cellMap[i][j].returnFill());
}
}
}
std::cout << score << std::endl;
}
window.display();
for (int i = 0; i < numColumns; i++)
{
for (int j = 0; j < numRows; j++)
{
cellMap[i][j].setIsFilled(false);
}
}
blockListActive.clear();
//Tick timer
std::this_thread::sleep_for(std::chrono::milliseconds(150));
}
}