static void populateStruct(DracView dv, char *pastPlays, PlayerMessage messages[]) { dv->past = malloc(strlen(pastPlays)); strncpy(dv->past, pastPlays, strlen(pastPlays)); dv->pastPlaysLength = strlen(pastPlays); // Index of each player's information within pastPlays dv->pastPlaysIndex[PLAYER_LORD_GODALMING] = 0; dv->pastPlaysIndex[PLAYER_DR_SEWARD] = 8; dv->pastPlaysIndex[PLAYER_VAN_HELSING] = 16; dv->pastPlaysIndex[PLAYER_MINA_HARKER] = 24; dv->pastPlaysIndex[PLAYER_DRACULA] = 32; // Uncomment if needed in the future // dv->msgs = malloc(strlen(messages)); // strncpy(dv->msgs, messages, strlen(messages); //char *pastStr = malloc(strlen(pastPlays)); //strncpy(pastStr, pastPlays, strlen(pastPlays)); //dv->past = pastStr; //strncpy(dv->msgs, *messages, strlen(*messages)); dv->gv = newGameView(pastPlays, messages); dv->round = getRound(dv->gv); }
// Creates a new HunterView to summarise the current state of the game HunterView newHunterView(char *pastPlays, PlayerMessage messages[]) { //REPLACE THIS WITH YOUR OWN IMPLEMENTATION HunterView hunterView = malloc(sizeof(struct hunterView)); hunterView->game = newGameView(pastPlays, messages); return hunterView; }
// 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); hunterView->g = newGameView(pastPlays,messages); int rounds = giveMeTheRound(hunterView); hunterView->messages = malloc(sizeof(PlayerMessage)*rounds); int i; for (i=0;i<rounds;i++) { strncpy(hunterView->messages[i], messages[i], MESSAGE_SIZE); } return hunterView; }
// Creates a new DracView to summarise the current state of the game DracView newDracView(char *pastPlays, PlayerMessage messages[]) { DracView dracView = malloc(sizeof(struct dracView)); dracView->g = newGameView(pastPlays, messages); dracView->vampLocation = NOWHERE; dracView->traps = newTrail(TRAIL_SIZE); // If pastPlays is empty, we just return early. if (strcmp(pastPlays, "") == 0) { return dracView; } char *currentPlayMarker = pastPlays; char currentPlay[8] = {'\0'}; char givenLocation[3] = {'\0'}; LocationID currentPlace = NOWHERE; while (currentPlayMarker != NULL) { memcpy(currentPlay, currentPlayMarker, 8); // We isolate the location part for easy access.. memcpy(givenLocation, currentPlay + 1, 2); currentPlace = abbrevToID(givenLocation); if (currentPlay[0] == 'D') { // Then we parse each of the next four characters. for (int i = 3; i < 6; i++) { if (i == 3 || i == 4) { switch (currentPlay[i]) { // TRAP PLACED case 'T': prepend(dracView->traps, currentPlace); break; // VAMPIRE PLACED case 'V': dracView->vampLocation = currentPlace; break; // NOTHING default: break; } } else if (i == 5) { switch (currentPlay[i]) { // TRAP LEAVES case 'M': while (showElement(dracView->traps, TRAIL_SIZE - 1) != NOWHERE) { prepend(dracView->traps, NOWHERE); } prepend(dracView->traps, NOWHERE); break; // VAMPIRE MATURES case 'V': dracView->vampLocation = NOWHERE; break; // NOTHING default: break; } } } } else { // Then we parse each of the next four characters. for (int i = 3; i < 7; i++) { switch (currentPlay[i]) { // TRAP FOUND case 'T': removeElement(dracView->traps, abbrevToID(givenLocation)); break; // VAMPIRE FOUND case 'V': dracView->vampLocation = NOWHERE; break; // NOTHING default: break; } } } if (currentPlay[7] == '\0') break; currentPlayMarker += 8; } return dracView; }
int main() { int i; GameView gv; printf("Test basic empty initialisation\n"); PlayerMessage messages1[] = {}; gv = newGameView("", messages1); assert(getCurrentPlayer(gv) == PLAYER_LORD_GODALMING); assert(getRound(gv) == 0); assert(getHealth(gv,PLAYER_DR_SEWARD) == GAME_START_HUNTER_LIFE_POINTS); assert(getHealth(gv,PLAYER_DRACULA) == GAME_START_BLOOD_POINTS); assert(getScore(gv) == GAME_START_SCORE); assert(getLocation(gv,PLAYER_LORD_GODALMING) == UNKNOWN_LOCATION); printf("passed\n"); disposeGameView(gv); printf("Test for Dracula trail and basic functions\n"); PlayerMessage messages2[] = {"Hello","Rubbish","Stuff","","Mwahahah"}; gv = newGameView("GST.... SAO.... HZU.... MBB.... DC?....", messages2); assert(getCurrentPlayer(gv) == PLAYER_LORD_GODALMING); assert(getRound(gv) == 1); assert(getLocation(gv,PLAYER_LORD_GODALMING) == STRASBOURG); assert(getLocation(gv,PLAYER_DR_SEWARD) == ATLANTIC_OCEAN); assert(getLocation(gv,PLAYER_VAN_HELSING) == ZURICH); assert(getLocation(gv,PLAYER_MINA_HARKER) == BAY_OF_BISCAY); assert(getLocation(gv,PLAYER_DRACULA) == CITY_UNKNOWN); assert(getHealth(gv,PLAYER_DRACULA) == GAME_START_BLOOD_POINTS); printf("passed\n"); disposeGameView(gv); printf("Test for encountering Dracula and hunter history\n"); PlayerMessage messages3[] = {"Hello","Rubbish","Stuff","","Mwahahah","Aha!"}; gv = newGameView("GST.... SAO.... HCD.... MAO.... DGE.... GGED...", messages3); assert(getLocation(gv,PLAYER_DRACULA) == GENEVA); assert(getHealth(gv,PLAYER_LORD_GODALMING) == 5); assert(getHealth(gv,PLAYER_DRACULA) == 30); assert(getLocation(gv,PLAYER_LORD_GODALMING) == GENEVA); LocationID history[TRAIL_SIZE]; getHistory(gv,PLAYER_DRACULA,history); assert(history[0] == GENEVA); assert(history[1] == UNKNOWN_LOCATION); getHistory(gv,PLAYER_LORD_GODALMING,history); assert(history[0] == GENEVA); assert(history[1] == STRASBOURG); assert(history[2] == UNKNOWN_LOCATION); getHistory(gv,PLAYER_DR_SEWARD,history); assert(history[0] == ATLANTIC_OCEAN); assert(history[1] == UNKNOWN_LOCATION); printf("passed\n"); disposeGameView(gv); printf("Test for Dracula doubling back at sea, and losing blood points (Hunter View)\n"); PlayerMessage messages4[] = {"Hello","Rubbish","Stuff","","Mwahahah","Aha!","","","","Back I go"}; gv = newGameView("GGE.... SGE.... HGE.... MGE.... DS?.... " "GST.... SST.... HST.... MST.... DD1....", messages4); assert(getLocation(gv,PLAYER_DRACULA) == DOUBLE_BACK_1); getHistory(gv,PLAYER_DRACULA,history); assert(history[0] == DOUBLE_BACK_1); assert(history[1] == SEA_UNKNOWN); assert(getHealth(gv,PLAYER_DRACULA) == GAME_START_BLOOD_POINTS - 4); assert(getCurrentPlayer(gv) == 0); printf("passed\n"); disposeGameView(gv); printf("Test for Dracula doubling back at sea, and losing blood points (Drac View)\n"); PlayerMessage messages5[] = {"Hello","Rubbish","Stuff","","Mwahahah","Aha!","","","","Back I go"}; gv = newGameView("GGE.... SGE.... HGE.... MGE.... DEC.... " "GST.... SST.... HST.... MST.... DD1....", messages5); assert(getLocation(gv,PLAYER_DRACULA) == DOUBLE_BACK_1); getHistory(gv,PLAYER_DRACULA,history); assert(history[0] == DOUBLE_BACK_1); assert(history[1] == ENGLISH_CHANNEL); assert(getHealth(gv,PLAYER_DRACULA) == GAME_START_BLOOD_POINTS - 4); assert(getCurrentPlayer(gv) == 0); printf("passed\n"); disposeGameView(gv); printf("Test for connections\n"); int size, seen[NUM_MAP_LOCATIONS], *edges; gv = newGameView("", messages1); printf("Checking Galatz road connections\n"); edges = connectedLocations(gv,&size,GALATZ,PLAYER_LORD_GODALMING,0,1,0,0); memset(seen, 0, NUM_MAP_LOCATIONS*sizeof(int)); for (i = 0; i< size ; i++) seen[edges[i]] = 1; assert(size == 5); assert(seen[GALATZ]); assert(seen[CONSTANTA]); assert(seen[BUCHAREST]); assert(seen[KLAUSENBURG]); assert(seen[CASTLE_DRACULA]); free(edges); printf("Checking Ionian Sea sea connections\n"); edges = connectedLocations(gv,&size,IONIAN_SEA,PLAYER_LORD_GODALMING,0,0,0,1); memset(seen, 0, NUM_MAP_LOCATIONS*sizeof(int)); for (i = 0; i < size; i++) seen[edges[i]] = 1; assert(size == 7); assert(seen[IONIAN_SEA]); assert(seen[BLACK_SEA]); assert(seen[ADRIATIC_SEA]); assert(seen[TYRRHENIAN_SEA]); assert(seen[ATHENS]); assert(seen[VALONA]); assert(seen[SALONICA]); free(edges); printf("Checking Athens rail connections (none)\n"); edges = connectedLocations(gv,&size,ATHENS,PLAYER_LORD_GODALMING,0,0,1,0); assert(size == 1); assert(edges[0] == ATHENS); free(edges); printf("passed\n"); disposeGameView(gv); return 0; }
// Creates a new HunterView to summarise the current state of the game HunterView newHunterView(char *pastPlays, PlayerMessage messages[]) { assert(pastPlays != NULL); assert(messages != NULL); // malloc HunterView hunterView = malloc(sizeof(struct hunterView)); assert(hunterView != NULL); // setup the gameview hunterView->g = newGameView(pastPlays, messages); assert(hunterView->g != NULL); int i; // initialise Dracula's actual locations to all UNKNOWN_LOCATION for(i=0;i<TRAIL_SIZE;i++) { hunterView->trailLocs[i] = UNKNOWN_LOCATION; } // length of pastPlays int pastPlaysLength = strnlen(pastPlays, MAX_PAST_PLAYS_LENGTH); // iterate over each turn and process for(i=0;i<pastPlaysLength;i+=CHARS_PER_PLAY_BLOCK) { // get curPlayer PlayerID curPlayer = ( (i/CHARS_PER_PLAY_BLOCK) % NUM_PLAYERS); // ensure it's dracula if(curPlayer == PLAYER_DRACULA) { // try to get current loc (if it's exact) LocationID curLoc = abbrevToID(pastPlays+i+LOC_ABBREV_INDEX); // fprintf(stderr,"here = %.7s curLoc = %d\n",pastPlays+i,curLoc); // test if exact location if(!validPlace(curLoc)) { // not exact; try to use trail to work out where we really are // either a HIDE, DOUBLE_BACK_ or TELEPORT if(pastPlays[i+LOC_ABBREV_INDEX] == 'H') { // HIDE: go to most recent location curLoc = hunterView->trailLocs[LAST_TRAIL_LOC_INDEX]; } else if(pastPlays[i+LOC_ABBREV_INDEX] == 'D') { // DOUBLE BACK int numBack = (int)(pastPlays[i+LOC_ABBREV_INDEX+1]-'0'); curLoc = hunterView->trailLocs[numBack]; } else if(pastPlays[i+LOC_ABBREV_INDEX] == 'T') { // TELEPORT back to CASTLE_DRACULA curLoc = CASTLE_DRACULA; } else if(pastPlays[i+LOC_ABBREV_INDEX] == 'C') { curLoc = CITY_UNKNOWN; } else if(pastPlays[i+LOC_ABBREV_INDEX] == 'S') { curLoc = SEA_UNKNOWN; } else { curLoc = UNKNOWN_LOCATION; } } // push curLoc onto the trail pushOnTrailLocs(hunterView, curLoc); } } return hunterView; }