-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFC26.c
More file actions
2925 lines (2465 loc) · 97.3 KB
/
FC26.c
File metadata and controls
2925 lines (2465 loc) · 97.3 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
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 <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
// ==================== inventory.h ====================
#ifndef INVENTORY_H
#define INVENTORY_H
#define MAX_NAME_LEN 50
#define TYPE_LEN 4
typedef struct InventoryNode {
char name[MAX_NAME_LEN];
char type[TYPE_LEN];
int rating;
int price;
struct InventoryNode* next;
struct InventoryNode* prev;
} InventoryNode;
typedef struct PlayerInventory {
InventoryNode* head;
InventoryNode* tail;
int count;
} PlayerInventory;
void initInventory(PlayerInventory* inv);
InventoryNode* createInventoryNode(const char* name, const char* type,
int rating, int price);
void addPlayer(PlayerInventory* inv, const char* name, const char* type,
int rating, int price);
InventoryNode* removePlayerByName(PlayerInventory* inv, const char* name);
InventoryNode* searchInventoryByName(PlayerInventory* inv, const char* name);
void searchInventoryByType(PlayerInventory* inv, const char* type);
void searchInventoryByRating(PlayerInventory* inv, int min, int max);
void displayInventory(PlayerInventory* inv);
void giveInitialPlayers(PlayerInventory* inv, int user_id);
void freeInventory(PlayerInventory* inv);
void seePlayers(PlayerInventory* inv);
#endif
// ==================== squad.h ====================
#ifndef SQUAD_H
#define SQUAD_H
#define SQUAD_SIZE 11
#define MAX_NAME_LEN 50
typedef struct SquadSlot {
char position_type[4];
char player_name[MAX_NAME_LEN];
char native_type[4];
int player_rating;
bool is_filled;
} SquadSlot;
typedef struct Squad {
SquadSlot slots[SQUAD_SIZE];
} Squad;
void initSquad(Squad* squad);
void autoSelectBestSquad(Squad* squad, PlayerInventory* inv);
void displaySquad(Squad* squad);
int getSquadAverageRating(Squad* squad);
bool isPlayerInSquad(Squad* squad, const char* name);
void swapPlayerCards(Squad* squad, PlayerInventory* inv, const char* name1, const char* name2);
void displayAllUserCards(Squad* squad, PlayerInventory* inv);
#endif
// ==================== matchhistory.h ====================
#ifndef MATCHHISTORY_H
#define MATCHHISTORY_H
#define MAX_NAME_LEN 50
#define RESULT_WIN 'W'
#define RESULT_LOSS 'L'
#define RESULT_DRAW 'D'
typedef struct MatchRecord {
char opponent_name[MAX_NAME_LEN];
char result;
int coins_earned;
int week;
int match_number;
int my_goals;
int opp_goals;
int my_shots;
int opp_shots;
int my_assists;
int opp_assists;
int my_fouls;
int opp_fouls;
int my_offsides;
int opp_offsides;
int my_red_cards;
int opp_red_cards;
int my_yellow_cards;
int opp_yellow_cards;
struct MatchRecord* next;
} MatchRecord;
typedef struct MatchStack {
MatchRecord* top;
int total_matches;
int total_wins;
int total_losses;
int total_draws;
int week_wins;
int week_matches;
int current_week;
} MatchStack;
void initMatchStack(MatchStack* stack);
bool isStackEmpty(MatchStack* stack);
void pushMatch(MatchStack* stack, const char* opponent,
char result, int coins, int week,
int my_goals, int opp_goals,
int my_shots, int opp_shots,
int my_assists, int opp_assists,
int my_fouls, int opp_fouls,
int my_offsides, int opp_offsides,
int my_red_cards, int opp_red_cards,
int my_yellow_cards, int opp_yellow_cards);
void displayMatchHistory(MatchStack* stack);
void displayStatistics(MatchStack* stack, int week, int match_num, const char* user_name);
float getWinPercentage(MatchStack* stack);
float getWeeklyWinPercentage(MatchStack* stack);
void resetWeeklyStats(MatchStack* stack);
void freeMatchStack(MatchStack* stack);
#endif
// ==================== user_registration.h ====================
#ifndef USER_REGISTRATION_H
#define USER_REGISTRATION_H
#define MAX_NAME_LEN 50
typedef struct PlayerInventory PlayerInventory;
typedef struct Squad Squad;
typedef struct MatchStack MatchStack;
typedef struct UserNode {
char name[MAX_NAME_LEN];
char password[MAX_NAME_LEN];
int id;
int level;
int coins;
int total_wins;
int total_losses;
int total_draws;
int week_wins;
int week_matches;
int past_week_levels[5];
int past_week_ranks[5];
int past_week_rewards[5];
int past_weeks[5];
int reward_history_count;
PlayerInventory* inventory;
Squad* squad;
MatchStack* match_history;
struct UserNode* next;
struct UserNode* prev;
} UserNode;
typedef struct UserRegistry {
UserNode* head;
UserNode* tail;
int user_count;
} UserRegistry;
void initRegistry(UserRegistry* registry);
int newIDProvider(UserRegistry* registry);
UserNode* addUser(UserRegistry* registry, const char* name, const char* password);
void deleteAccount(UserRegistry* registry, UserNode* node);
void traverseUsers(UserRegistry* registry);
UserNode* findUserByID(UserRegistry* registry, int id);
UserNode* findUserByName(UserRegistry* registry, const char* name);
const char* getLevelName(int level);
void updateUserLevel(UserNode* user, int rank);
int getWeeklyReward(int level);
bool isPartialMatch(const char* str, const char* substr);
#endif
// ==================== marketplace.h ====================
#ifndef MARKETPLACE_H
#define MARKETPLACE_H
#define MAX_NAME_LEN 50
#define TYPE_LEN 4
typedef struct MarketNode {
char name[MAX_NAME_LEN];
char type[TYPE_LEN];
int rating;
int price;
struct MarketNode* next;
struct MarketNode* prev;
} MarketNode;
typedef struct Marketplace {
MarketNode* head;
int count;
} Marketplace;
void initMarketplace(Marketplace* market);
void addToMarketplace(Marketplace* market, const char* name, const char* type, int rating, int price);
MarketNode* removeFromMarketplace(Marketplace* market, const char* name);
MarketNode* searchMarketByName(Marketplace* market, const char* name);
void displayMarketplace(Marketplace* market);
void freeMarketplace(Marketplace* market);
void seedMarketplace(Marketplace* market);
bool searchMarketAdvanced(Marketplace* market, const char* name, const char* type, int min_rating, int max_rating);
#endif
// ==================== matchmaking.h ====================
#ifndef MATCHMAKING_H
#define MATCHMAKING_H
typedef struct MatchQueueNode {
int user_id;
int user_level;
struct MatchQueueNode* next;
} MatchQueueNode;
typedef struct MatchQueue {
MatchQueueNode* front;
MatchQueueNode* rear;
int size;
} MatchQueue;
void initMatchQueue(MatchQueue* queue);
bool isQueueEmpty(MatchQueue* queue);
void enqueueUser(MatchQueue* queue, int user_id, int user_level);
int dequeueUser(MatchQueue* queue);
int peekQueue(MatchQueue* queue);
bool isUserInQueue(MatchQueue* queue, int user_id);
void displayMatchQueue(MatchQueue* queue, UserRegistry* registry);
int findSameLevelOpponent(MatchQueue* queue, int user_id, int user_level);
void removeFromQueue(MatchQueue* queue, int user_id);
#endif
// ==================== leaderboard.h ====================
#ifndef LEADERBOARD_H
#define LEADERBOARD_H
#define MAX_NAME_LEN 50
typedef struct LBNode {
int user_id;
char user_name[MAX_NAME_LEN];
int wins;
int losses;
int draws;
float win_percentage;
float week_win_percentage;
int level;
struct LBNode* next;
} LBNode;
typedef struct Leaderboard {
LBNode* head;
int count;
} Leaderboard;
void initLeaderboard(Leaderboard* lb);
void addToLeaderboard(Leaderboard* lb, int user_id, const char* name,
int wins, int losses, int draws, float win_percentage, float week_win_percentage, int level);
void removeFromLeaderboard(Leaderboard* lb, int user_id);
void updateLeaderboardEntry(Leaderboard* lb, int user_id,
int wins, int losses, int draws, float win_percentage, float week_win_percentage, int level);
void sortLeaderboard(Leaderboard* lb);
void displayLeaderboard(Leaderboard* lb, int current_user_id);
int getUserRank(Leaderboard* lb, int user_id);
const char* getLevelNameLB(int level);
void freeLeaderboard(Leaderboard* lb);
#endif
// ==================== inventory.c ====================
void initInventory(PlayerInventory* inv) {
inv -> head = NULL;
inv -> tail = NULL;
inv -> count = 0;
}
InventoryNode* createInventoryNode(const char* name, const char* type,
int rating, int price) {
InventoryNode* node = (InventoryNode *) malloc(sizeof(InventoryNode));
if (node == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
strncpy(node -> name, name, MAX_NAME_LEN - 1);
node -> name[MAX_NAME_LEN - 1] = '\0';
strncpy(node -> type, type, TYPE_LEN - 1);
node -> type[TYPE_LEN - 1] = '\0';
node -> rating = rating;
node -> price = price;
node -> next = NULL;
node -> prev = NULL;
return node;
}
static int getTypeOrder(const char* t) {
if(strcmp(t, "FW")==0) return 1;
if(strcmp(t, "MF")==0) return 2;
if(strcmp(t, "DF")==0) return 3;
if(strcmp(t, "GK")==0) return 4;
return 5;
}
void addPlayer(PlayerInventory* inv, const char* name, const char* type,
int rating, int price) {
InventoryNode* node = createInventoryNode(name, type, rating, price);
if (inv->head == NULL) {
inv->head = node;
inv->tail = node;
inv->count++;
return;
}
InventoryNode* curr = inv->head;
while(curr != NULL) {
int orderNew = getTypeOrder(type);
int orderCurr = getTypeOrder(curr->type);
if(orderNew < orderCurr || (orderNew == orderCurr && rating > curr->rating)) {
node->next = curr;
node->prev = curr->prev;
if(curr->prev != NULL) { curr->prev->next = node; } else { inv->head = node; }
curr->prev = node;
inv->count++;
return;
}
curr = curr->next;
}
node->prev = inv->tail;
inv->tail->next = node;
inv->tail = node;
inv->count++;
}
InventoryNode* removePlayerByName(PlayerInventory* inv, const char* name) {
InventoryNode* current = inv -> head;
while (current != NULL) {
if (strcasecmp(current -> name, name) == 0) {
if (current -> prev != NULL) {
current -> prev->next = current -> next;
} else {
inv -> head = current -> next;
}
if (current -> next != NULL) {
current -> next->prev = current -> prev;
} else {
inv -> tail = current -> prev;
}
current -> next = NULL;
current -> prev = NULL;
inv -> count--;
return current;
}
current = current -> next;
}
return NULL;
}
InventoryNode* searchInventoryByName(PlayerInventory* inv, const char* name) {
InventoryNode* current = inv -> head;
while (current != NULL) {
if (isPartialMatch(current -> name, name)) {
return current;
}
current = current -> next;
}
return NULL;
}
void searchInventoryByType(PlayerInventory* inv, const char* type) {
bool found = false;
InventoryNode* current = inv -> head;
printf("\n%-25s %-5s %-7s\n", "Name", "Type", "Rating");
printf("%-25s %-5s %-7s\n",
"-------------------------", "-----", "-------");
while (current != NULL) {
if (strcasecmp(current -> type, type) == 0) {
printf("%-25s %-5s %-7d\n",
current -> name, current -> type,
current -> rating);
found = true;
}
current = current -> next;
}
if (!found) {
printf("No players found with type '%s'.\n", type);
}
printf("\n");
}
void searchInventoryByRating(PlayerInventory* inv, int min, int max) {
bool found = false;
InventoryNode* current = inv -> head;
printf("\n%-25s %-5s %-7s\n", "Name", "Type", "Rating");
printf("%-25s %-5s %-7s\n",
"-------------------------", "-----", "-------");
while (current != NULL) {
if (current -> rating >= min && current -> rating <= max) {
printf("%-25s %-5s %-7d\n",
current -> name, current -> type,
current -> rating);
found = true;
}
current = current -> next;
}
if (!found) {
printf("No players found with rating between %d and %d.\n", min, max);
}
printf("\n");
}
void displayInventory(PlayerInventory* inv) {
if (inv -> head == NULL) {
printf("Inventory is empty.\n");
return;
}
printf("\n%-3s %-25s %-5s %-7s\n",
"#", "Name", "Type", "Rating");
printf("%-3s %-25s %-5s %-7s\n",
"---", "-------------------------", "-----", "-------");
int i = 1;
InventoryNode* current = inv -> head;
while (current != NULL) {
printf("%-3d %-25s %-5s %-7d\n",
i, current -> name, current -> type,
current -> rating);
current = current -> next;
i++;
}
printf("\nTotal players: %d\n\n", inv -> count);
}
void giveInitialPlayers(PlayerInventory* inv, int user_id) {
const char* fw_names[] = {"Lionel Messi", "Cristiano Ronaldo", "Kylian Mbappe", "Erling Haaland", "Neymar Jr", "Mohamed Salah", "Harry Kane", "Vinicius Junior", "Lautaro Martinez", "Marcus Rashford"};
const char* mf_names[] = {"Kevin De Bruyne", "Luka Modric", "Toni Kroos", "Bruno Fernandes", "Pedri", "Jude Bellingham", "Bernardo Silva", "Declan Rice", "Frenkie De Jong", "Rodri"};
const char* df_names[] = {"Joao Cancelo", "Kyle Walker", "Trent Alexander Arnold", "Virgil Van Dijk", "Ruben Dias", "Antonio Rudiger", "Kalidou Koulibaly", "Eder Militao", "Marquinhos", "Achraf Hakimi", "Alphonso Davies", "Andrew Robertson", "Reece James", "Dani Carvajal", "Milan Skriniar", "Mats Hummels", "Aymeric Laporte", "Jules Kounde", "Alessandro Bastoni", "Pau Torres"};
const char* gk_names[] = {"Emiliano Martinez", "Thibaut Courtois", "Ederson Moraes", "Alisson Becker", "Mike Maignan", "Gianluigi Donnarumma", "Andre Onana", "Keylor Navas", "Wojciech Szczesny"};
int fw_count = 10, mf_count = 10, df_count = 20, gk_count = 9;
bool fw_picked[10] = {false};
bool mf_picked[10] = {false};
bool df_picked[20] = {false};
bool gk_picked[9] = {false};
for (int i = 0; i < 3; i++) {
int idx;
do { idx = rand() % fw_count; } while (fw_picked[idx]);
fw_picked[idx] = true;
addPlayer(inv, fw_names[idx], "FW", 60 + rand() % 10, 500);
}
for (int i = 0; i < 3; i++) {
int idx;
do { idx = rand() % mf_count; } while (mf_picked[idx]);
mf_picked[idx] = true;
addPlayer(inv, mf_names[idx], "MF", 60 + rand() % 10, 500);
}
for (int i = 0; i < 4; i++) {
int idx;
do { idx = rand() % df_count; } while (df_picked[idx]);
df_picked[idx] = true;
addPlayer(inv, df_names[idx], "DF", 60 + rand() % 10, 500);
}
int idx = rand() % gk_count;
gk_picked[idx] = true;
addPlayer(inv, gk_names[idx], "GK", 60 + rand() % 10, 500);
}
void freeInventory(PlayerInventory* inv) {
InventoryNode* current = inv -> head;
while (current != NULL) {
InventoryNode* temp = current;
current = current -> next;
free(temp);
}
inv -> head = NULL;
inv -> tail = NULL;
inv -> count = 0;
}
void seePlayers(PlayerInventory* inv){
if (inv == NULL || inv->head == NULL) {
printf("\nNo players available in inventory.\n");
return;
}
char movement;
InventoryNode* current = inv->head;
printf("\n%-25s %-5s %-7s %-7s\n", "Name", "Type", "Rating", "Price");
printf("%-25s %-5s %-7s %-7s\n", "-------------------------", "-----", "-------", "-------");
while (1){
printf("%-25s %-5s %-7d %-7d\n", current->name, current->type, current->rating, current->price);
printf("%-25s %-5s %-7s %-7s\n", "-------------------------", "-----", "-------", "-------");
printf("Enter direction (Next: D, Previous: A, Exit: 0): ");
if (scanf(" %c", &movement) != 1) {
printf("\nInput cannot be recognised. Please enter a valid input...\n");
continue;
}
if (movement == 'D' || movement == 'd'){
current = (current == inv->tail) ? inv->head : current->next;
}
else if (movement == 'A' || movement == 'a'){
current = (current == inv->head) ? inv->tail : current->prev;
}
else if (movement == '0' || movement == 'o' || movement == 'O'){
break;
}
else {
printf("\nInput cannot be recognised. Please enter a valid input...\n");
}
}
}
// ==================== squad.c ====================
void initSquad(Squad* squad) {
const char* types[SQUAD_SIZE] = {
"FW","FW","FW",
"MF","MF","MF",
"DF","DF","DF","DF",
"GK"
};
for (int i = 0; i < SQUAD_SIZE; i++) {
strncpy(squad->slots[i].position_type, types[i], 3);
squad->slots[i].position_type[3] = '\0';
squad->slots[i].player_name[0] = '\0';
squad->slots[i].native_type[0] = '\0';
squad->slots[i].player_rating = 0;
squad->slots[i].is_filled = false;
}
}
static InventoryNode* findBestOfType(PlayerInventory* inv, Squad* squad, const char* type) {
InventoryNode* best = NULL;
InventoryNode* current = inv->head;
while (current != NULL) {
if (strcasecmp(current->type, type) == 0) {
bool already_in = false;
for (int k = 0; k < SQUAD_SIZE; k++) {
if (squad->slots[k].is_filled && strcasecmp(squad->slots[k].player_name, current->name) == 0) {
already_in = true; break;
}
}
if (!already_in) {
if (best == NULL || current->rating > best->rating) best = current;
}
}
current = current->next;
}
return best;
}
void autoSelectBestSquad(Squad* squad, PlayerInventory* inv) {
for (int i = 0; i < SQUAD_SIZE; i++) {
squad->slots[i].player_name[0] = '\0';
squad->slots[i].player_rating = 0;
squad->slots[i].is_filled = false;
}
for (int i = 0; i < SQUAD_SIZE; i++) {
InventoryNode* best = findBestOfType(inv, squad, squad->slots[i].position_type);
if (best != NULL) {
strncpy(squad->slots[i].player_name, best->name, MAX_NAME_LEN - 1);
squad->slots[i].player_name[MAX_NAME_LEN - 1] = '\0';
strncpy(squad->slots[i].native_type, best->type, 3);
squad->slots[i].native_type[3] = '\0';
squad->slots[i].player_rating = best->rating;
squad->slots[i].is_filled = true;
}
}
printf("Auto-selection complete.\n");
displaySquad(squad);
}
void displaySquad(Squad* squad) {
printf("\n=== Current Squad ===\n");
printf("%-5s %-5s %-25s %-6s %-7s\n", "Slot", "Pos", "Player", "P Pos", "Rating");
printf("%-5s %-5s %-25s %-6s %-7s\n", "----", "-----", "-------------------------", "------", "-------");
for (int i = 0; i < SQUAD_SIZE; i++) {
if (squad->slots[i].is_filled) {
printf("%-5d %-5s %-25s %-6s %-7d\n", i + 1, squad->slots[i].position_type, squad->slots[i].player_name, squad->slots[i].native_type, squad->slots[i].player_rating);
} else {
printf("%-5d %-5s %-25s %-6s %-7s\n", i + 1, squad->slots[i].position_type, "(empty)", "-", "-");
}
}
printf("\nAverage Squad Rating: %d\n\n", getSquadAverageRating(squad));
}
int getSquadAverageRating(Squad* squad) {
int total = 0, filled = 0;
for (int i = 0; i < SQUAD_SIZE; i++) {
if (squad->slots[i].is_filled) {
total += squad->slots[i].player_rating;
filled++;
}
}
return (filled == 0) ? 0 : total / filled;
}
bool isPlayerInSquad(Squad* squad, const char* name) {
for (int i = 0; i < SQUAD_SIZE; i++) {
if (squad->slots[i].is_filled && isPartialMatch(squad->slots[i].player_name, name)) return true;
}
return false;
}
typedef struct TempCard {
char name[MAX_NAME_LEN];
char type[4];
int rating;
bool in_squad;
int squad_slot;
} TempCard;
// Insertion sort for TempCard using a comparison function
void insertionSortTempCard(TempCard* arr, int n, int (*cmp)(const void*, const void*)) {
for (int i = 1; i < n; i++) {
TempCard key = arr[i];
int j = i - 1;
while (j >= 0 && cmp(&arr[j], &key) > 0) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
int compareCards(const void* a, const void* b) {
TempCard* c1 = (TempCard*)a;
TempCard* c2 = (TempCard*)b;
// position sort: FW -> MF -> DF -> GK
int pos1 = 0, pos2 = 0;
if (strcmp(c1->type, "FW") == 0) pos1 = 1;
else if (strcmp(c1->type, "MF") == 0) pos1 = 2;
else if (strcmp(c1->type, "DF") == 0) pos1 = 3;
else if (strcmp(c1->type, "GK") == 0) pos1 = 4;
if (strcmp(c2->type, "FW") == 0) pos2 = 1;
else if (strcmp(c2->type, "MF") == 0) pos2 = 2;
else if (strcmp(c2->type, "DF") == 0) pos2 = 3;
else if (strcmp(c2->type, "GK") == 0) pos2 = 4;
if (pos1 != pos2) return pos1 - pos2;
return c2->rating - c1->rating;
}
int compareCardsRating(const void* a, const void* b) {
TempCard* c1 = (TempCard*)a;
TempCard* c2 = (TempCard*)b;
return c2->rating - c1->rating;
}
void displayAllUserCards(Squad* squad, PlayerInventory* inv) {
TempCard cards[200];
int count = 0;
for (int i = 0; i < SQUAD_SIZE; i++) {
if (squad->slots[i].is_filled) {
strcpy(cards[count].name, squad->slots[i].player_name);
strcpy(cards[count].type, squad->slots[i].native_type);
cards[count].rating = squad->slots[i].player_rating;
cards[count].in_squad = true;
cards[count].squad_slot = i;
count++;
}
}
int squad_count = count;
InventoryNode* curr = inv->head;
while (curr != NULL) {
bool already_in = false;
for (int i = 0; i < SQUAD_SIZE; i++) {
if (squad->slots[i].is_filled && strcmp(squad->slots[i].player_name, curr->name) == 0) {
already_in = true; break;
}
}
if (!already_in) {
strcpy(cards[count].name, curr->name);
strcpy(cards[count].type, curr->type);
cards[count].rating = curr->rating;
cards[count].in_squad = false;
cards[count].squad_slot = -1;
count++;
}
curr = curr->next;
}
// Lineup gets ordered by position FW -> MF -> DF -> GK
insertionSortTempCard(cards, squad_count, compareCards);
// Other players ordered strictly by rating descending
if (count > squad_count) {
insertionSortTempCard(cards + squad_count, count - squad_count, compareCardsRating);
}
printf("\n=== All Player Cards ===\n");
printf("\n--- LINEUP ---\n");
printf("%-3s %-25s %-6s %-7s\n", "#", "Player", "P Pos", "Rating");
printf("%-3s %-25s %-6s %-7s\n", "---", "-------------------------", "------", "-------");
for (int i = 0; i < squad_count; i++) {
printf("%-3d %-25s %-6s %-7d\n", i + 1, cards[i].name, cards[i].type, cards[i].rating);
}
if (count > squad_count) {
printf("\n--- OTHER PLAYERS ---\n");
printf("--------------------------------------------\n");
for (int i = squad_count; i < count; i++) {
printf("%-3d %-25s %-6s %-7d\n", i + 1, cards[i].name, cards[i].type, cards[i].rating);
}
}
printf("\n");
}
void swapPlayerCards(Squad* squad, PlayerInventory* inv, const char* name1, const char* name2) {
int s1 = -1, s2 = -1;
for (int i = 0; i < SQUAD_SIZE; i++) {
if (squad->slots[i].is_filled) {
if (s1 == -1 && isPartialMatch(squad->slots[i].player_name, name1)) s1 = i;
if (s2 == -1 && isPartialMatch(squad->slots[i].player_name, name2)) s2 = i;
}
}
InventoryNode* i1 = NULL;
InventoryNode* i2 = NULL;
InventoryNode* curr = inv->head;
while (curr != NULL) {
if (i1 == NULL && isPartialMatch(curr->name, name1)) i1 = curr;
if (i2 == NULL && isPartialMatch(curr->name, name2)) i2 = curr;
curr = curr->next;
}
if ((s1 == -1 && i1 == NULL) || (s2 == -1 && i2 == NULL)) {
printf("One or both players not found.\n");
return;
}
if (s1 != -1 && s2 != -1) {
char temp_name[MAX_NAME_LEN];
char temp_type[4];
int temp_rating;
strcpy(temp_name, squad->slots[s1].player_name);
strcpy(temp_type, squad->slots[s1].native_type);
temp_rating = squad->slots[s1].player_rating;
strcpy(squad->slots[s1].player_name, squad->slots[s2].player_name);
strcpy(squad->slots[s1].native_type, squad->slots[s2].native_type);
squad->slots[s1].player_rating = squad->slots[s2].player_rating;
strcpy(squad->slots[s2].player_name, temp_name);
strcpy(squad->slots[s2].native_type, temp_type);
squad->slots[s2].player_rating = temp_rating;
printf("Swapped %s and %s in the squad.\n", squad->slots[s2].player_name, squad->slots[s1].player_name);
return;
}
if (s1 != -1 && s2 == -1 && i2 != NULL) {
printf("Replaced %s with %s in the squad.\n", squad->slots[s1].player_name, i2->name);
strcpy(squad->slots[s1].player_name, i2->name);
strcpy(squad->slots[s1].native_type, i2->type);
squad->slots[s1].player_rating = i2->rating;
return;
}
if (s2 != -1 && s1 == -1 && i1 != NULL) {
printf("Replaced %s with %s in the squad.\n", squad->slots[s2].player_name, i1->name);
strcpy(squad->slots[s2].player_name, i1->name);
strcpy(squad->slots[s2].native_type, i1->type);
squad->slots[s2].player_rating = i1->rating;
return;
}
printf("At least one player must be in the squad to swap.\n");
}
// ==================== matchhistory.c ====================
void initMatchStack(MatchStack* stack) {
stack -> top = NULL;
stack -> total_matches = 0;
stack -> total_wins = 0;
stack -> total_losses = 0;
stack -> total_draws = 0;
stack -> week_wins = 0;
stack -> week_matches = 0;
stack -> current_week = 1;
}
bool isStackEmpty(MatchStack* stack) {
return stack -> top == NULL;
}
void pushMatch(MatchStack* stack, const char* opponent,
char result, int coins, int week,
int my_goals, int opp_goals,
int my_shots, int opp_shots,
int my_assists, int opp_assists,
int my_fouls, int opp_fouls,
int my_offsides, int opp_offsides,
int my_red_cards, int opp_red_cards,
int my_yellow_cards, int opp_yellow_cards) {
MatchRecord* record = (MatchRecord *) malloc(sizeof(MatchRecord));
if (record == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
strncpy(record -> opponent_name, opponent, MAX_NAME_LEN - 1);
record -> opponent_name[MAX_NAME_LEN - 1] = '\0';
record -> result = result;
record -> coins_earned = coins;
record -> week = week;
int match_count = 0;
MatchRecord* curr = stack->top;
while (curr) {
if (curr->week == week) match_count++;
curr = curr->next;
}
record -> match_number = match_count + 1;
record -> my_goals = my_goals;
record -> opp_goals = opp_goals;
record -> my_shots = my_shots;
record -> opp_shots = opp_shots;
record -> my_assists = my_assists;
record -> opp_assists = opp_assists;
record -> my_fouls = my_fouls;
record -> opp_fouls = opp_fouls;
record -> my_offsides = my_offsides;
record -> opp_offsides = opp_offsides;
record -> my_red_cards = my_red_cards;
record -> opp_red_cards = opp_red_cards;
record -> my_yellow_cards = my_yellow_cards;
record -> opp_yellow_cards = opp_yellow_cards;
record -> next = stack -> top;
stack -> top = record;
stack -> total_matches++;
if (result == RESULT_WIN) {
stack -> total_wins++;
stack -> week_wins++;
stack -> week_matches++;
} else if (result == RESULT_LOSS) {
stack -> total_losses++;
stack -> week_matches++;
} else if (result == RESULT_DRAW) {
stack -> total_draws++;
stack -> week_matches++;
}
}
void popMatch(MatchStack* stack) {
if (isStackEmpty(stack)) {
printf("Match history is empty.\n");
return;
}
MatchRecord* temp = stack -> top;
stack -> top = stack -> top->next;
stack -> total_matches--;
if (temp -> result == RESULT_WIN) {
stack -> total_wins--;
} else if (temp -> result == RESULT_LOSS) {
stack -> total_losses--;
} else if (temp -> result == RESULT_DRAW) {
stack -> total_draws--;
}
free(temp);
}
void displayMatchHistory(MatchStack* stack) {
if (isStackEmpty(stack)) {
printf("No match history available.\n");
return;
}
printf("\n=== Match History ===\n");
printf("%-5s %-20s %-7s %-10s %-5s\n",
"#", "Opponent", "Result", "Match No", "Week");
printf("%-5s %-20s %-7s %-10s %-5s\n",
"---", "--------------------", "------", "--------", "----");
MatchRecord* current = stack -> top;
int num = 1;
int min_week = stack->current_week - 3;
if (min_week < 1) min_week = 1;
while (current != NULL) {
if (current->week >= min_week && current->week <= stack->current_week) {