/
HunterView.c
executable file
·651 lines (572 loc) · 19.7 KB
/
HunterView.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
// HunterView.c ... HunterView ADT implementation
#include <stdlib.h>
#include <assert.h>
#include "Globals.h"
#include "Game.h"
#include "GameView.h"
#include "HunterView.h"
#include "Map.h" //... if you decide to use the Map ADT
//Players' Status
typedef struct _player *PlayerLink; //Pointer to Player
typedef struct _player {
int currentHealth;
LocationID currentLocation;
HistoryList historyList;
}Player;
//Players location storing
typedef struct _history *HistoryLink;
typedef struct _history {
LocationID location;
HistoryLink next;
}History;
typedef struct _historyList{
HistoryLink head;
}HistoryList;
//Trap & Vampire storing
typedef struct _torv *TorVLink;
typedef struct _torv{
LocationID location;
int v; //Vampire
int t; //Trap
TorVLink next;
}TorV;
typedef struct _torvList{
TorVLink head;
}TorVList;
//LinkedList for map functions
typedef struct _location *LocationLink;
typedef struct _location{
LocationID location;
LocationLink next;
}LocationRep;
typedef struct _locationList{
LocationLink head;
}LocationList;
struct gameView {
PlayerID currentPlayer;
Round currentRound;
int currentScore;
Player LG; //Lord Godalming
Player DS; //Dr. Seward
Player VH; //Van Helsing
Player MH; //Mina Harker
Player DC; //Dracula
TorVList torvList;
};
//Supplementary functions
static void processTurn(char *pastPlays, int counter, GameView gameView); // Processes each turn for each player (process each 7 chars)
static void processHunter (char *pastPlays, int counter, GameView gameView); // Process Hunter Type Turn
static void processDracula (char *pastPlays, int counter, GameView gameView); // Process Dracula Type Turn
//Hunter specific functions
static void hunterRest(int currentHealth, int bool); //Score modifier for resting
static void maxHealth(int currentHealth); //Limit maximum health of Hunter
//Dracula Specific Functions
static void processDoubleBack(char *arrayLocation, LocationID currentLocation); //Processing doubleback turn
static void draculaEncounter(LocationID dracula, LocationID hunter1, LocationID hunter2 ,LocationID hunter3 ,LocationID hunter4, int currentHealth); //Score Modifier for hunter encounter
static void checkSea(LocationID location, int currentHealth); //Score modifier for SEA move
//Technical functions
static int arrayLength (char *pastPlays); //Same as strlen in string.h
static void copyLocation (char *pastPlays, int counter, char *array); // Same as strcpy in string.h
static PlayerLink playerSelector (PlayerID currentPlayer, gameView g); // Pointer to Player
//LinkedList Functions
static void historyAdd(HistoryList l, LocationID currLocation);
static int historyLength(HistoryList l);
static void torvAdd(TorVList l, LocationID currLocation, int vampire, int trap);
static void torvRemove(TorVList l, LocationID currLocation, int vampire, int trap);
//Linkedlist Map Location
static LocationList newLocation();
void addLocation(LocationList l, LocationID new);
int countLocation(LocationList l);
// Creates a new HunterView to summarise the current state of the game
HunterView newHunterView(char *pastPlays, PlayerMessage messages[])
{
HunterView hunterView = malloc(sizeof(struct hunterView));
assert(hunterView != NULL);
// Initialize
hunterView->currentRound = 0;
hunterView->LG->currentHealth = hunterView->DS->currentHealth = hunterView->VH->currentHealth = hunterView->MH->currentHealth = GAME_START_HUNTER_LIFE_POINTS;
hunterView->DC->currentHealth = GAME_START_BLOOD_POINTS;
hunterView->currentScore = GAME_START_SCORE;
hunterView->currentPlayer = PLAYER_LORD_GODALMING;
hunterView->LG->currentLocation = hunterView->DC->currentLocation = hunterView->VH->currentLocation = hunterView->MH->currentLocation = hunterView->DC->currentLocation = UNKNOWN_LOCATION;
int playLength = arrayLength (pastPlays);
int counter = 0;
if(playLength == 0){
return hunterView;
} else {
while(playLength[counter] != NULL){
processTurn(pastPlays,counter,hunterView);
counter++;
}
}
return hunterView;
}
// Frees all memory previously allocated for the HunterView toBeDeleted
void disposeHunterView(HunterView toBeDeleted){
assert(toBeDeleted != NULL);
free( toBeDeleted->LG->currentHealth );
free( toBeDeleted->DS->currentHealth );
free( toBeDeleted->VH->currentHealth );
free( toBeDeleted->MH->currentHealth );
free( toBeDeleted->DC->currentHealth );
free( toBeDeleted->LG->currentLocation );
free( toBeDeleted->DS->currentLocation);
free( toBeDeleted->VH->currentLocation );
free( toBeDeleted->MH->currentLocation );
free( toBeDeleted->DC->currentLocation );
free( toBeDeleted->currentRound );
free( toBeDeleted->currentPlayer );
free( toBeDeleted->currrentScore );'
free( toBeDeleted->LG);
free( toBeDeleted->DS);
free( toBeDeleted->VH);
free( toBeDeleted->MH);
free( toBeDeleted->DC);
free( toBeDeleted );
}
//// Functions to return simple information about the current state of the game
// Get the current round
Round giveMeTheRound(HunterView currentView)
{
return (currentView->currentRound);
}
// Get the id of current player
PlayerID whoAmI(HunterView currentView)
{
return (currentView->currentPlayer);
}
// Get the current score
int giveMeTheScore(HunterView currentView)
{
return (currentView->currentScore);
}
// Get the current health points for a given player
int howHealthyIs(HunterView currentView, PlayerID player)
{
return ((playerSelector(player,currentView))->currentHealth);
}
// Get the current location id of a given player
LocationID whereIs(HunterView currentView, PlayerID player)
{
return ((playerSelector(player,currentView))->currentLocation);
}
//// Functions that return information about the history of the game
// Fills the trail array with the location ids of the last 6 turns
void giveMeTheTrail(HunterView currentView, PlayerID player,
LocationID trail[TRAIL_SIZE])
{
PlayerLink p = playerSelector(player,currentView);
HistoryLink link = p->historyList->head;
HistoryList l = p->historyList
int length = historyLength(l);
int counter = 0;
if(length <= 6){ //History less than or equal to 6
while(link != NULL){
int unknownPlaceCounter = TRAIL_SIZE - length;
while(unknownPlaceCounter > 0)
trail[counter] = UNKNOWN_LOCATION;
link = link->next;
counter--;
unknownPlaceCounter--;
}
while(counter > 0){
trail[counter] = link->location;
link = link->next;
counter--;
}
}
} else { // History more than 6
int startPos = (length - 6);
int startCounter = 1;
while(counter < int startPos){
link = link->next;
startCounter++;
}
while(link != NULL){
trail[counter] = link->location;
link = link->next;
counter++;
}
}
}
//// Functions that query the map to find information about connectivity
// What are my possible next moves (locations)
LocationID *whereCanIgo(HunterView currentView, int *numLocations,
int road, int rail, int sea)
{
Map map = newmap();
TransportID transport;
LocationList tempLoc = newLocation();
if(road == 1) {
transport = ROAD;
} else if(rail == 1){
transport = RAIL;
} else if(boat == 1) {
transport = BOAT;
}
//Scanning transport
VList temp = map->connections[from];
while(temp != NULL){
if(temp->type == transport){
if(transport == RAIL){
if(round % 4 == 0){
temp = temp->next;
}
}else{
addLocation(tempLoc, temp->v);
temp = temp->next;
}
}else{
temp = temp->next;
}
}
destroyMap(map);
//Counting nodes
int counter = countLocation(tempLoc);
//Place connected ID to array
LocationID connectedPlaces[(counter-1)];
int tempCounter = 0;
LocationLink tempLink = tempLoc->head;
while(tempLink !=NULL){
connectedPlaces[tempCounter] = tempLink->location;
tempLink = tempLink->next;
tempCounter++;
}
return *connectedPlaces;
}
// What are the specified player's next possible moves
LocationID *whereCanTheyGo(HunterView currentView, int *numLocations,
PlayerID player, int road, int rail, int sea)
{
Map map = newmap();
TransportID transport;
LocationList tempLoc = newLocation();
if(road == 1) {
transport = ROAD;
} else if(rail == 1){
transport = RAIL;
} else if(boat == 1) {
transport = BOAT;
}
//Scanning transport
VList temp = map->connections[from];
while(temp != NULL){
if(temp->type == transport){
if(player == PLAYER_DRACULA && (transport == RAIL || temp->v == ST_JOSEPH_AND_ST_MARYS)){
temp = temp->next;
} else if((player == PLAYER_DR_SEWARD || player == PLAYER_LORD_GODALMING || player == PLAYER_MINA_HARKER || player == PLAYER_VAN_HELSING) && transport == RAIL){
if(round % 4 == 0){
temp = temp->next;
}
}else{
addLocation(tempLoc, temp->v);
temp = temp->next;
}
}else{
temp = temp->next;
}
}
destroyMap(map);
//Counting nodes
int counter = countLocation(tempLoc);
//Place connected ID to array
LocationID connectedPlaces[(counter-1)];
int tempCounter = 0;
LocationLink tempLink = tempLoc->head;
while(tempLink !=NULL){
connectedPlaces[tempCounter] = tempLink->location;
tempLink = tempLink->next;
tempCounter++;
}
return *connectedPlaces;
}
///////////////////////////
//Supplementary Functions//
///////////////////////////
int arrayLength (char *pastPlays){
int length = 0;
while(pastPlays[length] != NULL){
length++;
} return length;
}
void copyLocation (char *pastPlays, int counter, char *array){
int tempCounter = 0;
array[tempCounter] = pastPlays[counter];
array[tempCounter+1] = pastPlays[counter+1];
array[tempCounter+2] = NULL;
}
PlayerLink playerSelector (PlayerID Player, GameView g){
PlayerLink *temp = malloc(sizeof(struct _player));
if(Player == 0){
temp = gameView->LG;
}else if(currentPlayer == 1){
temp = gameView->DS;
}else if(currentPlayer == 2){
temp = gameView->VH;
}else if(currentPlayer == 3){
temp = gameView->MH;
}else if(currentPlayer == 4){
temp = gameView->DC;
}
return temp;
}
PlayerID currentTurnSelector(char *pastPlays, int counter){
PlayerID player;
if(pastPlays[counter] == 'G'){
player = PLAYER_LORD_GODALMING;
}else if(pastPlays[counter] == 'S'){
player = PLAYER_DR_SEWARD;
}else if(pastPlays[counter] == 'H'){
player = PLAYER_VAN_HELSING;
}else if(pastPlays[counter] == 'M'){
player = PLAYER_MINA_HARKER;
}else if(pastPlays[counter] == 'D'){
player = PLAYER_DRACULA ;
}
return player;
}
//////////////////
//MAIN FUNCTIONS//
//////////////////
void processTurn(char *pastPlays, int counter, GameView gameView){
if(pastPlays[counter] != 'D'){
processHunter (pastPlays,counter,gameView);
}else{
processDracula(pastPlays,counter,gameView);
}
}
void processHunter (char *pastPlays, int counter, GameView gameView){
char tempNewLocation [3];
int rest = FALSE;
int turnCounter = counter+6;
//Determining Player to be Processed
gameView->currentPlayer = currentTurnSelector(char *pastPlays, int counter)
PlayerLink player = (playerSelector(gameView->currentPlayer,gameView)); //Pointer to current Player ADT.
counter++; // Advance to Location
//Processing Location
copyLocation(pastPlays,counter,tempNewLocation);
if(currentLocation == tempNewLocation){
rest = TRUE;
}
player->currentLocation = abbrevToID(tempNewLocation);
counter = counter+2; // Advance to Actions
//Processing Actions
while(counter <= turnCounter){
if(pastPlays[counter] == 'T'){
player->currentHealth = (player->currentHealth)-2;
void hunterHospital(int currentHealth, LocationID currentLocation);
counter++;
} else if(pastPlays[counter] == 'V'){
//VANQUISH IMMATURE VAMPIRE
counter++
} else if(pastPlays[counter] == 'D'){
player->currentHealth = (player->currentHealth)-LIFE_LOSS_DRACULA_ENCOUNTER;
gameView->DC->currentHealth = (gameView->DC->currentHealth)-LIFE_LOSS_HUNTER_ENCOUNTER ;
counter++;
} else if(pastPlays[counter] == '.'){
counter++;
} else{
printf("INVALID INPUT");
abort();
}
}
hunterRest(player->currentHealth, rest);
}
void processDracula (char *pastPlays, int counter, GameView gameView){
char tempNewLocation [3];
int turnCounter = counter+6;
//Set currentTurn
gameView->currentPlayer = currentTurnSelector(char *pastPlays, int counter)
counter++; // Advance to Location
//Processing Location
copyLocation(pastPlays,counter,tempNewLocation);
if(tempNewLocation == 'C?'){
gameView->DC->currentLocation = CITY_UNKNOWN
}else if (tempNewLocation == 'S?'){
gameView->DC->currentLocation = SEA_UNKNOWN
}else if (tempNewLocation == 'HI'){
//Stays, do nothing;
}else if (tempNewLocation == 'TP'){
gameView->DC->currentLocation = CASTLE_DRACULA
}else if (tempNewLocation[0] == 'D' && tempNewLocation[1] != 'U'){ // Not Dublin
processDoubleBack(tempNewLocation,gameView->DC->currentLocation);
}else{
gameView->DC->currentLocation = abbrevToID(tempNewLocation);
}
counter = counter+2; // Advance to Actions
//Processing Encounter;
while(counter < (turnCounter-2)){
if(pastPlays[counter] == 'T' ){
//Place Trap at currentLocation
}else if(pastPlays[counter] == 'V'){
//Place Immature Dracula at currentLocation
}
counter++;
}
//Processing Action;
while(counter <= turnCounter){
if(pastPlays[counter] == 'M'){
//Trap is gone
}else if (pastPlays[counter] == 'V'){
//Vampire has Matured
gameView->currentScore = (gameView->currentScore) - SCORE_LOSS_VAMPIRE_MATURES;
}
counter++
}
//Score modifiers
draculaEncounter(gameView->DC->currentLocation, gameView->LG->currentLocation, gameView->DS->currentLocation, gameView->VH->currentLocation,gameView->MH->currentLocation,gameView->currentScore);
checkSea(gameView->DC->currentLocation, gameView->DC->currentHealth);
draculaCastle(gameView->DC->currentLocation, gameView->DC->currentHealth);
gameView->currentScore = (gameView->currentScore)-SCORE_LOSS_DRACULA_TURN;
}
/////////////////////////////
//HUNTER SPECIFIC FUNCTIONS//
/////////////////////////////
void hunterRest(int currentHealth, int bool){
if(bool == TRUE){
currentHealth = currentHealth + LIFE_GAIN_REST;
maxHealth(currentHealth);
}
}
void maxHealth(int currentHealth){
if(currentHealth > 9)currentHealth = HUNTER_MAX_HEALTH;
}
void hunterHospital(int currentHealth, LocationID currentLocation){ //Teleport to Hospital if Hunter's health below 0
if(currentHealth < 0){
currentLocation = ST_JOSEPH_AND_ST_MARYS;
currentHealth = 0;
}
}
//////////////////////////////
//DRACULA SPECIFIC FUNCTIONS//
//////////////////////////////
void processDoubleBack(char *arrayLocation, HistoryList l, int nBack, LocationID currlocation){
int length = (historyLength(l) - nBack);
int counter = 1;
HistoryLink link = l->head;
while(counter < length){
link = link->next;
counter++;
}
currLocation = link->location;
link = NULL;
free(link);
}
void draculaEncounter(LocationID dracula, LocationID hunter1, LocationID hunter2 ,LocationID hunter3 ,LocationID hunter4, int currentHealth){
if(dracula == hunter1 || dracula == hunter2 || dracula == hunter3 || dracula == hunter4)currentHealth = currentHealth - LIFE_LOSS_HUNTER_ENCOUNTER;
}
void checkSea(LocationID location, int currentHealth){
if( idToType(location) == 'SEA')currentHealth = currentHealth - LIFE_LOSS_SEA;
}
void draculaCastle(LocationID location, int currentHealth){
if(location == CASTLE_DRACULA)currentHealth = currentHealth + LIFE_GAIN_CASTLE_DRACULA;
}
////////////////////////////////
//HISTORY LINKEDLIST FUNCTIONS//
////////////////////////////////
void historyAdd(HistoryList l, LocationID currLocation){
HistoryLink link = l;
History newHistory == malloc(sizeof(History));
newHistory->location = currLocation;
newHistory->next = NULL;
link = l->head;
if(link == NULL){
l->head = newHistory;
free(link);
} else {
while(link->next != NULL){
link = link->next;
}
link->next = newHistory;
}
}
int historyLength(HistoryList l){
int length = 0;
HistoryLink link = l->head;
while(link != NULL){
length++
link = link->next;
}
return length;
}
////////////////////////////
//Trap & Vampire Functions//
////////////////////////////
void torvAdd(TorVList l, LocationID currLocation, int vampire, int trap){
TorVLink link = l->head;
if(link == NULL){ //When list is empty
TorV newTorV == malloc(sizeof(History));
newTorV->location = currLocation;
newTorV->v = vampire;
newTorV->t = trap;
newTorV->next = NULL;
link = newTorV;
}else{
while(link->next) != NULL && (link->location) != currLocation){
link = link->next;
}
if(link->location == currentLocation){ //Existing data found
link->v = (link->v) + vampire;
link->t = (link->t) + trap;
}else{ //Create new Entry
TorV newTorV == malloc(sizeof(History));
newTorV->location = currLocation;
newTorV->v = vampire;
newTorV->t = trap;
newTorV->next = NULL;
link->next = newTorV;
}
}
}
void torvRemove(TorVList l, LocationID currLocation, int vampire, int trap){
TorVLink link = l->head;
while(link->next) != NULL && (link->location) != currLocation){
link = link->next;
}
link->v = (link->v) - vampire;
link->t = (link->t) - trap;
//Delete Entry if Trap & Vampire = 0
if(link->v == 0 && link->t == 0){
TorVLink tempLink = l->head;
while(tempLink->next != link){
tempLink->tempLink->next;
}
tempLink->next = link->next;
free(link);
}
}
////////////////////////////
//Map LinkedList Functions//
////////////////////////////
LocationList newLocation(){
LocationList new = malloc(sizeof(LocationList));
new->head = NULL;
return new;
}
void addLocation(LocationList l, LocationID new){
LocationLink temp = l->head;
if(temp == NULL){
LocationRep newLocation = malloc(sizeof(LocationRep));
newLocation->location = new;
newLocation->next = NULL;
temp = newLocation;
}
else{
while(temp->next != NULL){
temp = temp->next;
}
LocationRep newLocation = malloc(sizeof(LocationRep));
newLocation->location = new;
newLocation->next = NULL;
temp->next = newLocation;
}
}
int countLocation(LocationList l){
LocationLink temp = l->head;
int counter = 0;
while(l != NULL){
counter++;
temp = temp->next;
}
return counter;
}