Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.03 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////
  2. // COMP2521 18x1 ... the Fury of Dracula
  3. // GameView.c: GameView ADT implementation
  4. //
  5. // 2014-07-01   v1.0    Team Dracula <cs2521@cse.unsw.edu.au>
  6. // 2017-12-01   v1.1    Team Dracula <cs2521@cse.unsw.edu.au>
  7.  
  8. #include <assert.h>
  9. #include <err.h>
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include <sysexits.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <stdbool.h>
  17.  
  18. #include "Game.h"
  19. #include "GameView.h"
  20. #include "Globals.h"
  21. #include "Map.h"
  22. //#include "Places.h"
  23. // ... if you decide to use the Map ADT
  24.  
  25. #define LINE_LEN 40
  26. #define PLAY_LEN 8
  27. static char playerToChar(PlayerID player);
  28.  
  29. struct gameView {
  30.     Round roundNumber;
  31.     int currPlayer;
  32.     int currScore;
  33.     //pList players[NUM_PLAYERS];
  34.    
  35.     int godLife;
  36.     int drsLife;
  37.     int vanLife;
  38.     int minLife;
  39.     int draLife;
  40.    
  41.     int godLoca;
  42.     int drsLoca;
  43.     int vanLoca;
  44.     int minLoca;
  45.     int draLoca;
  46.    
  47.     char *pastP;
  48.    
  49.     Map dracMap;   
  50. };
  51. static char playerToChar(PlayerID player);
  52. static void pushToTrail(LocationID trail[TRAIL_SIZE], LocationID location);
  53. static int latestPlay(GameView gv);
  54.  
  55. static int pastPlaysLength(GameView gv){
  56.     char *string = gv->pastP;
  57.     int i;
  58.     for (i = 0; string[i] != '\0'; i++);
  59.     return i;
  60. }
  61.  
  62. // Creates a new GameView to summarise the current state of the game
  63. GameView
  64. newGameView (char *pastPlays, PlayerMessage messages[])
  65. {
  66.     GameView new = malloc (sizeof *new);
  67.     if (new == NULL) err (EX_OSERR, "couldn't allocate GameView");
  68.    
  69.     new->pastP = pastPlays;
  70.        
  71.     new->godLife = GAME_START_HUNTER_LIFE_POINTS;
  72.     new->drsLife = GAME_START_HUNTER_LIFE_POINTS;
  73.     new->vanLife = GAME_START_HUNTER_LIFE_POINTS;
  74.     new->minLife = GAME_START_HUNTER_LIFE_POINTS;
  75.     new->draLife = GAME_START_BLOOD_POINTS;
  76.    
  77.     new->godLoca = UNKNOWN_LOCATION;
  78.     new->drsLoca = UNKNOWN_LOCATION;
  79.     new->vanLoca = UNKNOWN_LOCATION;
  80.     new->minLoca = UNKNOWN_LOCATION;
  81.     new->draLoca = UNKNOWN_LOCATION;
  82.    
  83.     new->roundNumber = 0;
  84.     new->currPlayer = 0;
  85.     new->currScore = GAME_START_SCORE;
  86.     new->dracMap = newMap();
  87.        
  88.     return new;
  89. }
  90.  
  91. // Frees all memory previously allocated for the GameView toBeDeleted
  92. void
  93. disposeGameView (GameView toBeDeleted)
  94. {
  95.  
  96.     //free(toBeDeleted->pastP);
  97.     free(toBeDeleted->dracMap);
  98.     free (toBeDeleted);
  99. }
  100.  
  101. //// Functions to return simple information about the current state of the game
  102.  
  103. // Get the current round
  104. Round
  105. getRound (GameView gv)
  106. {
  107.     if (strcmp(gv->pastP, "\0") == 0) return 0;
  108.     int roundCount = 0;
  109.     for (int i = 0; gv->pastP[i] != '\0'; i++){
  110.         if (i % (LINE_LEN-1) == 0){
  111.             roundCount++;
  112.         }      
  113.     }
  114.     gv->roundNumber = roundCount;      
  115.     return roundCount;
  116. }
  117.  
  118. // Get the id of current player - ie whose turn is it?
  119. PlayerID
  120. getCurrentPlayer (GameView gv)
  121. {  
  122.     int pastPlaysLen = pastPlaysLength(gv), currPlayer = 0;
  123.     for (int i = 0; i < pastPlaysLen; i++){
  124.         if (i % 8 == 0){
  125.             currPlayer++;
  126.             if (currPlayer > 4){
  127.                 currPlayer = 0;
  128.             }
  129.         }
  130.     }
  131.     return currPlayer;
  132. }
  133.  
  134. // Get the current score
  135. int
  136. getScore (GameView gv)
  137. {
  138. {
  139.     if (strcmp(gv->pastP, "\0") == 0){
  140.         return GAME_START_SCORE;
  141.     }
  142.     int result = GAME_START_SCORE;
  143.     int turnInt = 0;
  144.     int i = 0;
  145.     int vampCount = 0;
  146.     int vampTimer = 0;//once it reaches 6 vamp falls off trail
  147.     while(i <= pastPlaysLength(gv)){
  148.  
  149.         if(gv->pastP[i] == 'G'){
  150.             turnInt++;
  151.         } else if(gv->pastP[i] == 'S'){
  152.             turnInt++;
  153.         } else if(gv->pastP[i] == 'H'){
  154.             turnInt++;
  155.         } else if(gv->pastP[i] == 'M'){
  156.             turnInt++;
  157.         } else /*gv->pastP[i] == 'D'*/{
  158.             turnInt++;
  159.             //checking for vampires
  160.             if(gv->pastP[i+5] == 'V') vampCount++;
  161.             if(vampCount > 0) vampTimer++;
  162.         }
  163.        
  164.         if(turnInt % 5 == 0){
  165.             result -= SCORE_LOSS_DRACULA_TURN;
  166.         } else {//not Draculas turn
  167.             if(gv->pastP[i+1] == 'J' && gv->pastP[i+2] == 'M'){
  168.                 result -= SCORE_LOSS_HUNTER_HOSPITAL;//hunter taken to hospital
  169.             }
  170.         }
  171.         if(vampTimer > TRAIL_SIZE) result -= SCORE_LOSS_VAMPIRE_MATURES;
  172.         i++;
  173.     }
  174.     gv->currScore = result;
  175.     return gv->currScore;
  176. }
  177. }
  178.  
  179. // Get the current health points for a given player
  180. int
  181. getHealth (GameView gv, PlayerID player)
  182. {
  183.     int turnCount = 0;
  184.     int i = 3;
  185.     int playerAdd = 0;
  186.     int playerHealth = 0;
  187.     if (player == PLAYER_LORD_GODALMING) {
  188.         playerAdd = 3;
  189.         playerHealth = gv->godLife;
  190.     }
  191.     if (player == PLAYER_DR_SEWARD){
  192.         playerAdd = 11;
  193.         playerHealth = gv->drsLife;
  194.     }
  195.     if (player == PLAYER_VAN_HELSING){
  196.         playerAdd = 19;
  197.         playerHealth = gv->vanLife;
  198.     }
  199.     if (player == PLAYER_MINA_HARKER){
  200.         playerAdd = 27;
  201.         playerHealth = gv->minLife;
  202.     }
  203.     if (player == PLAYER_DRACULA){
  204.         playerAdd = 3;
  205.         playerHealth = gv->draLife;
  206.     }
  207.        
  208.     if (player != PLAYER_DRACULA) {
  209.         while (turnCount < getRound(gv)){
  210.             while (i < 5){
  211.                 if (gv->pastP[playerAdd+i] == 'D'){
  212.                     playerHealth = playerHealth - 4;
  213.                 }
  214.                 i++;
  215.             }
  216.             turnCount++;
  217.             playerAdd = playerAdd+39;
  218.             i = 0;
  219.         }
  220.         return playerHealth;
  221.     }
  222.    
  223.     else if (player == PLAYER_DRACULA){
  224.         int i = 0;
  225.         char *string = gv->pastP;
  226.         while ((playerAdd + i) < pastPlaysLength(gv)){
  227.             while (i < 5){
  228.                 if (string[playerAdd + i] == 'D'){
  229.                     playerHealth = playerHealth - 10;
  230.                 }
  231.                 i++;
  232.             }
  233.             i = 0;
  234.             playerAdd = playerAdd+8;
  235.             turnCount++;   
  236.         }
  237.         int m = 33;
  238.         char loca[2] = {0};
  239.         int prevLocaSea = 0;
  240.        
  241.         while (m < pastPlaysLength(gv)){
  242.             loca[0] = gv->pastP[m];
  243.             loca[1] = gv->pastP[m+1];
  244.             char *b = loca;
  245.             PlaceType z = UNKNOWN;
  246.             LocationID y = abbrevToID(b);
  247.             if (validPlace(y))  z = idToType(y);
  248.                
  249.             if (loca[0] == 'S' && loca[1] == '?'){
  250.                 playerHealth = playerHealth - 2;
  251.                 prevLocaSea = 1;
  252.             }else if (z == SEA){
  253.                 playerHealth = playerHealth - 2;
  254.                 prevLocaSea = 1;
  255.             }else if (loca[0] == 'D' && (loca[1] >= 1)){
  256.                 if (prevLocaSea == 1) playerHealth = playerHealth - 2;
  257.             }else if (loca[0] == 'C' && loca[1] == 'D'){
  258.                 playerHealth = playerHealth+10;
  259.                 if (playerHealth > 40)  playerHealth = GAME_START_BLOOD_POINTS;
  260.             }else{
  261.                 prevLocaSea = 0;
  262.             }
  263.                
  264.             m = m+LINE_LEN;
  265.         }
  266.            
  267.    
  268.         return playerHealth;
  269.     }
  270.  
  271.     return 0;
  272. }
  273.  
  274. // Get the current location id of a given player
  275. LocationID
  276. getLocation (GameView gv, PlayerID player)
  277. {
  278.     int currPlayer = getCurrentPlayer(gv);
  279.     char loca[2] = {0};
  280.     int i = 1;
  281.     if (strcmp(gv->pastP, "\0") == 0){
  282.         return UNKNOWN_LOCATION;
  283.     }
  284.     if (player == PLAYER_LORD_GODALMING){
  285.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 1];
  286.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 2];
  287.     }
  288.     if (player == PLAYER_DR_SEWARD){
  289.         if (currPlayer <= 1 && getRound(gv)>1) i = 2;
  290.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 9];
  291.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 10];
  292.     }
  293.     if (player == PLAYER_VAN_HELSING){
  294.         if (currPlayer <= 2 && getRound(gv)>1) i = 2;
  295.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 17];
  296.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 18];
  297.     }
  298.     if (player == PLAYER_MINA_HARKER){
  299.         if (currPlayer <= 3 && getRound(gv)>1) i = 2;
  300.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 25];
  301.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 26];
  302.     }
  303.     if (player == PLAYER_DRACULA){
  304.         if (currPlayer <= 4 && getRound(gv)>1) i = 2;
  305.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 33];   
  306.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 34];
  307.     }
  308.    
  309.     //printf("%d is getround\n", getRound(gv));
  310.     //printf("%s is loca\n", loca);
  311.     char *b = loca;
  312.     LocationID m = abbrevToID(b);
  313.     if (player == PLAYER_DRACULA){
  314.         if (validPlace(m))  {
  315.             return m;
  316.         }
  317.         if (loca[0] == 'C' && loca[1] == '?') return CITY_UNKNOWN;
  318.         if (loca[0] == 'S' && loca[1] == '?') return SEA_UNKNOWN;
  319.         if (loca[0] == 'H' && loca[1] == 'I') return HIDE;
  320.         if (loca[0] == 'D' && loca[1] == '1') return DOUBLE_BACK_1;
  321.         if (loca[0] == 'D' && loca[1] == '2') return DOUBLE_BACK_2;
  322.         if (loca[0] == 'D' && loca[1] == '3') return DOUBLE_BACK_3;
  323.         if (loca[0] == 'D' && loca[1] == '4') return DOUBLE_BACK_4;
  324.         if (loca[0] == 'D' && loca[1] == '5') return DOUBLE_BACK_5;
  325.         if (loca[0] == 'T' && loca[1] == 'P') return TELEPORT;
  326.         else return UNKNOWN_LOCATION;
  327.     }
  328.     return m;  
  329. }
  330.  
  331. //// Functions that return information about the history of the game
  332.  
  333. // Fills the trail array with the location ids of the last 6 turns
  334.  
  335. void
  336. getHistory (GameView gv, PlayerID player, LocationID trail[TRAIL_SIZE])
  337. {
  338.     //fill trail with -1 to begin with
  339.     for(int j = 0; j < TRAIL_SIZE; j++){
  340.         trail[j] = UNKNOWN_LOCATION;
  341.     }
  342.    
  343.     //find latest play index in pastP
  344.     int lastPlay = latestPlay(gv);
  345.     char tempPlayer = playerToChar(player);
  346.    
  347.     //lastPlay = lastPlay-6;
  348.    
  349.     //find latest play of our current player
  350.     while(gv->pastP[lastPlay] != tempPlayer){
  351.         lastPlay = lastPlay - PLAY_LEN;
  352.     }//index should now be at specified player
  353.    
  354.     //Dracula case:---------------
  355.     if(player == PLAYER_DRACULA){
  356.         int count = 0;
  357.         while(count < TRAIL_SIZE){
  358.                
  359.             if(lastPlay <= 0){//fills in rest of trail with unknown loc
  360.                 count = TRAIL_SIZE;//exit while loop
  361.             } else {
  362.                 char tempLoc[2];//temporary location
  363.                 tempLoc[0] = gv->pastP[lastPlay+1];
  364.                 tempLoc[1] = gv->pastP[lastPlay+2];
  365.  
  366.                 LocationID trailAdd;
  367.                
  368.                 if(tempLoc[0]=='C' && tempLoc[1]=='?'){//if unknown city location
  369.                     trailAdd = CITY_UNKNOWN;
  370.                 } else if(tempLoc[0]=='S' && tempLoc[1]=='?'){//if unknown sea location
  371.                     trailAdd = SEA_UNKNOWN;
  372.                 } else if(tempLoc[0]=='H' && tempLoc[1]=='I'){//if hide move
  373.                     trailAdd = HIDE;
  374.                 } else if(tempLoc[0]=='T' && tempLoc[1]=='P'){//if teleport move
  375.                     trailAdd = TELEPORT;
  376.                 }
  377.                 else if(tempLoc[0]=='D' && tempLoc[1]=='1'){ //all below are doubleback
  378.                     trailAdd = DOUBLE_BACK_1;
  379.                 } else if(tempLoc[0]=='D' && tempLoc[1]=='2'){
  380.                     trailAdd = DOUBLE_BACK_2;
  381.                 } else if(tempLoc[0]=='D' && tempLoc[1]=='3'){
  382.                     trailAdd = DOUBLE_BACK_3;
  383.                 } else if(tempLoc[0]=='D' && tempLoc[1]=='4'){
  384.                     trailAdd = DOUBLE_BACK_4;
  385.                 } else if(tempLoc[0]=='D' && tempLoc[1]=='5'){
  386.                     trailAdd = DOUBLE_BACK_5;
  387.                 }
  388.            
  389.                 else {//known location
  390.                     trailAdd = abbrevToID(tempLoc);
  391.                 }
  392.                
  393.                 pushToTrail(trail, trailAdd);//add to trail
  394.                 lastPlay = lastPlay - LINE_LEN;
  395.                 count++;
  396.             }
  397.         }//--------------
  398.     } else {
  399.         //hunter case:
  400.            
  401.         //now we fill in the trail, finally.
  402.         int countr = 0;
  403.         while(countr < TRAIL_SIZE){
  404.  
  405.             if(lastPlay < 0){
  406.                 countr = TRAIL_SIZE;
  407.             } else {
  408.            
  409.                 char currLoc[2];//current location
  410.                 //extract location abbreviation
  411.                 currLoc[0] = gv->pastP[lastPlay+1];
  412.                 currLoc[1] = gv->pastP[lastPlay+2];
  413.            
  414.                 //turn location abbreviation to locationID using places.c
  415.                 LocationID currLocation = abbrevToID(currLoc);
  416.            
  417.                 //finally, add locationID to trail
  418.                 pushToTrail(trail, currLocation);
  419.                
  420.                 lastPlay = lastPlay - LINE_LEN;//go back to the previous round
  421.                 countr++;//will only go to 6 to ensure trail is at most last 6 plays.
  422.             }
  423.         }
  424.         //after all of this the trail array should be updated for
  425.         //whichever player was parsed into the function.
  426.     }
  427. }
  428.  
  429.  
  430. //// Functions that query the map to find information about connectivity
  431.  
  432. // Returns an array of LocationIDs for all directly connected locations
  433.  
  434. LocationID *
  435. connectedLocations (GameView gv, int *numLocations,
  436.     LocationID from, PlayerID player, Round round,
  437.     bool road, bool rail, bool sea)
  438. {
  439.  
  440.     //int numConn = numberOfConnections(gv->dracMap, from);
  441.     //printf("numConn = %d\n", numConn);
  442.     //*numLocations = numConn;
  443.    
  444.     //LocationID locArray[50];
  445.     //printf("%d is locArray\n", locArray[0]);
  446.     LocationID *b;
  447.     b = locationsConnected(gv->dracMap, from, 100, road, rail, sea);
  448.     int i = 0;
  449.     int connCount = numberOfConnections(gv->dracMap, from, road, rail, sea);
  450.     //printf("%d is connCount\n", connCount);
  451.     while (i < connCount){
  452.         //printf("%s is id----------\n", idToName(b[i]));
  453.         i++;
  454.     }
  455.     *numLocations = connCount;
  456.  
  457.    
  458.     //LocationID locArr[numLocations];
  459.     // REPLACE THIS WITH YOUR OWN IMPLEMENTATION
  460.     return b;
  461. }
  462.  
  463. static void pushToTrail(LocationID trail[TRAIL_SIZE], LocationID location){
  464.     assert(trail != NULL);
  465.     int i = 0;
  466.     while(i < TRAIL_SIZE){
  467.         if(trail[i] == UNKNOWN_LOCATION){
  468.             trail[i] = location;
  469.             i = TRAIL_SIZE;
  470.         } else {
  471.             i++;
  472.         }
  473.     }
  474. }
  475. static int latestPlay(GameView gv){
  476.     int i = pastPlaysLength(gv);
  477.     i -= (PLAY_LEN - 1);
  478.     return i;
  479. }
  480. static char playerToChar(PlayerID player){//helper function
  481.  
  482.     char playerChar;
  483.     if(player == PLAYER_DRACULA){
  484.         playerChar = 'D';
  485.     } else if (player == PLAYER_LORD_GODALMING){
  486.         playerChar = 'G';
  487.     } else if (player == PLAYER_DR_SEWARD){
  488.         playerChar = 'S';
  489.     } else if (player == PLAYER_VAN_HELSING){
  490.         playerChar = 'H';
  491.     } else /*player == PLAYER_MINA_HARKER*/{
  492.         playerChar = 'M';
  493.     }
  494.     return playerChar;
  495. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement