This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
1695 lines (1474 loc) · 45.8 KB
/
Source.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 "SBDL.h"
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <cmath>
#define BOMB '*'
#define WONGAME_RATIO 100.0
#define NOBOOMB_RATIO 10.0
const int FPS = 100;
const int delay = 1000 / FPS;
//initializing of some variable that are used across several functions
int const width = 640, height = 704;
int game_state = 0;//0 in main menu - 1 in play and so on...
int user_pos_x, user_pos_y;//mouse positon
int current_save_slot, all_slots;//the core of saving and loading algorithm
int const element_size = 32;//size of board elements
int input;//for load_menu --- it is the slot number
bool load_board = false;//when a board is deleted shifts to true to prevent from building a new board
int x[3], i;//used for keypressing in custom menu
//used for keypressing in change_user and change_name menus
char temp_username[50] = "@";
int k=1;
bool hint = true;//used in change_user menu
SDL_Rect** rects;//for the board
//the definition of almost all of the textures and their corresponding rects
Texture background;
SDL_Rect background_R = { 0 , 0 ,672, 672 };
Texture new_game;
SDL_Rect new_game_R = { 245 , 0 , 150, 50 };
Texture load;
SDL_Rect load_R = { 245 , 50 , 150, 50 };
Texture change_name;
SDL_Rect change_name_R = { 245 , 150 , 150, 50 };
Texture leaderboard;
SDL_Rect leaderboard_R = { 245,250,150, 50 };
Texture end_game;
SDL_Rect end_game_R = { 245 , 100 , 150, 50 };
Texture change_user;
SDL_Rect change_user_R = { 245 , 200 , 150, 50 };
Texture difficulty;
SDL_Rect difficulty_R = { 0 , 0 , 150 , 50 };
Texture beginner;
SDL_Rect beginner_R = { 150 , 0 , 150 , 50 };
Texture intermediate;
SDL_Rect intermediate_R = { 150 , 50 , 150 , 50 };
Texture expert;
SDL_Rect expert_R = { 150 , 100 , 150 , 50 };
Texture custom;
SDL_Rect custom_R = { 0 , 150, 150 , 50 };
Texture back;
SDL_Rect back_newgame_menu_R = { 0 , 200, 150 , 50 };
SDL_Rect back_ingame_R = { 414 , 640, 150 , 64 };
SDL_Rect back_custom_menu_R = { 0,200 , 150, 50 };
SDL_Rect back_leaderboard_menu_R = { 255 ,500 ,150 ,50 };
Texture remaining_flags;
SDL_Rect remaining_flags_R = { 285 , 645 ,32 ,44 };
SDL_Rect flag_icon_R = { 325 , 650 , 32 , 32 };
//custom part
Texture rows;
SDL_Rect rows_R = { 0 , 0 , 150, 50 };
Texture columns;
SDL_Rect columns_R = { 0 , 50 ,150 , 50 };
Texture mines;
SDL_Rect mines_R = { 0 , 100, 150 ,50 };
Texture custom_0;//row
Texture custom_1;//col
Texture custom_2;//mine
SDL_Rect input_row_R = { 160 , 0 , 32 , 44 };
SDL_Rect input_columns_R = { 160 , 50 , 32 , 44 };
SDL_Rect input_mines_R = { 160 , 100 , 32 , 44 };
//
Texture play_again;
SDL_Rect play_again_R = { 444 , 640 ,150 , 64 };
Texture score;
SDL_Rect score_R = { 30, 640 , 150 ,64 };
Texture score_number;
SDL_Rect score_number_R = { 180 , 645 , 32 ,44 };
Texture menu;
SDL_Rect menu_aftergame_R = { 240, 640 , 150 ,64 };
//load menu
Texture save_slot1;
Texture save_slot2;
SDL_Rect save_slot1_R = { 130 , 0 , 225 , 75 };
SDL_Rect save_slot2_R = { 355 , 0 , 150 , 75 };
Texture input_slot;
SDL_Rect input_slot_R = { 300, 75 , 32 ,44 };
SDL_Rect back_load_menu_R = { 255 ,400 ,150 ,50 };
//
Texture save_game;
SDL_Rect save_game_R = { 245, 100, 150 ,50 };
Texture quit_game;
SDL_Rect quit_game_R = { 245 , 150 , 150 , 50 };
Texture board_number;
SDL_Rect board_number_R = { 60 , 640 ,150 ,64 };
Texture number;
SDL_Rect number_R = { 210 , 645 , 32 , 44 };
Texture username_menu_background;
SDL_Rect username_menu_background_R = { 0 , 0 ,640 , 704 };
Texture username_input;
SDL_Rect username_input_R = { 120 , 570 ,400, 100 };
Texture type_username;
SDL_Rect type_username_R = { 170, 620 , 300 , 40 };
Texture display_username;
SDL_Rect display_username_R = { 170 , 620 , 300 , 70 };
Texture changename_menu_input;
SDL_Rect changename_menu_input_R = { 170 , 0 , 300 , 50 };
SDL_Rect changename_menu_type_R = { 170 , 150, 300 , 70 };
//leaderboard
SDL_Rect rank_1_R = { 130 , 5 , 40 , 50 };
SDL_Rect rank_2_R = { 130 , 55 , 40 , 50 };
SDL_Rect rank_3_R = { 130 , 105 , 40 , 50 };
SDL_Rect rank_4_R = { 130 , 155 , 40 , 50 };
SDL_Rect rank_5_R = { 130 , 205 , 40 , 50 };
SDL_Rect first_R = { 170 , 0 , 250 , 50 };
SDL_Rect second_R = { 170 , 50 , 250 , 50 };
SDL_Rect third_R = { 170 , 100 , 250 , 50 };
SDL_Rect fourth_R = { 170 , 150 , 250 , 50 };
SDL_Rect fifth_R = { 170 , 200 , 250 , 50 };
SDL_Rect first_score_R = { 420 , 0 ,100 , 50 };
SDL_Rect second_score_R = { 420 , 50 , 100 , 50 };
SDL_Rect third_score_R = { 420 , 100 , 100 , 50 };
SDL_Rect fourth_score_R = { 420 , 150 , 100 , 50 };
SDL_Rect fifth_score_R = { 420 , 200 , 100 ,50 };
//
//definition of ingame pictures
Texture bomb;
Texture one;
Texture two;
Texture three;
Texture four;
Texture five;
Texture six;
Texture seven;
Texture eight;
Texture zero;
Texture uncovered;
Texture flag;
Texture wrong_flag;
Texture pressed_bomb;
//definition of sounds
Sound* error;
Sound* explosion;
Sound* victory;
Sound* switch_user;
Sound* change_name_done;
Sound* save_button;
//definition of fonts
Font* font;
Font* font_small;
Font* font_big;
struct board
{
int row;
int col;
int bomb;
int save_slot;//finding the board in list with this number
char** element;//the actual positining of bombs
int** is_clicked;//0 not clicked --- 1 is clicked (show the real value) --- 2 is clicked show flag --- 3 is wrong flag --- 4 is pressed bomb
int flag_counter;
bool deleted;//when this is true the board can be used for initializng a new game
double board_ratio;//for calculating scores
board* next_board;
};
board* head_board;//for implementing the linked list
struct user
{
char user_name[50];
double score;
board* user_headboard;//each user finds it's path to their particular head_board with this pointer.
//this pointer stores the address to that head_board -main core of saving the users board-
int headboard_tag;//for headboard
int user_current_save_slot;//the following 3 variables are for saving the game state when a another user is made
int user_all_slots;
bool user_load_board = false;
user* next_user;
};
user* head_user;//for implementing linked list
user* current_user;//used for changing name and applying score and... the program which user is currently logged in with this variable
//particulary for loading assets
void load_assets()
{
//background = SBDL::loadTexture("pics/Minesweeper.png");
username_menu_background = SBDL::loadTexture("assets/pics/defuser.png");
username_input = SBDL::loadTexture("assets/pics/input.png");
new_game = SBDL::loadTexture("assets/pics/newgame.png");
load = SBDL::loadTexture("assets/pics/loadgame.png");
leaderboard = SBDL::loadTexture("assets/pics/leaderboard.png");
change_name = SBDL::loadTexture("assets/pics/changename.png");
end_game = SBDL::loadTexture("assets/pics/exitgame.png");
save_slot1 = SBDL::loadTexture("assets/pics/entersaveslot.png");
save_slot2 = SBDL::loadTexture("assets/pics/number.png");
difficulty = SBDL::loadTexture("assets/pics/choosedifficulty.png");
beginner = SBDL::loadTexture("assets/pics/beginner.png");
intermediate = SBDL::loadTexture("assets/pics/intermediate.png");
expert = SBDL::loadTexture("assets/pics/expert.png");
custom = SBDL::loadTexture("assets/pics/custom.png");
back = SBDL::loadTexture("assets/pics/back.png");
rows = SBDL::loadTexture("assets/pics/rows.png");
columns = SBDL::loadTexture("assets/pics/columns.png");
mines = SBDL::loadTexture("assets/pics/mines.png");
bomb = SBDL::loadTexture("assets/pics/ingame/bomb.png");
one = SBDL::loadTexture("assets/pics/ingame/one.png");
two = SBDL::loadTexture("assets/pics/ingame/two.png");
three = SBDL::loadTexture("assets/pics/ingame/three.png");
four = SBDL::loadTexture("assets/pics/ingame/four.png");
five = SBDL::loadTexture("assets/pics/ingame/five.png");
six = SBDL::loadTexture("assets/pics/ingame/six.png");
seven = SBDL::loadTexture("assets/pics/ingame/seven.png");
eight = SBDL::loadTexture("assets/pics/ingame/eight.png");
zero = SBDL::loadTexture("assets/pics/ingame/zero.png");
uncovered = SBDL::loadTexture("assets/pics/ingame/uncovered.png");
flag = SBDL::loadTexture("assets/pics/ingame/flag.png");
wrong_flag = SBDL::loadTexture("assets/pics/ingame/wrongflag.png");
pressed_bomb = SBDL::loadTexture("assets/pics/ingame/pressedbomb.png");
play_again = SBDL::loadTexture("assets/pics/playagain.png");
score = SBDL::loadTexture("assets/pics/score.png");
save_game = SBDL::loadTexture("assets/pics/save.png");
quit_game = SBDL::loadTexture("assets/pics/quit.png");
board_number = SBDL::loadTexture("assets/pics/board.png");
menu = SBDL::loadTexture("assets/pics/menu.png");
change_user = SBDL::loadTexture("assets/pics/changeuser.png");
error = SBDL::loadSound("assets/music/error.wav");
explosion = SBDL::loadSound("assets/music/explosion.wav");
victory = SBDL::loadSound("assets/music/victory.wav");
switch_user = SBDL::loadSound("assets/music/switch.wav");
change_name_done = SBDL::loadSound("assets/music/changename.wav");
save_button = SBDL::loadSound("assets/music/savebutton.wav");
font = SBDL::loadFont("assets/music/Roboto-Medium.ttf", 25);
font_small = SBDL::loadFont("assets/music/times.ttf", 300);
font_big = SBDL::loadFont("assets/music/changenamefont.ttf", 300);
changename_menu_input = SBDL::createFontTexture(font_big, "ENTER NEW NAME", 0, 0, 0);
}
board** headboards;//is used to make an VLA array for storing address to headboards
void add_headboard(user* x)//a VLA array of pointers to headboard(which are board* themseleves)
{//x is the new made user
if (x->headboard_tag == 1)
{
headboards = new board* [x->headboard_tag];//is 1 for the frist time
x->user_headboard = headboards[x->headboard_tag - 1];
x->user_headboard = NULL;//the headboard of first user is now NULL
return;
}
board** new_headboards = new board * [x->headboard_tag];//added
for (int i = 0; i < x->headboard_tag - 1; i++)
{
new_headboards[i] = headboards[i];
}
x->user_headboard = new_headboards[x->headboard_tag - 1];//assigned
x->user_headboard = NULL;
headboards = new_headboards;//new_headboards variable is used as a transporter
delete[] new_headboards;//preventing memory leak
}
user* last_user_finder()
{
int tag = 0;
user* last = head_user;
while (last->next_user != NULL)
last = last->next_user;
return last;
}
void add_user()//adding a new user to our linked list
{
user** ptr_head = &head_user;
user* new_user = new user;
new_user->user_current_save_slot = 0;
new_user->user_all_slots = 0;
new_user->user_load_board = false;
new_user->score = 0.0;
new_user->next_user = NULL;
if (*ptr_head == NULL)//the first user
{
new_user->headboard_tag = 1;
add_headboard(new_user);
*ptr_head = new_user;
}
else
{
user* last = last_user_finder();
new_user->headboard_tag = (last->headboard_tag) + 1;
add_headboard(new_user);
last->next_user = new_user;
}
}
int lentgh(char str[])
{
int i = 0;
while (str[i] != '\0')
i++;
return i;
}
bool compare(char a[], char b[])//compare function considering @ character
{
for (int i = 1; a[i] != '\0'; i++)
{
if (a[i] != b[i - 1])
return false;
}
return true;
}
bool is_already_taken(char x[], int size)
{
user* temp = head_user;
while (temp != NULL)
{
if (lentgh(x) != lentgh(temp->user_name) + 1)
{
temp = temp->next_user;
continue;
}
if (compare(x, temp->user_name))
{
current_user = temp;
return true;
}
temp = temp->next_user;
}
return false;
}
void user_name_menu()
{
game_state = 5;
if (hint)
type_username = SBDL::createFontTexture(font_small, "ex:pouria82", 0, 0, 0);
else
type_username = SBDL::createFontTexture(font_small, temp_username, 0, 0, 0);
SBDL::showTexture(username_menu_background, username_menu_background_R);
SBDL::showTexture(username_input, username_input_R);
SBDL::showTexture(type_username, type_username_R);
if (SBDL::keyPressed(SDL_SCANCODE_A)) {
temp_username[k] = 'a'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_B)) {
temp_username[k] = 'b'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_C)) {
temp_username[k] = 'c'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_D)) {
temp_username[k] = 'd'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_E)) {
temp_username[k] = 'e'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_F)) {
temp_username[k] = 'f'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_G)) {
temp_username[k] = 'g'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_H)) {
temp_username[k] = 'h'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_I)) {
temp_username[k] = 'i'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_J)) {
temp_username[k] = 'j'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_K)) {
temp_username[k] = 'k'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_L)) {
temp_username[k] = 'l'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_M)) {
temp_username[k] = 'm'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_N)) {
temp_username[k] = 'n'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_O)) {
temp_username[k] = 'o'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_P)) {
temp_username[k] = 'p'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_Q)) {
temp_username[k] = 'q'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_R)) {
temp_username[k] = 'r'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_S)) {
temp_username[k] = 's'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_T)) {
temp_username[k] = 't'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_U)) {
temp_username[k] = 'u'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_V)) {
temp_username[k] = 'v'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_W)) {
temp_username[k] = 'w'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_X)) {
temp_username[k] = 'x'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_Y)) {
temp_username[k] = 'y'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_Z)) {
temp_username[k] = 'z'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_0)) {
temp_username[k] = '0'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_1)) {
temp_username[k] = '1'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_2)) {
temp_username[k] = '2'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_3)) {
temp_username[k] = '3'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_4)) {
temp_username[k] = '4'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_5)) {
temp_username[k] = '5'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_6)) {
temp_username[k] = '6'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_7)) {
temp_username[k] = '7'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_8)) {
temp_username[k] = '8'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_9)) {
temp_username[k] = '9'; k++; hint = false;
}else if (SBDL::keyPressed(SDL_SCANCODE_BACKSPACE)) {
if (lentgh(temp_username) == 1)
return;
k--; temp_username[k] = '\0';
}
if (SBDL::keyPressed(SDL_SCANCODE_RETURN))
{
if (is_already_taken(temp_username, k))//switch to this user
{
SBDL::playSound(switch_user, 1);
current_save_slot = current_user->user_current_save_slot;//saving game state...
all_slots = current_user->user_all_slots;
load_board = current_user->user_load_board;
for (int i = 1; i < k; i++)
{
temp_username[i] = '\0';
}
temp_username[0] = '@';//reinitializing temp_username for next times
k = 1;
game_state = 0;
return;
}
//if we reached here we are definitly making a new user
add_user();
user* x = last_user_finder();//automatically will return the new user which was made in previous line
for (int i = 1; i < k; i++) {
x->user_name[i - 1] = temp_username[i];
}
for (int i = 1; i < k; i++)
{
temp_username[i] = '\0';
}
temp_username[0] = '@';//reinitializing temp_username for next times
x->user_name[k-1] = '\0';
game_state = 0;
k = 1;
current_user = x;
}
SBDL::freeTexture(type_username);
}
void change_name_menu()
{
game_state = 15;
type_username = SBDL::createFontTexture(font_small, temp_username, 0, 0, 0);
SBDL::showTexture(changename_menu_input, changename_menu_input_R);
SBDL::showTexture(back, back_leaderboard_menu_R);
SBDL::showTexture(type_username, changename_menu_type_R);
if (SBDL::Mouse.clicked(SDL_BUTTON_LEFT, 1, SDL_RELEASED) && SBDL::pointInRect(user_pos_x, user_pos_y, back_leaderboard_menu_R))
game_state = 0;
if (SBDL::keyPressed(SDL_SCANCODE_A)) {
temp_username[k] = 'a'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_B)) {
temp_username[k] = 'b'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_C)) {
temp_username[k] = 'c'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_D)) {
temp_username[k] = 'd'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_E)) {
temp_username[k] = 'e'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_F)) {
temp_username[k] = 'f'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_G)) {
temp_username[k] = 'g'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_H)) {
temp_username[k] = 'h'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_I)) {
temp_username[k] = 'i'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_J)) {
temp_username[k] = 'j'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_K)) {
temp_username[k] = 'k'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_L)) {
temp_username[k] = 'l'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_M)) {
temp_username[k] = 'm'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_N)) {
temp_username[k] = 'n'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_O)) {
temp_username[k] = 'o'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_P)) {
temp_username[k] = 'p'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_Q)) {
temp_username[k] = 'q'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_R)) {
temp_username[k] = 'r'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_S)) {
temp_username[k] = 's'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_T)) {
temp_username[k] = 't'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_U)) {
temp_username[k] = 'u'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_V)) {
temp_username[k] = 'v'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_W)) {
temp_username[k] = 'w'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_X)) {
temp_username[k] = 'x'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_Y)) {
temp_username[k] = 'y'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_Z)) {
temp_username[k] = 'z'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_0)) {
temp_username[k] = '0'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_1)) {
temp_username[k] = '1'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_2)) {
temp_username[k] = '2'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_3)) {
temp_username[k] = '3'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_4)) {
temp_username[k] = '4'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_5)) {
temp_username[k] = '5'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_6)) {
temp_username[k] = '6'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_7)) {
temp_username[k] = '7'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_8)) {
temp_username[k] = '8'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_9)) {
temp_username[k] = '9'; k++;
}else if (SBDL::keyPressed(SDL_SCANCODE_BACKSPACE)) {
if (lentgh(temp_username) == 1)
return;
k--; temp_username[k] = '\0';
}
if (SBDL::keyPressed(SDL_SCANCODE_RETURN))
{
if (is_already_taken(temp_username, k))
{
SBDL::playSound(error, 1);
for (int i = 1; i < k; i++)
{
temp_username[i] = '\0';
}
temp_username[0] = '@';
k = 1;
return;
}
else
{
for (int i = 1; i < k; i++) {
current_user->user_name[i - 1] = temp_username[i];
}
for (int i = 1; i < k; i++)
{
temp_username[i] = '\0';
}
temp_username[0] = '@';
current_user->user_name[k - 1] = '\0';
game_state = 0;
k = 1;
SBDL::playSound(change_name_done, 1);
}
}
SBDL::freeTexture(type_username);
}
//in this function we will count all the users first then a VLA is made and all the useres are stored in it
//after we will sort it by score and put the top five in our refrenced array
void leaderboard_calc(user* top_five[],int size,int &number_of_users)
{
user* temp = head_user;
while (temp != NULL)
{
number_of_users++;
temp = temp->next_user;
}
int idx = 0;
user** ranking = new user * [number_of_users];
ranking[idx] = head_user;
for (int i = 1; i < number_of_users; i++)
{
ranking[i] = ranking[i - 1]->next_user;
}
//now we'll sort it
for (int i = 0; i < number_of_users - 1; i++)
for (int j = 0; j < number_of_users - 1 - i; j++)
if (ranking[j + 1]->score > ranking[j]->score)
std::swap(ranking[j + 1], ranking[j]);
for (int j = 0; j < size; j++)
{
top_five[j] = ranking[j];
if (j == number_of_users - 1)
break;
}
delete[] ranking;
}
void leaderboard_menu()
{
int number_of_users = 0;
Texture top_five_username[5];
Texture top_five_score[5];
for (int i = 0; i < 5; i++)
top_five_username[i] = SBDL::createFontTexture(font_small,"@username",0,0,0);
for (int i = 0; i < 5; i++)
top_five_score[i] = SBDL::createFontTexture(font_small, "#score", 0, 0, 0);
user* top_five[5]{};
leaderboard_calc(top_five,5,number_of_users);
for (int i = 0; i < number_of_users; i++)
SBDL::freeTexture(top_five_username[i]);
for (int i = 0; i < number_of_users; i++)
SBDL::freeTexture(top_five_score[i]);
for (int i = 0; i < 5; i++)
{
top_five_username[i] = SBDL::createFontTexture(font_small, top_five[i]->user_name, 0, 0, 0);
top_five_score[i] = SBDL::createFontTexture(font_small,"#" + std::to_string((int)round(top_five[i]->score)), 7, 73,117);
if (i == number_of_users - 1)
break;
}
Texture rank_1 = SBDL::createFontTexture(font, "1", 207, 158, 0);
Texture rank_2 = SBDL::createFontTexture(font, "2", 176, 176, 176);
Texture rank_3 = SBDL::createFontTexture(font, "3",176 ,141 ,87);
Texture rank_4 = SBDL::createFontTexture(font, "4", 0, 0, 0);
Texture rank_5 = SBDL::createFontTexture(font, "5", 0, 0, 0);
SBDL::showTexture(rank_1, rank_1_R);
SBDL::showTexture(rank_2, rank_2_R);
SBDL::showTexture(rank_3, rank_3_R);
SBDL::showTexture(rank_4, rank_4_R);
SBDL::showTexture(rank_5, rank_5_R);
SBDL::showTexture(top_five_username[0], first_R);
SBDL::showTexture(top_five_username[1], second_R);
SBDL::showTexture(top_five_username[2], third_R);
SBDL::showTexture(top_five_username[3], fourth_R);
SBDL::showTexture(top_five_username[4], fifth_R);
SBDL::showTexture(top_five_score[0], first_score_R);
SBDL::showTexture(top_five_score[1], second_score_R);
SBDL::showTexture(top_five_score[2], third_score_R);
SBDL::showTexture(top_five_score[3], fourth_score_R);
SBDL::showTexture(top_five_score[4], fifth_score_R);
SBDL::showTexture(back, back_leaderboard_menu_R);
if (SBDL::Mouse.clicked(SDL_BUTTON_LEFT, 1, SDL_RELEASED) && SBDL::pointInRect(user_pos_x, user_pos_y, back_leaderboard_menu_R))
game_state = 0;
SBDL::freeTexture(rank_1);
SBDL::freeTexture(rank_2);
SBDL::freeTexture(rank_3);
SBDL::freeTexture(rank_4);
SBDL::freeTexture(rank_5);
for (int i = 0; i < 5; i++)
SBDL::freeTexture(top_five_username[i]);
for (int i = 0; i < 5; i++)
SBDL::freeTexture(top_five_score[i]);
number_of_users = 0;
}
board* slot_finder(int slot)//recives a slot number and will find the corrsponding board
{
board* result = current_user->user_headboard;
while (result != NULL)
{
if (slot == result->save_slot)
return result;
result = result->next_board;
}
}
void show_main_menu()
{
display_username = SBDL::createFontTexture(font_small, current_user->user_name, 0, 0, 0);
SBDL::showTexture(new_game, new_game_R);
SBDL::showTexture(load,load_R);
SBDL::showTexture(end_game, end_game_R);
SBDL::showTexture(change_name, change_name_R);
SBDL::showTexture(leaderboard,leaderboard_R);
SBDL::showTexture(change_user, change_user_R);
SBDL::showTexture(display_username, display_username_R);
//SBDL::showTexture(seven, background_1_R);
if (SBDL::Mouse.clicked(SDL_BUTTON_LEFT, 1, SDL_RELEASED) && SBDL::pointInRect(user_pos_x, user_pos_y, new_game_R))
{
game_state = 11;
}
if (SBDL::Mouse.clicked(SDL_BUTTON_LEFT, 1, SDL_RELEASED) && SBDL::pointInRect(user_pos_x, user_pos_y, load_R))
{
if (all_slots == 0)//no current board is loaded
SBDL::playSound(error, 1);
else
game_state = 13;
}
if (SBDL::Mouse.clicked(SDL_BUTTON_LEFT, 1, SDL_RELEASED) && SBDL::pointInRect(user_pos_x, user_pos_y, end_game_R))
{
game_state = 14;
}
if (SBDL::Mouse.clicked(SDL_BUTTON_LEFT, 1, SDL_RELEASED) && SBDL::pointInRect(user_pos_x, user_pos_y, change_name_R))
{
game_state = 15;
}
if (SBDL::Mouse.clicked(SDL_BUTTON_LEFT, 1, SDL_RELEASED) && SBDL::pointInRect(user_pos_x, user_pos_y, leaderboard_R))
{
game_state = 16;
}
if (SBDL::Mouse.clicked(SDL_BUTTON_LEFT, 1, SDL_RELEASED) && SBDL::pointInRect(user_pos_x, user_pos_y, change_user_R))
{
//if we type a username which is not already taken a new user will be made otherwise if it's already taken, the program will
//switch between these two users
current_user->user_current_save_slot = current_save_slot;//saving the game state
current_user->user_all_slots = all_slots;
current_user->user_load_board = load_board;
current_save_slot = 0;
all_slots = 0;
load_board = false;
game_state = 5;
}
SBDL::freeTexture(display_username);
}
bool is_in_board(int i, int j,int row,int col)
{
if (i >= 0 && i < row && j >= 0 && j < col)
return true;
else
return false;
}
int adjacent_bombs(int i, int j,int row,int col,char** board_)
{
int bomb_num = 0;
for (int k = i - 1; k <= i + 1; k++)
for (int l = j - 1; l <= j + 1; l++)
if (is_in_board(k, l,row,col) && board_[k][l] == BOMB)
bomb_num++;
return bomb_num;
}
void init_board(board* new_board)
{
int row = new_board->row;
int col = new_board->col;
int bomb = new_board->bomb;
new_board->deleted = false;
new_board->element = new char*[row];
new_board->is_clicked = new int* [row];
for (int i = 0; i < row; i++)//will be deleted deleted in delete_game function
{
new_board->element[i] = new char[col];
new_board->is_clicked[i] = new int[col];
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
new_board->element[i][j] = '0';//initialize with 0 for the moment
new_board->is_clicked[i][j] = false;
}
}
//placing bombs
bool** is_bomb = new bool* [row];
for (int i = 0; i < row; i++)
is_bomb [i] = new bool[col];
int B_row = 0, B_col = 0;
int i = 0;
while(i < bomb)
{
B_row = (rand() % row), B_col = (rand() % col);
if (new_board->element[B_row][B_col]!= BOMB)
{
new_board->element[B_row][B_col] = BOMB;
i++;
}
else
continue;
}
//setting the number of adjacent bombs in none-bomb positions
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
if (new_board->element[i][j] != BOMB)
new_board->element[i][j] = adjacent_bombs(i, j,row,col,new_board->element) + '0';//setting the number
for (int i = 0; i < row; i++)
delete[] is_bomb[i];
delete[] is_bomb;
}
board* last_board_finder()//returns the adress of the last board in list
{
board* last = current_user->user_headboard;
while (last->next_board != NULL)
last = last->next_board;
return last;
}
//in the following function we already know the board's saveslot number
void add_game(int difficulty_level,int save_slot)//receives a number to generate a new board with a particular difficulty
{
board** ptr_head;
board* new_board;
if (!load_board)//should we build a new board or not?
{//here we should
ptr_head = ¤t_user->user_headboard;
new_board = new board;
new_board->next_board = NULL;
if (*ptr_head == NULL)//list is empty
{
*ptr_head = new_board;
}
else
{
board* last = last_board_finder();
last->next_board = new_board;
}
}
else
{
new_board = slot_finder(current_save_slot);
load_board = false;
}
switch (difficulty_level)
{
case 1:
new_board->row = 5;
new_board->col = 5;
new_board->bomb = 4;
new_board->flag_counter = 0;
new_board->save_slot = save_slot;
new_board->board_ratio = 4.0 / (5.0 * 5.0);
break;
case 2:
new_board->row = 12;
new_board->col = 12;
new_board->bomb = 28;
new_board->flag_counter = 0;
new_board->save_slot = save_slot;
new_board->board_ratio = 28.0 / (12.0 * 12.0);
break;
case 3:
new_board->row = 20;
new_board->col = 20;
new_board->flag_counter = 0;
new_board->bomb = 96;
new_board->save_slot = save_slot;
new_board->board_ratio = 96.0 / (20.0 * 20.0);
break;
case 4:
new_board->row = x[0];
new_board->col = x[1];
new_board->flag_counter = 0;
new_board->bomb = x[2];
new_board->save_slot = save_slot;
new_board->board_ratio = (double)x[2] / (x[0] * x[1]);//explicit type conversion(by assignment)
x[0] = 0;
x[1] = 0;
x[2] = 0;
i = 0;
}
std::cout << new_board->board_ratio<<std::endl;
init_board(new_board);
game_state = 1;//show time!
}
//once a board is deleted every thing in it is deleted EXEPCT the address of the board itself ---
// new boards will be built when we have no empty boards in our list otherwise the new board adress will be the FIRST EMPTY board that had been created
int empty_board_finder()//finds if between CURRENT boards we have an empty one with the help of deleted function if not returns -1
{
board* temp = current_user->user_headboard;
int check = 0;
while (temp != NULL)
{
if (temp->deleted)//empty board found(could be the last board)
{
return temp->save_slot;//temp->deleted value will shift to false in init_board
}
temp = temp->next_board;
}
return -1;//we have to make a new board
}
void new_game_menu()
{
SBDL::showTexture(difficulty, difficulty_R);
SBDL::showTexture(beginner, beginner_R);
SBDL::showTexture(intermediate, intermediate_R);
SBDL::showTexture(expert, expert_R);
SBDL::showTexture(custom, custom_R);