-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.c
1105 lines (957 loc) · 30.1 KB
/
path.c
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 "path.h"
/** Checks the player's args.
*
* @param argc argc
* @param argv argv
* @return 0 on success.
*/
int check_player_args(int argc, char **argv) {
if (argc != 3) {
player_exit(INVALID_NUM_ARGS_PLAYER);
}
char *trash;
char *validIDs = "0123456789";
// check number of sites is more than 2
int playerCount = (int) strtol(argv[1], &trash, 10);
int playerID = (int) strtol(argv[2], &trash, 10);
if (strstr(validIDs, argv[2]) == NULL || strlen(argv[2]) == 0) {
player_exit(INVALID_ID);
}
if (playerCount < 1) {
player_exit(INVALID_PLAYER_COUNT);
}
if (playerID < 0 || playerID >= playerCount) {
player_exit(INVALID_ID);
}
return 0;
}
/** Allocates the path from the given string.
*
* @param line The contents of the path file
* @param numPlayers The number of players in the game
* @return The game path
*/
Path *allocate_path(char *line, int numPlayers) {
Path *path = malloc(sizeof(Path));
// get number of stops
// following is to find how many stops we have
char temp[] = ";";
char *trash;
int i = (int) strcspn(line, temp);
char numOfSites[5];
snprintf(numOfSites, i + 1, "%s", &line[0]);
int numberOfStops = (int) strtol(numOfSites, &trash, 10);
path->size = numberOfStops;
// make a list of sites with length of the number of stops
Site *sites = malloc(sizeof(Site) * path->size);
for (int siteNum = 0; siteNum < numberOfStops; ++siteNum) {
// break up into each site
// already checked first few chars
char string[4];
snprintf(string, 4, "%s", &line[strlen(numOfSites) + 1 + 3 * siteNum]);
// get site type and its value
// 4th is a number
char type[3];
snprintf(type, 3, "%s", string);
sites[siteNum].type = malloc(sizeof(char) * 3);
strcpy(sites[siteNum].type, type);
if (strcmp(sites[siteNum].type, "::") == 0) {
sites[siteNum].playerLimit = numPlayers;
} else {
sites[siteNum].playerLimit = string[2] - '0';
}
}
path->sites = sites;
path->playersInGame = 0;
return path;
}
/** Prints the player details to stdout.
*
* @param path The game path
* @param playerId The playerID
* @param location The location to display to
*/
void print_player_details(Path *path, int playerId, FILE *location) {
// Cards cards;
Player *player = path->players[playerId];
fprintf(location, "Player %d Money=%d V1=%d V2=%d Points=%d A=%d"
" B=%d C=%d "
"D=%d E=%d\n", playerId, player->money, player->countV1,
player->countV2, player->points, player->cards[A],
player->cards[B], player->cards[C], player->cards[D],
player->cards[E]);
print_path(path, location);
}
/** Exits with a path error for the given process.
*
* @param processType The process type; either player or dealer
*/
void path_exit(ProcessType processType) {
if (processType == DEALER) {
dealer_exit(INVALID_PATH_FILE);
}
player_exit(PATH_ERROR);
}
/** Checks if maps args are valid.
*
* @param numberOfStops The number of stops on the path
* @param processType The process type, either player or dealer
* @param line The contents of the map file
* @param numOfSites The number of sites
*/
void check_map_args(int numberOfStops, ProcessType processType, char *line,
char *numOfSites) {
if (numberOfStops < 2) {
path_exit(processType);
}
// check file length is correct
if (strlen(line) != 3 * numberOfStops + 1 + strlen(numOfSites)) {
path_exit(processType);
}
}
/** Checks if the map file is valid.
*
* @param line The contents of the map file
* @param processType The type of the process, either player or dealer
* @return NO_ERROR on success
*/
int check_map_file(char *line, ProcessType processType) {
// get number of stops
char temp[] = ";";
char *trash;
int i = (int) strcspn(line, temp);
char numOfSites[5];
snprintf(numOfSites, i + 1, "%s", &line[0]);
// check number of sites is more than 2
int numberOfStops = (int) strtol(numOfSites, &trash, 10);
check_map_args(numberOfStops, processType, line, numOfSites);
char *validTypes = "Mo V1 V2 Do Ri ::";
char *validCapacity = "123456789";
// check each site
for (int siteNum = 0; siteNum < numberOfStops; ++siteNum) {
// get entire site
char site[4];
snprintf(site, 4, "%s", &line[strlen(numOfSites) + 1 + 3 * siteNum]);
// get site characters
char type[3];
snprintf(type, 3, "%s", site);
// if not valid type
if (strstr(validTypes, type) == NULL) {
path_exit(processType);
}
// checking capacities
if (strcmp(type, "::") == 0) {
// needs to be -
char dash[2];
snprintf(dash, 2, "%s", &site[2]);
if (strcmp(dash, "-") != 0) {
path_exit(processType);
}
} else {
// is normal site
char str[2] = "\0";
str[0] = site[2];
if (strstr(validCapacity, str) == NULL) {
path_exit(processType);
}
}
// first and last site need to be barriers
if (siteNum == 0 || siteNum == numberOfStops - 1) {
if (strcmp(type, "::") != 0) {
path_exit(processType);
}
}
}
return NO_ERROR;
}
/** Matches a comma in line at the given location.
*
* @param line The HAP message
* @param location The location to match the comma
*/
void match_comma(char *line, int location) {
char read[3];
snprintf(read, 2, "%s", &line[location]);
if (strcmp(read, ",") != 0) {
player_exit(PLAYER_COMMUNICATION_ERROR);
}
}
/** Does associated things with a YT message.
*
* @param line The YT message
* @param path The path
* @param playerId The playerID
* @param playerType The player Type, either A or B
* @return 0 on success.
*/
int do_message_yt(char *line, Path *path, int playerId, char playerType) {
int move = 0;
if (playerType == 'A') {
move = player_a_next_move(path, playerId);
} else if (playerType == 'B') {
move = player_b_next_move(path, playerId);
}
fprintf(stdout, "DO%d\n", path->players[playerId]->siteNumber + move);
fflush(stdout);
return 0;
}
/** Matches the playerID in a HAP message.
*
* @param path The path
* @param line The HAP message
* @return The player ID in the HAP message.
*/
int match_player_id(Path *path, char *line) {
char *trash;
char subString[5];
char *validIDs = "0123456789";
snprintf(subString, 2, "%s", &line[3]);
if (strstr(validIDs, subString) == NULL) {
player_exit(PLAYER_COMMUNICATION_ERROR);
}
int newMovePlayerID = (int) strtol(subString, &trash, 10);
if (newMovePlayerID > path->playersInGame - 1) {
player_exit(PLAYER_COMMUNICATION_ERROR);
}
return newMovePlayerID;
}
/** Matches the site in a HAP message.
*
* @param path The path
* @param line The HAP message
* @return The site in the HAP message
*/
int match_site(Path *path, char *line) {
char *trash;
char subString[5];
// get the site number
snprintf(subString, 5, "%s", &line[5]);
int newMoveSiteID = (int) strtol(subString, &trash, 10);
// received invalid move
if (newMoveSiteID > path->size) {
player_exit(PLAYER_COMMUNICATION_ERROR);
}
// returns the padding following functions need to add
return newMoveSiteID;
}
/** Matches the points in a HAP message.
*
* @param path The game path
* @param line The HAP message
* @param padding The padding to be added to subsequent print calls
* @return The points in the HAP message
*/
int match_points(Path *path, char *line, int padding) {
char *trash;
char subString[5];
char *validIDs = "0123456789";
snprintf(subString, 5, "%s", &line[7 + padding]);
for (int i = 0; i < strlen(subString); ++i) {
char p[2] = "\0";
p[0] = subString[i];
if (subString[i] == ',') {
break;
}
if (strstr(validIDs, p) == NULL) {
player_exit(PLAYER_COMMUNICATION_ERROR);
}
}
// get additional points for player
return (int) strtol(subString, &trash, 10);
}
/** Matches the money in a HAP message.
*
* @param line The message
* @param padding The padding to be added to subsequent print calls
* @return The money of the HAP message
*/
int match_money(char *line, int padding) {
char *trash;
char subString[5];
// get change in money for player
snprintf(subString, 5, "%s", &line[9 + padding]);
int newMoveMoney = (int) strtol(subString, &trash, 10);
return newMoveMoney;
}
/** Matches the card in a HAP message.
*
* @param line The message
* @param padding The padding to be added to subsequent print calls
* @return The card in the HAP message
*/
int match_card(char *line, int padding) {
char *trash;
char subString[5];
// get the card drawn by player.
snprintf(subString, 2, "%s", &line[11 + padding]);
int newMoveCardDrawn = (int) strtol(subString, &trash, 10);
if (newMoveCardDrawn > 5 || newMoveCardDrawn < 0) {
player_exit(PLAYER_COMMUNICATION_ERROR);
}
// check if any more to the line
if (line[12 + padding] != '\0') {
player_exit(PLAYER_COMMUNICATION_ERROR);
}
return newMoveCardDrawn;
}
/** Do V1 and V2 actions.
*
* @param player The player to do the actions to
* @param site The site which the player has landed on
*/
void site_v1_v2(Player *player, Site *site) {
if (strcmp(site->type, V1) == 0) {
player->countV1 += 1;
return;
}
if (strcmp(site->type, V2) == 0) {
player->countV2 += 1;
return;
}
}
/** Performs the associated HAP message on the player.
*
* @param line The line
* @param path
* @param playerID
* @param playerType
* @return 0 if successful.
*/
int do_hap(char *line, Path *path) {
int padding = 0;
// get details from HAP
int newMovePlayerID = match_player_id(path, line);
match_comma(line, 4);
int newMoveSiteID = match_site(path, line);
padding += count_chars(newMoveSiteID) - 1;
match_comma(line, 6 + padding);
int newMovePoints = match_points(path, line, padding);
padding += count_chars(newMovePoints) - 1;
match_comma(line, 8 + padding);
int newMoveMoney = match_money(line, padding);
padding += count_chars(newMoveMoney) - 1;
match_comma(line, 10 + padding);
int newMoveCardDrawn = match_card(line, padding);
Player *player = path->players[newMovePlayerID];
int steps = newMoveSiteID - player->siteNumber;
move_player(path, newMovePlayerID, steps, PLAYER);
Site *site = &path->sites[player->siteNumber];
site_v1_v2(player, site);
player->points += newMovePoints;
player->money += newMoveMoney;
// add card to player's cards
if (newMoveCardDrawn > 0) {
player->cards[newMoveCardDrawn - 1] += 1;
}
print_player_details(path, newMovePlayerID, stderr);
return 0;
}
/** Checks the dealer response and acts accordingly.
*
* @param line The line of input the dealer sent through
* @param path The game path
* @param playerId The playerID
* @param playerType The type of player, either A or B
* @return 0 if successful, 1 otherwise
*/
int check_input(char *line, Path *path, int playerId, char playerType) {
if (strcmp(line, "YT") == 0) {
do_message_yt(line, path, playerId, playerType);
return 0;
}
if (strcmp(line, "EARLY") == 0) {
player_exit(EARLY_GAME_OVER);
return 0;
}
if (strcmp(line, "DONE") == 0) {
game_over(path, stderr);
player_exit(NO_ERROR_PLAYER);
return 0;
}
// need to check first 3 characters
char read[4];
snprintf(read, 4, "%s", &line[0]);
if (strcmp(read, "HAP") == 0) {
do_hap(line, path);
return 0;
}
return 1;
}
/** Signal handler for SIGABRT.
*
* @param signal The signal received
*/
void sig_handler(int signal) {
if (signal == SIGABRT) {
return;
}
}
/** Main loop for a player.
*
* @param numPlayers The number of players in the game
* @param id The player's id
* @param path The game path
* @param playerType The type of the process - either Dealer or Player
*/
void start_player(int numPlayers, int id, Path *path, char playerType) {
print_path(path, stderr);
fflush(stderr);
while (true) {
// wait for dealer input
char *input = read_line(stdin);
if (strcmp(input, "\a") == 0) {
player_exit(PLAYER_COMMUNICATION_ERROR);
}
if (check_input(input, path, id, playerType)) {
player_exit(PLAYER_COMMUNICATION_ERROR);
}
}
}
/** Display each player's score to stdout.
*
* @param path The game path
* @param location The location to display to
*/
void game_over(Path *path, FILE *location) {
int numPlayers = path->playersInGame;
fprintf(location, "Scores: ");
for (int playerId = 0; playerId < numPlayers; ++playerId) {
int score = calculate_total_score(path->players[playerId]);
if (playerId == numPlayers - 1) {
fprintf(location, "%d\n", score);
} else {
fprintf(location, "%d,", score);
}
}
}
/** Returns 0 if the game is over.
*
* @param path The game path
* @return 0 if all players have reached the final barrier.
*/
int check_game_over(Path *path) {
if (path->sites[path->size - 1].numPlayersCurrently ==
path->playersInGame) {
return 0;
}
return 1;
}
/** Returns the index of the next barrier.
*
* @param path The game path
* @param currentSiteID The current site Id
* @return The ID of the next barrier site.
*/
int find_next_barrier(Path *path, int currentSiteID) {
for (int siteID = currentSiteID + 1; siteID < path->size; ++siteID) {
if (strcmp(path->sites[siteID].type, BARRIER) == 0) {
return siteID;
}
}
// if we get here then there are no more barriers, eg we are at end
return currentSiteID;
}
/** Returns 0 if all other players are on later sites.
*
* @param path The game path
* @param player The current player
* @return 0 if all other players are on later sites, 1 otherwise.
*/
int check_if_last_player(Path *path, Player *player) {
// checks from left to right if player is at smallest site
Site *sites = path->sites;
for (int siteID = 0; siteID < path->size; ++siteID) {
// if player is at site
if (sites[siteID].numPlayersCurrently != 0) {
if (player->siteNumber == siteID) {
if (player->position == 1) {
return 0;
}
return 1;
}
return 1;
}
}
return 1;
}
/** Check if the specified site exists before the next barrier.
* Returns -1 if doesnt exist, or returns number of steps if it exists.
* param path: The game path
* param player: The player
* param siteType: The type of site to be searched for
* **/
int check_before_barrier(Path *path, Player *player, char *siteType) {
int nextBarrierID = find_next_barrier(path, player->siteNumber);
Site *sites = path->sites;
for (int siteID = player->siteNumber + 1; siteID < nextBarrierID;
++siteID) {
if (strcmp(sites[siteID].type, siteType) == 0) {
if (available(sites[siteID]) == 0) {
return siteID - player->siteNumber;
}
}
}
return -1;
}
/** Returns a sum of all the player's cards.
*
* @param player The player
* @return The number of cards the player has.
*/
int sum_cards(Player *player) {
int *playerCards = player->cards;
int total = 0;
total += playerCards[A];
total += playerCards[B];
total += playerCards[C];
total += playerCards[D];
total += playerCards[E];
return total;
}
/** Returns 0 if the given player has the most cards of all players.
*
* @param path The game path
* @param player The player to be checked
* @return 0 if the player has the most cards, otherwise, 1
*/
int check_if_most_cards(Path *path, Player *player) {
for (int playerID = 0; playerID < path->playersInGame; ++playerID) {
if (sum_cards(path->players[playerID]) >= sum_cards(player)) {
if (playerID != player->id) {
return 1;
}
}
}
return 0;
}
/** Returns 0 if everyone has zero cards.
*
* @param path The game path
* @return 0 if all players have zero cards, or 1 otherwise.
*/
int check_empty_cards(Path *path) {
Player **players = path->players;
for (int playerID = 0; playerID < path->playersInGame; ++playerID) {
if (sum_cards(players[playerID]) != 0) {
return 1;
}
}
return 0;
}
/** Finds the earliest site which is available.
*
* @param path The game path
* @param currentSiteID The site the player is currently one
* @return The ID of the next available site.
*/
int find_earliest(Path *path, int currentSiteID) {
for (int siteID = currentSiteID + 1; siteID < path->size; ++siteID) {
if (path->sites[siteID].numPlayersCurrently <
path->sites[siteID].playerLimit) {
return siteID;
}
}
// shouldn't be able to get here
return 1;
}
/** Returns the number of steps player B should make.
*
* @param path The game path
* @param playerId The player ID
* @return The number of steps a player of type B should make
*/
int player_b_next_move(Path *path, int playerId) {
Player *player = path->players[playerId];
Site *sites = path->sites;
Site nextSite = sites[player->siteNumber + 1];
// if the next site is not full
if (available(nextSite) == 0) {
// if all other players are on later sites than us
if (check_if_last_player(path, player) == 0) {
return 1;
}
}
// if we have an odd amount of money
if (player->money % 2 == 1) {
// if there is a MO between us and the next barrier
int steps = check_before_barrier(path, player, MO);
if (steps != -1 && !available(path->sites[player->siteNumber +
steps])) {
return steps;
}
}
// if we have the most cards or if everyone has zero cards
if (!check_if_most_cards(path, player) || !check_empty_cards(path)) {
// if there is a Ri between us and the next barrier
int steps = check_before_barrier(path, player, RI);
if (steps != -1) {
return steps;
}
}
// if there is a V2 between us and the next barrier, go there
int steps = check_before_barrier(path, player, V2);
if (steps != -1) {
if (available(path->sites[player->siteNumber + steps]) == 0) {
return steps;
}
}
return find_earliest(path, player->siteNumber) - player->siteNumber;
}
/** Calculates the number of sets of cards the player has.
* Removes cards from the player which have been counted.
* param player: The player
* param setSize: The size of the set to be matched
* return: The number of sets of cards of the given size which the player has
* **/
int calculate_sets(Player *player, int setSize) {
int *cards = player->cards;
int lowestDenom = INT_MAX;
int seenSets = 0;
// find how many sets
for (int suit = A; suit <= E; ++suit) {
if (cards[suit] == 0) {
seenSets += 1;
}
if (cards[suit] < lowestDenom && cards[suit] != 0) {
lowestDenom = cards[suit];
}
}
if (seenSets != (NUM_CARDS - setSize)) {
return 0;
}
// remove counted cards
for (int suit = A; suit <= E; ++suit) {
if (cards[suit] != 0) {
cards[suit] -= lowestDenom;
}
}
return lowestDenom;
}
/** Calculates a player's total store from items and those derived from other
* ways.
* param player: The player
* return: The player's total calculated score.
* **/
int calculate_total_score(Player *player) {
int totalScore = player->points;
// calculate sets of 5
totalScore += calculate_sets(player, 5) * 10;
// calculate sets of 4
totalScore += calculate_sets(player, 4) * 7;
// calculate sets of 3
totalScore += calculate_sets(player, 3) * 5;
// calculate sets of 2
totalScore += calculate_sets(player, 2) * 3;
// calculate sets of 1
totalScore += calculate_sets(player, 1);
// V1
totalScore += player->countV1;
// V2
totalScore += player->countV2;
return totalScore;
}
/** Returns 0 if a site is available to move to.
*
* @param site The site to be checked
* @return 0 if the site is available. 1 if the site is not.
*/
int available(Site site) {
if (site.numPlayersCurrently < site.playerLimit) {
return 0;
}
return 1;
}
/** Returns the number of steps a type A player should make.
*
* @param path The game path
* @param playerId The id of the player to move
* @return The number of steps the player should take
*/
int player_a_next_move(Path *path, int playerId) {
Player *player = path->players[playerId];
Site *sites = path->sites;
int nextBarrierID = find_next_barrier(path, player->siteNumber);
// if player has money and there is Do in front, go there
if (player->money > 0) {
int steps = check_before_barrier(path, player, DO);
if (steps != -1) {
return steps;
}
}
// if the next site is MO and there is room, go there
Site nextSite = sites[player->siteNumber + 1];
if (strcmp(nextSite.type, MO) == 0) {
if (nextSite.numPlayersCurrently < nextSite.playerLimit) {
// return 1 because next site is one space ahead
return 1;
}
}
// pick the closest V1, V2 or Barrier and go there
for (int siteId = player->siteNumber + 1; siteId <= nextBarrierID;
++siteId) {
if (available(sites[siteId]) == 0) {
if (strcmp(sites[siteId].type, V1) == 0) {
return siteId - player->siteNumber;
}
if (strcmp(sites[siteId].type, V2) == 0) {
return siteId - player->siteNumber;
}
if (strcmp(sites[siteId].type, BARRIER) == 0) {
return siteId - player->siteNumber;
}
}
}
// shouldnt be able to get here
return -1;
}
/** Prints the sites and returns the number of lines required to
* display the board.
*
* @param path The game path
* @param location The filestream to write to
* @return The number of lines required for print_path()
*/
int print_path_find_lines(Path *path, FILE *location) {
int numLines = 0;
for (int sitenum = 0; sitenum < path->size; ++sitenum) {
Site site = path->sites[sitenum];
fprintf(location, "%s ", site.type);
fflush(location);
// find out how many lines we need to display
if (site.numPlayersCurrently > numLines) {
numLines = site.numPlayersCurrently;
}
}
return numLines;
}
/** Displays the path to the given filestream.
*
* @param path The game path
* @param location The filestream to print the path to.
*/
void print_path(Path *path, FILE *location) {
Player **players = path->players;
int numLines = print_path_find_lines(path, location);
char **lines = make_empty_strings(numLines, path->size);
// printing player positions below the path
for (int siteNumber = 0; siteNumber < path->size; ++siteNumber) {
Site site = path->sites[siteNumber];
int printed = 0;
int lastPrintedPos = 0;
int playerWithLowest = -1;
// need to loop through each player to find lowest position and print
while (printed < site.numPlayersCurrently) {
int lowestPosition = 9999;
// find positioning
for (int player = 0; player < path->playersInGame; ++player) {
if (players[player]->siteNumber == siteNumber) {
if (players[player]->position < lowestPosition &&
players[player]->position > lastPrintedPos) {
lowestPosition = players[player]->position;
playerWithLowest = player;
}
}
}
for (int j = 0; j < numLines; ++j) {
// if the space hasn't been printed to
if (lines[j][3 * siteNumber] == ' ') {
lines[j][3 * siteNumber] = playerWithLowest + '0';
printed += 1;
lastPrintedPos = lowestPosition;
break;
}
}
}
}
// display lines
for (int line = 0; line < numLines; ++line) {
fprintf(location, "\n%s", lines[line]);
}
fprintf(location, "\n");
fflush(stdout);
}
/** Initialises and returns a player.
*
* @param path The game path
* @param id The player's id
* @return an initialised player
*/
Player *init_player(Path *path, int id) {
Player *player = malloc(sizeof(Player));
player->id = id;
player->money = 7;
player->countV1 = 0;
player->countV2 = 0;
player->siteNumber = 0;
player->points = 0;
// init cards - there are 5 cards to a hand
int *cards = malloc(sizeof(int) * NUM_CARDS);
memset(cards, 0, sizeof(int) * 5);
player->cards = cards;
path->sites[0].numPlayersCurrently += 1;
// dynamically increase playerIDs array for each new player
for (int siteNum = 0; siteNum < path->size; ++siteNum) {
Site *site = &path->sites[siteNum];
if (path->playersInGame == 0) {
site->playerIDs = malloc(sizeof(int));
} else {
site->playerIDs = realloc(site->playerIDs,
sizeof(int) * path->playersInGame + 1);
}
}
path->playersInGame += 1;
// ensure the player only exists at one site
for (int siteNum = 0; siteNum < path->size; ++siteNum) {
path->sites[siteNum].playerIDs[id] = 0;
}
path->sites[0].playerIDs[id] = 1;
player->position = path->sites[0].numPlayersCurrently;
return player;
}
/** Arranges the order of players initially in order of smallest player
* furthest from the path.
* param path: The game path
* **/
void arrange_order_of_players(Path *path) {
Player **players = path->players;
int countPlayers = path->sites[0].numPlayersCurrently;
for (int player = 0; player < countPlayers; ++player) {
players[player]->position = -1 * (player - countPlayers);
}
}
/** Returns the ID of the player who has the highest score in the current
* site.
*
* param path: The game path
* param siteNum: The Site to be checked
* Return: The Id of the player with the highest score on the current site.
*/
int get_highest_score(Path *path, int siteNum) {
int highestScore = 0;
int playerIdwithHS = -1;
for (int i = 0; i < path->playersInGame; ++i) {
// if the player is at the site
if (path->players[i]->siteNumber == siteNum) {
if (path->players[i]->position > highestScore) {
highestScore = path->players[i]->position;
playerIdwithHS = i;
}
}
}
return playerIdwithHS;
}
/** Finds the next play to make a move.
*
* @param path The game path
* @return The ID of the player to move next.
*/
int next_player_to_move(Path *path) {
Site *sites = path->sites;
for (int i = 0; i < path->size; ++i) {
if (sites[i].numPlayersCurrently != 0) {
return get_highest_score(path, i);
}
}
// shouldnt be able to get here
return -1;
}
/** Does action associated with landing on a Ri site.
*
* @param deck The deck of cards to draw from.
* @param player The player who landed on Ri site.