-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
1196 lines (1108 loc) · 31.8 KB
/
main.cpp
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
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <fstream>
bool firstPlay = true;
// Game menu for exit and and play
bool showMenu(sf::RenderWindow& window)
{
firstPlay = false;
// Set up the font for text
sf::Font font;
if (!font.loadFromFile("fonts/menu.ttf"))
{
std::cerr << "Failed to load font!" << std::endl;
return false;
}
// Load the image into a texture
sf::Texture backgroundImage;
if (!backgroundImage.loadFromFile("images/bg1.jpg"))
{
// Handle the case where the image cannot be loaded
return false;
}
// Create a sprite and set its texture to the loaded image
sf::Sprite backgroundSprite(backgroundImage);
// Set the desired size for the background image
float desiredWidth = 1000.0f; // Adjust to your desired width
float desiredHeight = 600.0f; // Adjust to your desired height
// Calculate the scaling factors for width and height
float scaleX = desiredWidth / backgroundSprite.getLocalBounds().width;
float scaleY = desiredHeight / backgroundSprite.getLocalBounds().height;
// Load the sound buffer from a file
sf::SoundBuffer soundBuff;
if (!soundBuff.loadFromFile("sounds/slam.wav"))
{
std::cerr << "Failed to load sound file!" << std::endl;
return false;
}
sf::Sound sound;
sound.setBuffer(soundBuff);
sound.play();
// Set the scale of the background sprite
backgroundSprite.setScale(scaleX, scaleY);
// Set up the text for menu options
sf::Text playText;
sf::Text exitText;
playText.setFont(font);
playText.setCharacterSize(50);
playText.setString("Play");
playText.setPosition(380.0f, 240.0f);
playText.setFillColor(sf::Color::Green);
playText.setOutlineThickness(2.0f); // Border thickness
exitText.setFont(font);
exitText.setCharacterSize(50);
exitText.setString("Exit");
exitText.setPosition(380.0f, 340.0f);
exitText.setFillColor(sf::Color::Red);
exitText.setOutlineThickness(2.0f); // Border thickness
// Game loop for the menu
bool playSelected = true; // Indicates which option is currently selected
playText.setOutlineColor(sf::Color::White);
exitText.setOutlineColor(sf::Color::Red);
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::Up)
{
playSelected = true;
playText.setOutlineColor(sf::Color::White);
exitText.setOutlineColor(sf::Color::Red);
}
else if (event.key.code == sf::Keyboard::Down)
{
playSelected = false;
playText.setOutlineColor(sf::Color::Green);
exitText.setOutlineColor(sf::Color::White);
}
else if (event.key.code == sf::Keyboard::Return)
{
window.close();
return playSelected;
}
}
}
// Clear the window
window.clear();
// Set the background image
window.draw(backgroundSprite);
// Draw menu options
window.draw(playText);
window.draw(exitText);
// Display the content
window.display();
}
return false; // Default to false in case of unexpected closure
}
// Stores images to textures
void loadImages(sf::Texture textures[])
{
for (int i = 0; i <= 20; i++)
{
if (!textures[i].loadFromFile("images/image" + std::to_string(i) + ".png"))
{
std::cout << "Failed to load texture: " << "images/image" + std::to_string(i) + ".png" << std::endl;
return;
}
textures[i].setSmooth(true);// Set the smooth property for the texture
}
}
// Return index of fire gem
int destroyerGem(int index)
{
int returnIndex = 0;
switch (index)
{
case 0:
returnIndex = 14;
break;
case 1:
returnIndex = 15;
break;
case 2:
returnIndex = 16;
break;
case 3:
returnIndex = 17;
break;
case 4:
returnIndex = 18;
break;
case 5:
returnIndex = 19;
break;
case 6:
returnIndex = 20;
break;
default:
break;
}
return returnIndex;
}
// Return index of fire gem
int flameGem(int index)
{
int returnIndex = 0;
switch (index)
{
case 0:
returnIndex = 7;
break;
case 1:
returnIndex = 8;
break;
case 2:
returnIndex = 9;
break;
case 3:
returnIndex = 10;
break;
case 4:
returnIndex = 11;
break;
case 5:
returnIndex = 12;
break;
case 6:
returnIndex = 13;
break;
default:
break;
}
return returnIndex;
}
// Moves highlight based on the cursor key press
void moveHighlight(sf::Keyboard::Key key, int& highlightedRow, int& highlightedCol)
{
switch (key)
{
case sf::Keyboard::Left:
highlightedRow = std::max(0, highlightedRow - 1);
break;
case sf::Keyboard::Right:
highlightedRow = std::min(7, highlightedRow + 1);
break;
case sf::Keyboard::Up:
highlightedCol = std::max(0, highlightedCol - 1);
break;
case sf::Keyboard::Down:
highlightedCol = std::min(7, highlightedCol + 1);
break;
default:
break;
}
}
// Randomizes the initial data for the game board
void randboardData(int boardData[][8])
{
srand(time(0));
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
boardData[i][j] = rand() % 7;
}
}
}
// Draws the game board
void drawBoard(sf::RenderWindow& window, int boardData[][8], sf::Texture textures[], sf::RectangleShape board[][8])
{
static const float cellSize = 62.0f;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
sf::Sprite sprite;
board[i][j].setSize(sf::Vector2f(cellSize, cellSize));
board[i][j].setPosition((i + 7.9) * cellSize, (j + 0.2) * cellSize);
if ((i + j) % 2 == 0)
{
board[i][j].setFillColor(sf::Color(43, 42, 42)); // Grey
}
else
{
board[i][j].setFillColor(sf::Color(0, 0, 0)); // Black
}
sprite.setTexture(textures[boardData[i][j]]); // Set the texture
sprite.setPosition((i + 7.9) * cellSize, (j + 0.2) * cellSize); // Set the position
// Set the scale of the sprites
float scale = cellSize / static_cast<float>(textures[boardData[i][j]].getSize().x);
scale -= 0.07f;
sprite.setScale(scale, scale);
float xOffset = (cellSize - sprite.getGlobalBounds().width) / 2.0f;
float yOffset = (cellSize - sprite.getGlobalBounds().height) / 2.0f;
sprite.setPosition(sprite.getPosition().x + xOffset, sprite.getPosition().y + yOffset);
window.draw(board[i][j]);
window.draw(sprite);
}
}
}
// Handles swapping of data based on keyboard input
void swapData(int boardData[][8], int& highlightedRow, int& highlightedCol, sf::Keyboard::Key key, bool& enterKeyPressed)
{
// Check if the highlighted position is valid
if (highlightedRow >= 0 && highlightedRow < 8 && highlightedCol >= 0 && highlightedCol < 8)
{
// Determine the target position based on the arrow key pressed
int targetRow = highlightedRow;
int targetCol = highlightedCol;
// Check the arrow key pressed and update the target position accordingly
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
targetCol = std::max(0, highlightedCol - 1);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
targetCol = std::min(7, highlightedCol + 1);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
targetRow = std::max(0, highlightedRow - 1);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
targetRow = std::min(7, highlightedRow + 1);
}
// Check if the target position is valid
if (targetRow >= 0 && targetRow < 8 && targetCol >= 0 && targetCol < 8)
{
std::swap(boardData[highlightedRow][highlightedCol], boardData[targetRow][targetCol]);
highlightedRow = targetRow;
highlightedCol = targetCol;
enterKeyPressed = false;
}
}
}
// If no match is found then reverse the swap to get to original position
void reverseSwapData(int boardData[][8], int& highlightedRow, int& highlightedCol, sf::Keyboard::Key key)
{
// Check if the highlighted position is valid
if (highlightedRow >= 0 && highlightedRow < 8 && highlightedCol >= 0 && highlightedCol < 8)
{
// Determine the target position based on the arrow key pressed
int targetRow = highlightedRow;
int targetCol = highlightedCol;
// Check the arrow key pressed and update the target position accordingly
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
targetCol = std::min(7, highlightedCol + 1);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
targetCol = std::max(0, highlightedCol - 1);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
targetRow = std::min(7, highlightedRow + 1);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
targetRow = std::max(0, highlightedRow - 1);
}
// Check if the target position is valid
if (targetRow >= 0 && targetRow < 8 && targetCol >= 0 && targetCol < 8)
{
std::swap(boardData[highlightedRow][highlightedCol], boardData[targetRow][targetCol]);
highlightedRow = targetRow;
highlightedCol = targetCol;
}
}
}
// Shift gems down after s swap to fill empty space
void shiftGemsDown(int board[][8]) {
for (int j = 0; j < 8; j++) {
int count = 0;
for (int i = 8 - 1; i >= 0; i--) {
if (board[i][j] != -1) {
std::swap(board[i][j], board[8 - 1 - count][j]);
count++;
}
}
}
}
// Fills new gems at the top after the shiftGemsDown function has been performed
void fillNewGems(int board[][8]) {
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
if (board[i][j] == -1) {
board[i][j] = rand() % 7;
}
}
}
}
// Destroyer gem check
bool checkElbowGems(int boardData[][8], int row, int col, int highlightedRow, int highlightedCol)
{
int tempValue = boardData[highlightedRow][highlightedCol];
srand(time(0));
if (col + 2 < 8 && row + 2 < 8 &&
boardData[row][col] == boardData[row][col + 1] &&
boardData[row][col] == boardData[row][col + 2] &&
boardData[row][col] == boardData[row + 1][col + 2] &&
boardData[row][col] == boardData[row + 2][col + 2])
{
boardData[row + 2][col + 2] = -1;
boardData[row][col] = -1;
boardData[row][col + 1] = -1;
boardData[row][col + 2] = -1;
boardData[row + 1][col + 2] = -1;
if ((highlightedRow == row || highlightedRow == row + 1 || highlightedRow == row + 2) && (highlightedCol == col || highlightedCol == col + 1 || highlightedCol == col + 2))
{
boardData[highlightedRow][highlightedCol] = destroyerGem(tempValue);
}
else
{
boardData[row][col] = destroyerGem(boardData[row][col]);
}
return true;
}
if (row + 2 < 8 && col + 2 < 8 &&
boardData[row][col] == boardData[row + 1][col] &&
boardData[row][col] == boardData[row + 2][col] &&
boardData[row][col] == boardData[row][col + 1] &&
boardData[row][col] == boardData[row][col + 2])
{
boardData[row][col + 2] = -1;
boardData[row][col] = -1;
boardData[row][col + 1] = -1;
boardData[row + 1][col] = -1;
boardData[row + 2][col] = -1;
if ((highlightedRow == row || highlightedRow == row + 1 || highlightedRow == row + 2) && (highlightedCol == col || highlightedCol == col + 1 || highlightedCol == col + 2))
{
boardData[highlightedRow][highlightedCol] = destroyerGem(tempValue);
}
else
{
boardData[row][col] = destroyerGem(boardData[row][col]);
}
return true;
}
if (row - 2 >= 0 && col + 2 < 8 &&
boardData[row][col] == boardData[row - 1][col] &&
boardData[row][col] == boardData[row - 2][col] &&
boardData[row][col] == boardData[row][col + 1] &&
boardData[row][col] == boardData[row][col + 2])
{
boardData[row][col + 2] = -1;
boardData[row][col] = -1;
boardData[row][col + 1] = -1;
boardData[row - 1][col] = -1;
boardData[row - 2][col] = -1;
if ((highlightedRow == row || highlightedRow == row - 1 || highlightedRow == row - 2) && (highlightedCol == col || highlightedCol == col + 1 || highlightedCol == col + 2))
{
boardData[highlightedRow][highlightedCol] = destroyerGem(tempValue);
}
else
{
boardData[row][col] = destroyerGem(boardData[row][col]);
}
return true;
}
if (row - 2 >= 0 && col - 2 >= 0 &&
boardData[row][col] == boardData[row - 1][col] &&
boardData[row][col] == boardData[row - 2][col] &&
boardData[row][col] == boardData[row][col - 1] &&
boardData[row][col] == boardData[row][col - 2])
{
boardData[row][col] = -1;
boardData[row - 1][col] = -1;
boardData[row - 2][col] = -1;
boardData[row][col - 1] = -1;
boardData[row][col - 2] = -1;
if ((highlightedRow == row || highlightedRow == row - 1 || highlightedRow == row - 2) && (highlightedCol == col || highlightedCol == col - 1 || highlightedCol == col - 2))
{
boardData[highlightedRow][highlightedCol] = destroyerGem(tempValue);
}
else
{
boardData[row][col] = destroyerGem(boardData[row][col]);
}
return true;
}
return false;
}
// Destroyer gem explosion
bool specialGemDestroyer(int boardData[][8])
{
bool sound = false;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
int temp = boardData[i][j];
if (temp >= 14)
{
// Check if it's a destroyer gem
int color = temp % 7; // Get the color of the destroyer gem
// Check in the row for consecutive gems of the same color
bool consecutiveRow = false;
if (j + 1 < 8 && j + 2 < 8) {
if ((boardData[i][j + 1]) % 7 == color && (boardData[i][j + 2]) % 7 == color) {
consecutiveRow = true;
}
if (j - 1 >= 0) {
if ((boardData[i][j - 1]) % 7 == color && (boardData[i][j + 1]) % 7 == color) {
consecutiveRow = true;
}
}
}
if (j - 1 >= 0 && j - 2 >= 0) {
if ((boardData[i][j - 1]) % 7 == color && (boardData[i][j - 2]) % 7 == color) {
consecutiveRow = true;
}
if (j + 1 < 8) {
if ((boardData[i][j - 1]) % 7 == color && (boardData[i][j + 1]) % 7 == color) {
consecutiveRow = true;
}
}
}
// Check in the column for consecutive gems of the same color
bool consecutiveCol = false;
if (i + 1 < 8 && i + 2 < 8) {
if ((boardData[i + 1][j]) % 7 == color && (boardData[i + 2][j]) % 7 == color) {
consecutiveRow = true;
}
if (i - 1 >= 0) {
if ((boardData[i - 1][j]) % 7 == color && (boardData[i + 1][j]) % 7 == color) {
consecutiveRow = true;
}
}
}
if (i - 1 >= 0 && i - 2 >= 0) {
if ((boardData[i - 1][j]) % 7 == color && (boardData[i - 2][j]) % 7 == color) {
consecutiveRow = true;
}
else if (i + 1 < 8) {
if ((boardData[i - 1][j]) % 7 == color && (boardData[i + 1][j]) % 7 == color) {
consecutiveRow = true;
}
}
}
// If consecutive gems found in row or column, update to -1
if (consecutiveRow || consecutiveCol)
{
for (int k = 0; k < 8; k++)
{
boardData[i][k] = -1;
}
for (int k = 0; k < 8; k++)
{
boardData[k][j] = -1;
}
sound = true;
}
}
}
}
shiftGemsDown(boardData);
fillNewGems(boardData);
return sound;
}
// Flame gem explosion
bool specialGemFlame(int boardData[][8])
{
bool sound = false;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
int temp = boardData[i][j];
if (temp >= 7 && temp < 14)
{
// Check if it's a flame gem
int color = temp % 7; // Get the color of the flame gem
// Check in the row for consecutive gems of the same color
bool consecutiveRow = false;
if (j + 1 < 8 && j + 2 < 8) {
if ((boardData[i][j + 1]) % 7 == color && (boardData[i][j + 2]) % 7 == color) {
consecutiveRow = true;
}
if (j - 1 >= 0) {
if ((boardData[i][j - 1]) % 7 == color && (boardData[i][j + 1]) % 7 == color) {
consecutiveRow = true;
}
}
}
if (j - 1 >= 0 && j - 2 >= 0) {
if ((boardData[i][j - 1]) % 7 == color && (boardData[i][j - 2]) % 7 == color) {
consecutiveRow = true;
}
else if (j + 1 < 8) {
if ((boardData[i][j - 1]) % 7 == color && (boardData[i][j + 1]) % 7 == color) {
consecutiveRow = true;
}
}
}
// Check in the column for consecutive gems of the same color
bool consecutiveCol = false;
if (i + 1 < 8 && i + 2 < 8) {
if ((boardData[i + 1][j]) % 7 == color && (boardData[i + 2][j]) % 7 == color) {
consecutiveCol = true;
}
if (i - 1 >= 0) {
if ((boardData[i - 1][j]) % 7 == color && (boardData[i + 1][j]) % 7 == color) {
consecutiveCol = true;
}
}
}
if (i - 1 >= 0 && i - 2 >= 0) {
if ((boardData[i - 1][j]) % 7 == color && (boardData[i - 2][j]) % 7 == color) {
consecutiveCol = true;
}
if (i + 1 < 8) {
if ((boardData[i - 1][j]) % 7 == color && (boardData[i + 1][j]) % 7 == color) {
consecutiveCol = true;
}
}
}
// If consecutive gems found in row or column, update to -1
if (consecutiveRow || consecutiveCol)
{
if (i - 1 >= 0 && j - 1 >= 0) {
boardData[i - 1][j - 1] = -1;
}
if (j - 1 >= 0) {
boardData[i][j - 1] = -1;
}
if (i + 1 < 8 && j - 1 >= 0) {
boardData[i + 1][j - 1] = -1;
}
if (i - 1 >= 0) {
boardData[i - 1][j] = -1;
}
boardData[i][j] = -1;
if (i + 1 < 8) {
boardData[i + 1][j] = -1;
}
if (i - 1 >= 0 && j + 1 < 8) {
boardData[i - 1][j + 1] = -1;
}
if (j + 1 < 8) {
boardData[i][j + 1] = -1;
}
if (i + 1 < 8 && j + 1 < 8) {
boardData[i + 1][j + 1] = -1;
}
sound = true;
}
}
}
}
shiftGemsDown(boardData);
fillNewGems(boardData);
return sound;
}
// Checks Elbow gem and plays the sound
bool elbowGems(int boardData[][8], int& score, int highlightedRow, int highlightedCol)
{
bool soundPlay = false;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
bool flag = checkElbowGems(boardData, i, j, highlightedRow, highlightedCol);
if (flag)
{
soundPlay = true;
score += 50;
}
}
}
if (soundPlay)
{
shiftGemsDown(boardData);
fillNewGems(boardData);
}
return soundPlay;
}
// Checks for matches on the game board and updates data
bool checkBoard(int boardData[][8], int& score, int highlightedRow, int highlightedCol, bool& flameFound)
{
srand(time(0));
bool flag = false;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (boardData[i][j] == boardData[i + 1][j] && boardData[i][j] == boardData[i + 2][j]) // Row Check
{
flag = true;
score += 15;
int count = 3;
int tempValue = boardData[highlightedRow][highlightedCol];
//Check if there are 3 or more same elements in a column
for (int k = i + 3; k < 8; k++)
{
if (boardData[i][j] == boardData[k][j])
{
boardData[k][j] = -1;
score += 5;
count++;
}
else
{
break;
}
}
boardData[i][j] = -1;
boardData[i + 1][j] = -1;
boardData[i + 2][j] = -1;
if (count > 3)
{
if ((highlightedRow == i || highlightedRow == i + 1 || highlightedRow == i + 2) && highlightedCol == j)
{
boardData[highlightedRow][highlightedCol] = flameGem(tempValue);
flameFound = true;
}
else
{
boardData[i][j] = flameGem(boardData[i][j]);
flameFound = true;
}
}
}
if (boardData[i][j] == boardData[i][j + 1] && boardData[i][j] == boardData[i][j + 2]) // Column Check
{
flag = true;
//Check if there are 3 or more same elements in a row
score += 15;
int count = 3;
int tempValue = boardData[highlightedRow][highlightedCol];
for (int k = j + 3; k < 8; k++)
{
if (boardData[i][j] == boardData[i][k])
{
boardData[i][k] = -1;
score += 5;
count++;
}
else
{
break;
}
}
boardData[i][j] = -1;
boardData[i][j + 1] = -1;
boardData[i][j + 2] = -1;
if (count > 3)
{
if ((highlightedCol == j || highlightedCol == j + 1 || highlightedCol == j + 2) && highlightedRow == i)
{
boardData[highlightedRow][highlightedCol] = flameGem(tempValue);
flameFound = true;
}
else
{
boardData[i][j] = flameGem(boardData[i][j]);
flameFound = true;
}
}
}
}
}
if (flag)
{
shiftGemsDown(boardData);
fillNewGems(boardData);
}
if (flameFound)
{
score += 20;
}
return flag;
}
// Game over menu to exit or restart game
bool showGameOverMenu(sf::RenderWindow& window, int score) {
int filescore = 0;
std::ifstream file("file.txt");
file >> filescore;
file.close();
std::ofstream readFile("file.txt");
if (score > filescore) { filescore = score; }
readFile << filescore;
readFile.close();
sf::Font font;
if (!font.loadFromFile("fonts/menu.ttf")) {
std::cerr << "Failed to load font!" << std::endl;
return false;
}
// Load the image into a texture
sf::Texture backgroundImage;
if (!backgroundImage.loadFromFile("images/bg2.jpg"))
{
// Handle the case where the image cannot be loaded
return false;
}
// Create a sprite and set its texture to the loaded image
sf::Sprite backgroundSprite(backgroundImage);
// Set the desired size for the background image
float desiredWidth = 1000.0f; // Adjust to your desired width
float desiredHeight = 600.0f; // Adjust to your desired height
// Calculate the scaling factors for width and height
float scaleX = desiredWidth / backgroundSprite.getLocalBounds().width;
float scaleY = desiredHeight / backgroundSprite.getLocalBounds().height;
backgroundSprite.setScale(scaleX, scaleY);
sf::Text highscore;
highscore.setFont(font);
highscore.setCharacterSize(40);
highscore.setFillColor(sf::Color::White);
highscore.setString("Highscore: " + std::to_string(filescore));
highscore.setPosition(200.0f, 120.0f);
sf::Text scoreText;
scoreText.setFont(font);
scoreText.setCharacterSize(40);
scoreText.setFillColor(sf::Color::White);
scoreText.setString("Score: " + std::to_string(score));
scoreText.setPosition(300.0f, 220.0f);
sf::Text restartText;
restartText.setFont(font);
restartText.setCharacterSize(40);
restartText.setFillColor(sf::Color::Green);
restartText.setString("Restart Game");
restartText.setPosition(250.0f, 320.0f);
restartText.setOutlineThickness(2.0f);
sf::Text exitText;
exitText.setFont(font);
exitText.setCharacterSize(40);
exitText.setFillColor(sf::Color::Red);
exitText.setString("Exit");
exitText.setPosition(400.0f, 420.0f);
exitText.setOutlineThickness(2.0f);
bool selected = true;
restartText.setOutlineColor(sf::Color::White);
exitText.setOutlineColor(sf::Color::Red);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
return false;
}
else if (event.type == sf::Event::KeyPressed) {
// Move selector with arrow keys
if (event.key.code == sf::Keyboard::Up) {
restartText.setOutlineColor(sf::Color::White);
exitText.setOutlineColor(sf::Color::Red);
selected = true;
}
else if (event.key.code == sf::Keyboard::Down) {
restartText.setOutlineColor(sf::Color::Green);
exitText.setOutlineColor(sf::Color::White);
selected = false;
}
else if (event.key.code == sf::Keyboard::Return) {
// Check if the cursor is over "Restart Game" or "Exit"
window.close();
return selected;
}
}
}
// Clear the window
window.clear();
// Draw menu options
window.draw(backgroundSprite);
window.draw(highscore);
window.draw(scoreText);
window.draw(restartText);
window.draw(exitText);
// Display the content
window.display();
}
return false; // Default to false in case of unexpected closure
}
int main()
{
// Load the icon image
sf::Image icon;
if (!icon.loadFromFile("images/icon.jpg"))
{
std::cout << "Failed to load icon.png";
return -1;
}
if (firstPlay) {
sf::RenderWindow menuWindow(sf::VideoMode(1000, 600), "Bejeweled Blitz", sf::Style::Close);
menuWindow.setFramerateLimit(60);
menuWindow.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
bool play = showMenu(menuWindow);
if (!play)
{
return 0;
}
}
static sf::RectangleShape board[8][8];
static sf::Texture textures[21];
int highlightedRow = 0;
int highlightedCol = 0;
int boardData[8][8]{ 0 }; // Create a 2D array to store the board data
randboardData(boardData); // Randomize the board data
// Set up the window
sf::RenderWindow window(sf::VideoMode(1000, 600), "Bejeweled Blitz", sf::Style::Close);
window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
window.setFramerateLimit(60);
// Set up the font for text
sf::Font font;
if (!font.loadFromFile("fonts/font.ttf"))
{
std::cerr << "Failed to load font!" << std::endl;
return -1;
}
// Set up the bold font for title
sf::Font fontBold;
if (!fontBold.loadFromFile("fonts/fontBold.otf"))
{
std::cerr << "Failed to load bold font!" << std::endl;
return -1;
}
// Set up the text for time, score, title and info
sf::Text timeText;
sf::Text scoreText;
sf::Text shuffleText;
sf::Text gameTitle;
sf::Text gameInfo;
gameTitle.setFont(fontBold);
gameTitle.setCharacterSize(36);
gameTitle.setFillColor(sf::Color::White);
timeText.setFont(font);
timeText.setCharacterSize(35);
timeText.setFillColor(sf::Color::White);
shuffleText.setFont(font);
shuffleText.setCharacterSize(25);
shuffleText.setFillColor(sf::Color::White);
scoreText.setFont(font);
scoreText.setCharacterSize(35);
scoreText.setFillColor(sf::Color::White);
gameInfo.setFont(font);
gameInfo.setCharacterSize(25);
gameInfo.setFillColor(sf::Color::White);
// Set up the board
const float cellSize = 62.0f;
loadImages(textures);
// Set up the clock
sf::Clock clock;
// Initialize the score
int score = 0;
// Declare a boolean flag to track Enter key status
bool enterKeyPressed = false;
// Load the sound buffer from a file
sf::SoundBuffer soundEffect1;
if (!soundEffect1.loadFromFile("sounds/select.wav"))
{
std::cerr << "Failed to load sound file!" << std::endl;
return -1;
}
sf::SoundBuffer soundEffect2;
if (!soundEffect2.loadFromFile("sounds/sequence1.wav"))
{
std::cerr << "Failed to load sound file!" << std::endl;
return -1;
}
sf::SoundBuffer soundEffect3;
if (!soundEffect3.loadFromFile("sounds/flame.wav"))
{
std::cerr << "Failed to load sound file!" << std::endl;
return -1;
}
sf::SoundBuffer soundEffect4;
if (!soundEffect4.loadFromFile("sounds/destroyer.wav"))
{
std::cerr << "Failed to load sound file!" << std::endl;
return -1;
}
sf::SoundBuffer destroyer;
if (!destroyer.loadFromFile("sounds/destroyerExplodes.wav"))
{
std::cerr << "Failed to load sound file!" << std::endl;
return -1;
}
sf::SoundBuffer flameSound;
if (!flameSound.loadFromFile("sounds/flameExplodes.wav"))
{
std::cerr << "Failed to load sound file!" << std::endl;
return -1;