-
Notifications
You must be signed in to change notification settings - Fork 0
/
HunterView.c
405 lines (361 loc) · 15.1 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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "game.h"
#include "HunterView.h"
#define CHARS_PER_ROUND 40
#define CHARS_PER_TURN 8
typedef struct player *Player;
struct hunterView {
int score;
Round round;
PlayerID currentPlayer;
Player players[NUM_PLAYERS];
playerMessage messages[MESSAGE_SIZE];
int trap[TRAIL_SIZE];
int vampire[TRAIL_SIZE];
int amtMess;
};
struct player {
int health;
LocationID location[TRAIL_SIZE];
int hospital;
};
static int isAtSea(LocationID location, HunterView hunterView);
static int isInCity(LocationID location, HunterView hunterView);
static int doubledBack (LocationID location, HunterView hunterView);
// newHunterView creates a new hunter view to summarise the current state of
// the game.
//
// pastPlays is a string of all the plays made in the game so far by all
// players (including Dracula) from earliest to most recent.
//
//
// messages is an array containing a playerMessage for each play in the game
// so far. It will have exactly the same number of elements as there are plays
// in pastPlays. The message from the first play will be at index 0, and so on.
// The contents of each playerMessage will be exactly as provided by the player.
//
// The "playerMessage" type is defined in game.h.
// You are free to ignore messages if you wish.
HunterView newHunterView( char *pastPlays, playerMessage messages[] ) {
HunterView hunterView = malloc(sizeof(struct hunterView));
//Graph g = newGraph();
int i, j;
int player;
hunterView->score = GAME_START_SCORE;
char *locations[] = {
"AL", "AM", "AT", "BA", "BI", "BE", "BR", "BO", "BU", "BC",
"BD", "CA", "CG", "CD", "CF", "CO", "CN", "DU", "ED", "FL",
"FR", "GA", "GW", "GE", "GO", "GR", "HA", "JM", "KL", "LE",
"LI", "LS", "LV", "LO", "MA", "MN", "MR", "MI", "MU", "NA",
"NP", "NU", "PA", "PL", "PR", "RO", "SA", "SN", "SR", "SJ",
"SO", "ST", "SW", "SZ", "TO", "VA", "VR", "VE", "VI", "ZA",
"ZU", "NS", "EC", "IS", "AO", "BB", "MS", "TS", "IO", "AS",
"BS", "C?", "S?", "HI", "D1", "D2", "D3", "D4", "D5", "TP"
};
for (i = 0; i < NUM_PLAYERS; i++) {
hunterView->players[i] = malloc(sizeof(struct player));
hunterView->players[i]->health = GAME_START_HUNTER_LIFE_POINTS;
if (i == 4) hunterView->players[i]->health = GAME_START_BLOOD_POINTS;
hunterView->players[i]->hospital = FALSE;
for (j = 0; j < TRAIL_SIZE; j++) {
hunterView->players[i]->location[j] = -1;
hunterView->vampire[i] = 0;
}
}
i = 0;
while (pastPlays[i] != '\0') {
//this character should represent the player name
if (pastPlays[i] == 'G') player = PLAYER_LORD_GODALMING;
else if (pastPlays[i] == 'S') player = PLAYER_DR_SEWARD;
else if (pastPlays[i] == 'H') player = PLAYER_VAN_HELSING;
else if (pastPlays[i] == 'M') player = PLAYER_MINA_HARKER;
else if (pastPlays[i] == 'D') player = PLAYER_DRACULA;
else {
perror("Something is wrong with pastPlays\n");
abort();
}
i++;
int z = 0;
char a[] = {pastPlays[i], pastPlays[i+1]};
while (z < NUM_LOCATIONS && (locations[z][0] != a[0] || locations[z][1] != a[1])) z++;
//maturing vampires
//if (hunterView->vampire[TRAIL_SIZE - 1]) hunterView->score -= SCORE_LOSS_VAMPIRE_MATURES;
//move array along 1
for (j = TRAIL_SIZE - 1; j > 0; j--) {
hunterView->players[player]->location[j] = hunterView->players[player]->location[j-1];
hunterView->vampire[j] = hunterView->vampire[j-1];
hunterView->trap[j] = hunterView->trap[j-1];
}
//set location array
hunterView->players[player]->location[0] = z;
//remove hospital state
if (hunterView->players[player]->hospital) hunterView->players[player]->hospital = FALSE;
if ((z == TELEPORT || z == CASTLE_DRACULA) && (player == PLAYER_DRACULA))
hunterView->players[player]->health += LIFE_GAIN_CASTLE_DRACULA;
i += 2;
hunterView->vampire[0] = 0;
hunterView->trap[0] = 0;
if (player == PLAYER_DRACULA) {
if (pastPlays[i] == 'T') {
hunterView->trap[0] = 1;
}
i++;
if (pastPlays[i] == 'V') {
//vampire placed
hunterView->vampire[0] = 1;
}
i++;
if (pastPlays[i] == 'M') {
//trap has left trail (already handled?)
} else if (pastPlays[i] == 'V') {
//vampire matures
hunterView->score -= SCORE_LOSS_VAMPIRE_MATURES;
}
i++;
if (isAtSea(hunterView->players[player]->location[0], hunterView) ||
isAtSea(hunterView->players[player]->
location[doubledBack (hunterView->players[player]->location[0], hunterView)], hunterView))
hunterView->players[player]->health -= LIFE_LOSS_SEA;
hunterView->score -= SCORE_LOSS_DRACULA_TURN;
} else {
int l = 0;
while (l < 3) {
if (pastPlays[i] == 'T') {
hunterView->players[player]->health -= LIFE_LOSS_TRAP_ENCOUNTER;
} else if (pastPlays[i] == 'V') {
//immature vampire encountered (slay vampire)
} else if (pastPlays[i] == 'D') {
hunterView->players[PLAYER_DRACULA]->health -= LIFE_LOSS_HUNTER_ENCOUNTER;
hunterView->players[player]->health -= LIFE_LOSS_DRACULA_ENCOUNTER;
if (hunterView->players[PLAYER_DRACULA]->health < 0) hunterView->players[PLAYER_DRACULA]->health = 0;
}
i++;
l++;
}
if (hunterView->players[player]->health <= 0) {
//teleport to hospital
hunterView->score -= SCORE_LOSS_HUNTER_HOSPITAL;
hunterView->players[player]->health = GAME_START_HUNTER_LIFE_POINTS;
hunterView->players[player]->hospital = TRUE;
}
}
/********THING TO MOVE*********/
if ((z == hunterView->players[player]->location[1]) &&
(player != PLAYER_DRACULA) && isInCity(player,hunterView)) {
hunterView->players[player]->health += LIFE_GAIN_REST;
if (hunterView->players[player]->health > GAME_START_HUNTER_LIFE_POINTS)
hunterView->players[player]->health = GAME_START_HUNTER_LIFE_POINTS;
}
/********THING TO MOVE*********/
i++; //to skip trailing dot
if (pastPlays[i] == ' ') i++;
//TODO something when run dracula out of health?
//still need to consider locations and traps/immaturevamps
}
if (hunterView->score < 0) hunterView->score = 0;
hunterView->round = (i + 1) / CHARS_PER_ROUND; //auto rounds down to remove incomplete rounds
hunterView->currentPlayer = ((i + 1) / CHARS_PER_TURN) % NUM_PLAYERS;
int amt_mess = (int) sizeof(messages)/sizeof(playerMessage);
for (i = 0; i < amt_mess; i++) {
for (j = 0; j < MESSAGE_SIZE-1; j++){
if (messages[i][j] == '\0') { hunterView->messages[i][j] = messages[i][j]; break; }
hunterView->messages[i][j] = messages[i][j];
}
}
hunterView->amtMess = amt_mess;
return hunterView;
}
// this function frees all memory previously allocated for the HunterView
// toBeDeleted. toBeDeleted should not be accessed after the call.
void disposeHunterView( HunterView toBeDeleted ) {
assert(toBeDeleted != NULL);
int i;
for (i = 0; i < NUM_PLAYERS; i++) free(toBeDeleted->players[i]);
free( toBeDeleted );
}
//Functions to return simple information about the current state of the game
//Get the current round
Round getRound (HunterView currentView) {
return currentView->round + getCurrentPlayer(currentView);
}
//Get the id of current player - ie whose turn is it?
// Only returns a 'playerID' which is one of:
// LORD_GODALMING (0): Lord Godalming's turn
// DR_SEWARD (1): Dr. Seward's turn
// VAN_HELSING (2): Van Helsing's turn
// MINA_HARKER (3): Mina Harker's turn
// DRACULA (4): Dracula's turn
PlayerID getCurrentPlayer (HunterView currentView) {
assert(currentView->currentPlayer >= PLAYER_LORD_GODALMING && currentView->currentPlayer <= PLAYER_DRACULA);
return currentView->currentPlayer;
}
//Get the current score
// Returns a positive integer [0...366]
int getScore(HunterView currentView) {
assert(currentView->score >= 0);
return currentView->score;
}
//Get the current health points for a given player
// 'player' specifies which players's life/blood points to return
// and must be a value in the interval [0...4] (see 'player' type)
int getHealth(HunterView currentView, PlayerID player) {
assert(player >= PLAYER_LORD_GODALMING && player <= PLAYER_DRACULA);
return currentView->players[player]->health;
}
// Get the current location id of a given player
// May be UNKNOWN_LOCATION if the player has not had a turn yet
// (ie at the beginning of the game when the round is 0)
// otherwise for a hunter it should be an integer in the interval [0..70]
// The given roundNumber is >= 0 and <= the current round number
// 'whichHunter' specifies which hunter's location to return
// and must be a value in the interval [0...3] (see 'player' type)
// Or for dracula it should
// gets the location of Dracula at the start of a particular round
// Returns an integer:
// in the interval [0...70] if Dracula was known to be in a city or sea
// CITY_UNKNOWN if Dracula was in an unknown city
// SEA_UNKNOWN if Dracula was in an unknown sea
// HIDE if Dracula was known to have made a hide move
// DOUBLE_BACK_N where N is [0...5], if Dracula was known to have
// made a double back move N positions back in the trail
// e.g. DOUBLE_BACK_1 is the last place place he visited
// TELEPORT if Dracula apparated back to Castle Dracula
// LOCATION_UNKNOWN if the round number is 0
LocationID getLocation(HunterView currentView, PlayerID player) {
if (currentView->players[player]->hospital) return ST_JOSEPH_AND_ST_MARYS;
return currentView->players[player]->location[0];
}
//Functions that return information about the history of the game
// Fills the trail array with the location ids of the last 6 turns for the given player
// For dracula this may include integers:
// in the interval [0...70] if Dracula was known to be in a city or sea
// CITY_UNKNOWN if Dracula was in an unknown city
// SEA_UNKNOWN if Dracula was in an unknown sea
// HIDE if Dracula was known to have made a hide move
// DOUBLE_BACK_N where N is [0...5], if Dracula was known to have
// made a double back move N positions back in the trail
// e.g. DOUBLE_BACK_1 is the last place place he visited
// TELEPORT if Dracula apparated back to Castle Dracula
// For any player if the move does not exist yet (i.e, the start of the game),
// the value should be UNKNOWN_LOCATION (-1)
// For example after 2 turns the array may have the contents
// {29, 182, -1, -1, -1, -1}
// This would mean in the first move the player started on location 182
// then moved to the current location of 29
void getHistory (HunterView currentView, PlayerID player,LocationID trail[TRAIL_SIZE]) {
int i;
for (i = 0; i < TRAIL_SIZE; i++) trail[i] = currentView->players[player]->location[i];
}
//Functions that query the map to find information about connectivity
//This function returns an array of LocationID that represent all locations that are connected
//to the given LocationID.
//road, rail and sea are connections should only be considered if the road, rail, sea parameters
//are TRUE.
//The size of the array should be stored in the variable pointed to by numLocations
//The array can be in any order but must contain unique entries
//Your function must take into account the round and player id for rail travel
//Your function must take into account that dracula can't move to the hospital or travel by rail
//but need not take into account draculas trail
//The destination 'from' should be included.
LocationID * connectedLocations(int * numLocations, LocationID from,
PlayerID player, Round round, int type, Graph g) {
int road = FALSE, rail = FALSE, sea = FALSE;
if (type == 0) road = TRUE;
if (type == 1) rail = TRUE;
if (type == 2) sea = TRUE;
if (type == 3) { road = TRUE; rail = TRUE; sea = TRUE; }
//conditions that need to be considered
LocationID to_search = NUM_MAP_LOCATIONS;
int moves_allowed = round % 4;
if (!moves_allowed) rail = FALSE;
if (player == PLAYER_DRACULA) rail = FALSE;
if (!sea) to_search = ZURICH + 1; //Zurich is the last city
int i;
*numLocations = 0;
LocationID *connected;//[NUM_MAP_LOCATIONS];
connected = malloc(sizeof(LocationID)*NUM_MAP_LOCATIONS);
int locationsFound[NUM_MAP_LOCATIONS]; //using this later
for (i = 0; i < NUM_MAP_LOCATIONS; i++) {
connected[i] = -1;
locationsFound[i] = FALSE;
}
i = 0;
int found;
while (i < to_search) {
found = FALSE;
if (road) {
//don't need to check for duplicates here, all connections will be uninitialized
if (isAdjacent(g,from, i, ROAD)) {
connected[*numLocations] = i;
(*numLocations)++;
found = TRUE;
}
}
if ((sea)&&(!found)) {
if (isAdjacent(g,from, i, SEA)) {
connected[*numLocations] = i;
(*numLocations)++;
found = TRUE;
}
}
if (found) locationsFound[i] = TRUE;
i++;
}
if (rail) {
//now we consider being able to move further by train
//only do the check for the further cities if the condition of mod 4 is met
//check places within two moves
LocationID connected_by_rail[NUM_MAP_LOCATIONS];
for (i = 0; i < NUM_MAP_LOCATIONS; i++) connected_by_rail[i] = FALSE;
canReachInN(g, from, RAIL, moves_allowed, connected_by_rail);
int j = 0;
while (j < NUM_MAP_LOCATIONS) {
if (!locationsFound[j]) {
if (connected_by_rail[j]) {
connected[*numLocations] = j;
(*numLocations)++;
locationsFound[j] = TRUE;
}
}
j++;
}
}
if (!locationsFound[from]) {
connected[*numLocations] = from;
(*numLocations)++;
}
return connected;
}
LocationID getLatestMessageLoc(HunterView currentView) {
int i, j;
char *locations[] = {
"AL", "AM", "AT", "BA", "BI", "BE", "BR", "BO", "BU", "BC",
"BD", "CA", "CG", "CD", "CF", "CO", "CN", "DU", "ED", "FL",
"FR", "GA", "GW", "GE", "GO", "GR", "HA", "JM", "KL", "LE",
"LI", "LS", "LV", "LO", "MA", "MN", "MR", "MI", "MU", "NA",
"NP", "NU", "PA", "PL", "PR", "RO", "SA", "SN", "SR", "SJ",
"SO", "ST", "SW", "SZ", "TO", "VA", "VR", "VE", "VI", "ZA",
"ZU", "NS", "EC", "IS", "AO", "BB", "MS", "TS", "IO", "AS",
"BS", "C?", "S?", "HI", "D1", "D2", "D3", "D4", "D5", "TP"
};
for (i = 0; i < currentView->amtMess; i++) { //find latest message of location
if (strcmp(currentView->messages[i], "") == 0) { //location is known
for (j = 0; j < NUM_LOCATIONS; j++) if (locations[j] == currentView->messages[i]) return j;
}
}
return UNKNOWN_LOCATION;
}
static int isAtSea(LocationID location, HunterView hunterView) {
return (((location >= NORTH_SEA)&&(location <= BLACK_SEA))||(location == SEA_UNKNOWN ));
}
static int isInCity(LocationID location, HunterView hunterView) {
return (((location >= ALICANTE)&&(location <= ZURICH))||(location == CITY_UNKNOWN ));
}
static int doubledBack (LocationID location, HunterView hunterView) {
if ((location >= DOUBLE_BACK_1)&&(location <= DOUBLE_BACK_5))
return location - DOUBLE_BACK_1 + 1;
else return FALSE;
}