Advertisement
Guest User

Untitled

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