Advertisement
zynamo

gameview.c 2:07-21/1

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