Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////
- // COMP2521 18x1 ... the Fury of Dracula
- // GameView.c: GameView ADT implementation
- //
- // 2014-07-01 v1.0 Team Dracula <cs2521@cse.unsw.edu.au>
- // 2017-12-01 v1.1 Team Dracula <cs2521@cse.unsw.edu.au>
- #include <assert.h>
- #include <err.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <sysexits.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include "Game.h"
- #include "GameView.h"
- #include "Globals.h"
- #include "Map.h"
- //#include "Places.h"
- // ... if you decide to use the Map ADT
- #define MAX_SIZE 1000
- #define LINE_LEN 40
- #define PLAY_LEN 8
- static char playerToChar(PlayerID player);
- struct gameView {
- Round roundNumber;
- int currPlayer;
- int currScore;
- //pList players[NUM_PLAYERS];
- int godLife;
- int drsLife;
- int vanLife;
- int minLife;
- int draLife;
- int godLoca;
- int drsLoca;
- int vanLoca;
- int minLoca;
- int draLoca;
- char *pastP;
- Map dracMap;
- };
- static char playerToChar(PlayerID player);
- static void pushToTrail(LocationID trail[TRAIL_SIZE], LocationID location);
- static int latestPlay(GameView gv);
- static int pastPlaysLength(GameView gv){
- char *string = gv->pastP;
- int i;
- for (i = 0; string[i] != '\0'; i++);
- return i;
- }
- // Creates a new GameView to summarise the current state of the game
- GameView
- newGameView (char *pastPlays, PlayerMessage messages[])
- {
- GameView new = malloc (sizeof *new);
- if (new == NULL) err (EX_OSERR, "couldn't allocate GameView");
- new->pastP = pastPlays;
- new->godLife = GAME_START_HUNTER_LIFE_POINTS;
- new->drsLife = GAME_START_HUNTER_LIFE_POINTS;
- new->vanLife = GAME_START_HUNTER_LIFE_POINTS;
- new->minLife = GAME_START_HUNTER_LIFE_POINTS;
- new->draLife = GAME_START_BLOOD_POINTS;
- new->godLoca = UNKNOWN_LOCATION;
- new->drsLoca = UNKNOWN_LOCATION;
- new->vanLoca = UNKNOWN_LOCATION;
- new->minLoca = UNKNOWN_LOCATION;
- new->draLoca = UNKNOWN_LOCATION;
- new->roundNumber = 0;
- new->currPlayer = 0;
- new->currScore = GAME_START_SCORE;
- new->dracMap = newMap();
- return new;
- }
- // Frees all memory previously allocated for the GameView toBeDeleted
- void
- disposeGameView (GameView toBeDeleted)
- {
- // COMPLETE THIS IMPLEMENTATION
- free (toBeDeleted);
- }
- //// Functions to return simple information about the current state of the game
- // Get the current round
- Round
- getRound (GameView gv)
- {
- if (strcmp(gv->pastP, "\0") == 0) return 0;
- int roundCount = 0;
- for (int i = 0; gv->pastP[i] != '\0'; i++){
- if (i % (LINE_LEN-1) == 0){
- roundCount++;
- }
- }
- gv->roundNumber = roundCount;
- return roundCount;
- }
- // Get the id of current player - ie whose turn is it?
- PlayerID
- getCurrentPlayer (GameView gv)
- {
- int pastPlaysLen = pastPlaysLength(gv), currPlayer = 0;
- for (int i = 0; i < pastPlaysLen; i++){
- if (i % 8 == 0){
- currPlayer++;
- if (currPlayer > 4){
- currPlayer = 0;
- }
- }
- }
- return currPlayer;
- }
- // Get the current score
- int
- getScore (GameView gv)
- {
- if (strcmp(gv -> pastP, "\0") == 0){
- return GAME_START_SCORE;
- }
- /* int i = 35;
- int dracTurnCount = 0;
- while (i < pastPlaysLength(gv)){
- dracTurnCount++;
- i = i+LINE_LEN;
- }*/
- return 0;
- }
- // Get the current health points for a given player
- int
- getHealth (GameView gv, PlayerID player)
- {
- int turnCount = 0;
- int i = 3;
- int playerAdd = 0;
- int playerHealth = 0;
- if (player == PLAYER_LORD_GODALMING) {
- playerAdd = 3;
- playerHealth = gv->godLife;
- }
- if (player == PLAYER_DR_SEWARD){
- playerAdd = 11;
- playerHealth = gv->drsLife;
- }
- if (player == PLAYER_VAN_HELSING){
- playerAdd = 19;
- playerHealth = gv->vanLife;
- }
- if (player == PLAYER_MINA_HARKER){
- playerAdd = 27;
- playerHealth = gv->minLife;
- }
- if (player == PLAYER_DRACULA){
- playerAdd = 3;
- playerHealth = gv->draLife;
- }
- if (player != PLAYER_DRACULA) {
- while (turnCount < getRound(gv)){
- while (i < 5){
- if (gv->pastP[playerAdd+i] == 'D'){
- playerHealth = playerHealth - 4;
- }
- i++;
- }
- turnCount++;
- playerAdd = playerAdd+39;
- i = 0;
- }
- return playerHealth;
- }
- else if (player == PLAYER_DRACULA){
- int i = 0;
- char *string = gv->pastP;
- while ((playerAdd + i) < pastPlaysLength(gv)){
- while (i < 5){
- if (string[playerAdd + i] == 'D'){
- playerHealth = playerHealth - 10;
- printf("drac encounter\n");
- }
- i++;
- }
- i = 0;
- playerAdd = playerAdd+8;
- turnCount++;
- }
- int m = 33;
- char loca[2] = {0};
- loca[2] = '\0';
- int prevLocaSea = 0;
- while (m < pastPlaysLength(gv)){
- loca[0] = gv->pastP[m];
- loca[1] = gv->pastP[m+1];
- char *b = loca;
- PlaceType z = UNKNOWN;
- LocationID y = abbrevToID(b);
- if (validPlace(y)) z = idToType(y);
- if (loca[0] == 'S' && loca[1] == '?'){
- playerHealth = playerHealth - 2;
- prevLocaSea = 1;
- }else if (z == SEA){
- playerHealth = playerHealth - 2;
- prevLocaSea = 1;
- }else if (loca[0] == 'D' && (loca[1] >= 1)){
- if (prevLocaSea == 1) playerHealth = playerHealth - 2;
- }else{
- prevLocaSea = 0;
- }
- m = m+LINE_LEN;
- }
- return playerHealth;
- }
- return 0;
- }
- // Get the current location id of a given player
- LocationID
- getLocation (GameView gv, PlayerID player)
- {
- int currPlayer = getCurrentPlayer(gv);
- char loca[2] = {0};
- loca[2] = '\0';
- int i = 1;
- if (strcmp(gv->pastP, "\0") == 0){
- return UNKNOWN_LOCATION;
- }
- if (player == PLAYER_LORD_GODALMING){
- loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 1];
- loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 2];
- }
- if (player == PLAYER_DR_SEWARD){
- if (currPlayer <= 1 && getRound(gv)>1) i = 2;
- loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 9];
- loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 10];
- }
- if (player == PLAYER_VAN_HELSING){
- if (currPlayer <= 2 && getRound(gv)>1) i = 2;
- loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 17];
- loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 18];
- }
- if (player == PLAYER_MINA_HARKER){
- if (currPlayer <= 3 && getRound(gv)>1) i = 2;
- loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 25];
- loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 26];
- }
- if (player == PLAYER_DRACULA){
- if (currPlayer <= 4 && getRound(gv)>1) i = 2;
- loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 33];
- loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 34];
- }
- //printf("%d is getround\n", getRound(gv));
- //printf("%s is loca\n", loca);
- char *b = loca;
- LocationID m = abbrevToID(b);
- if (player == PLAYER_DRACULA){
- if (validPlace(m)) {
- return m;
- }
- if (loca[0] == 'C' && loca[1] == '?') return CITY_UNKNOWN;
- if (loca[0] == 'S' && loca[1] == '?') return SEA_UNKNOWN;
- if (loca[0] == 'H' && loca[1] == 'I') return HIDE;
- if (loca[0] == 'D' && loca[1] == '1') return DOUBLE_BACK_1;
- if (loca[0] == 'D' && loca[1] == '2') return DOUBLE_BACK_2;
- if (loca[0] == 'D' && loca[1] == '3') return DOUBLE_BACK_3;
- if (loca[0] == 'D' && loca[1] == '4') return DOUBLE_BACK_4;
- if (loca[0] == 'D' && loca[1] == '5') return DOUBLE_BACK_5;
- if (loca[0] == 'T' && loca[1] == 'P') return TELEPORT;
- else return UNKNOWN_LOCATION;
- }
- return m;
- }
- //// Functions that return information about the history of the game
- // Fills the trail array with the location ids of the last 6 turns
- void
- getHistory (GameView gv, PlayerID player, LocationID trail[TRAIL_SIZE])
- {
- //fill trail with -1 to begin with
- for(int j = 0; j < TRAIL_SIZE; j++){
- trail[j] = UNKNOWN_LOCATION;
- }
- //find latest play index in pastP
- int lastPlay = latestPlay(gv);
- char tempPlayer = playerToChar(player);
- //lastPlay = lastPlay-6;
- //find latest play of our current player
- while(gv->pastP[lastPlay] != tempPlayer){
- lastPlay = lastPlay - PLAY_LEN;
- }//index should now be at specified player
- //Dracula case:---------------
- if(player == PLAYER_DRACULA){
- int count = 0;
- while(count < TRAIL_SIZE){
- if(lastPlay <= 0){//fills in rest of trail with unknown loc
- count = TRAIL_SIZE;//exit while loop
- } else {
- char tempLoc[2];//temporary location
- tempLoc[0] = gv->pastP[lastPlay+1];
- tempLoc[1] = gv->pastP[lastPlay+2];
- LocationID trailAdd;
- if(tempLoc[0]=='C' && tempLoc[1]=='?'){//if unknown city location
- trailAdd = CITY_UNKNOWN;
- } else if(tempLoc[0]=='S' && tempLoc[1]=='?'){//if unknown sea location
- trailAdd = SEA_UNKNOWN;
- } else if(tempLoc[0]=='H' && tempLoc[1]=='I'){//if hide move
- trailAdd = HIDE;
- } else if(tempLoc[0]=='T' && tempLoc[1]=='P'){//if teleport move
- trailAdd = TELEPORT;
- }
- else if(tempLoc[0]=='D' && tempLoc[1]=='1'){ //all below are doubleback
- trailAdd = DOUBLE_BACK_1;
- } else if(tempLoc[0]=='D' && tempLoc[1]=='2'){
- trailAdd = DOUBLE_BACK_2;
- } else if(tempLoc[0]=='D' && tempLoc[1]=='3'){
- trailAdd = DOUBLE_BACK_3;
- } else if(tempLoc[0]=='D' && tempLoc[1]=='4'){
- trailAdd = DOUBLE_BACK_4;
- } else if(tempLoc[0]=='D' && tempLoc[1]=='5'){
- trailAdd = DOUBLE_BACK_5;
- }
- else {//known location
- trailAdd = abbrevToID(tempLoc);
- }
- pushToTrail(trail, trailAdd);//add to trail
- lastPlay = lastPlay - LINE_LEN;
- count++;
- }
- }//--------------
- } else {
- //hunter case:
- //now we fill in the trail, finally.
- int countr = 0;
- while(countr < TRAIL_SIZE){
- if(lastPlay < 0){
- countr = TRAIL_SIZE;
- } else {
- char currLoc[2];//current location
- //extract location abbreviation
- currLoc[0] = gv->pastP[lastPlay+1];
- currLoc[1] = gv->pastP[lastPlay+2];
- //turn location abbreviation to locationID using places.c
- LocationID currLocation = abbrevToID(currLoc);
- //finally, add locationID to trail
- pushToTrail(trail, currLocation);
- lastPlay = lastPlay - LINE_LEN;//go back to the previous round
- countr++;//will only go to 6 to ensure trail is at most last 6 plays.
- }
- }
- //after all of this the trail array should be updated for
- //whichever player was parsed into the function.
- }
- }
- //// Functions that query the map to find information about connectivity
- // Returns an array of LocationIDs for all directly connected locations
- LocationID *
- connectedLocations (GameView gv, int *numLocations,
- LocationID from, PlayerID player, Round round,
- bool road, bool rail, bool sea)
- {
- //int numConn = numberOfConnections(gv->dracMap, from);
- //printf("numConn = %d\n", numConn);
- //*numLocations = numConn;
- //LocationID locArray[50];
- //printf("%d is locArray\n", locArray[0]);
- LocationID *b;
- b = locationsConnected(gv->dracMap, from, 100, road, rail, sea);
- int i = 0;
- int connCount = numberOfConnections(gv->dracMap, from, road, rail, sea);
- //printf("%d is connCount\n", connCount);
- while (i < connCount){
- //printf("%s is id----------\n", idToName(b[i]));
- i++;
- }
- *numLocations = connCount;
- //LocationID locArr[numLocations];
- // REPLACE THIS WITH YOUR OWN IMPLEMENTATION
- return b;
- }
- static void pushToTrail(LocationID trail[TRAIL_SIZE], LocationID location){
- assert(trail != NULL);
- int i = 0;
- while(i < TRAIL_SIZE){
- if(trail[i] == UNKNOWN_LOCATION){
- trail[i] = location;
- i = TRAIL_SIZE;
- } else {
- i++;
- }
- }
- }
- static int latestPlay(GameView gv){
- int i = pastPlaysLength(gv);
- i -= (PLAY_LEN - 1);
- return i;
- }
- static char playerToChar(PlayerID player){//helper function
- char playerChar;
- if(player == PLAYER_DRACULA){
- playerChar = 'D';
- } else if (player == PLAYER_LORD_GODALMING){
- playerChar = 'G';
- } else if (player == PLAYER_DR_SEWARD){
- playerChar = 'S';
- } else if (player == PLAYER_VAN_HELSING){
- playerChar = 'H';
- } else /*player == PLAYER_MINA_HARKER*/{
- playerChar = 'M';
- }
- return playerChar;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement