Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.71 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.  
  17. #include "Game.h"
  18. #include "GameView.h"
  19. #include "Globals.h"
  20. #include "Map.h"// ... if you decide to use the Map ADT
  21.  
  22. #define MAX_SIZE 1000
  23. #define LINE_LEN 40
  24. #define PLAY_LEN 8
  25.  
  26. struct gameView {
  27.     Round roundNumber;
  28.     int currPlayer;
  29.     int currScore;
  30.     //pList players[NUM_PLAYERS];
  31.    
  32.     int godLife;
  33.     int drsLife;
  34.     int vanLife;
  35.     int minLife;
  36.     int draLife;
  37.    
  38.     int godLoca;
  39.     int drsLoca;
  40.     int vanLoca;
  41.     int minLoca;
  42.     int draLoca;
  43.    
  44.     char *pastP;
  45.    
  46. };
  47. //static void pushToTrail(LocationID trail[TRAIL_SIZE], LocationID location);
  48. //static int latestPlay(GameView gv);
  49.  
  50. static int pastPlaysLength(GameView gv){
  51.     int i = 0;
  52.     char *string = gv->pastP;
  53.     while (string[i] != '\0'){
  54.         i++;
  55.     }
  56.     return i;
  57. }
  58.  
  59.  
  60. // Creates a new GameView to summarise the current state of the game
  61. GameView
  62. newGameView (char *pastPlays, PlayerMessage messages[])
  63. {
  64.     GameView new = malloc (sizeof *new);
  65.     //printf("%c is pastPlays[5]\n", pastPlays[5]);
  66.     if (new == NULL) err (EX_OSERR, "couldn't allocate GameView");
  67.    
  68.     new->pastP = pastPlays;
  69.        
  70.     new->godLife = GAME_START_HUNTER_LIFE_POINTS;
  71.     new->drsLife = GAME_START_HUNTER_LIFE_POINTS;
  72.     new->vanLife = GAME_START_HUNTER_LIFE_POINTS;
  73.     new->minLife = GAME_START_HUNTER_LIFE_POINTS;
  74.     new->draLife = GAME_START_BLOOD_POINTS;
  75.    
  76.     new->godLoca = UNKNOWN_LOCATION;
  77.     new->drsLoca = UNKNOWN_LOCATION;
  78.     new->vanLoca = UNKNOWN_LOCATION;
  79.     new->minLoca = UNKNOWN_LOCATION;
  80.     new->draLoca = UNKNOWN_LOCATION;
  81.    
  82.     new->roundNumber = 0;
  83.     new->currPlayer = 0;
  84.     new->currScore = GAME_START_SCORE;
  85.        
  86.     return new;
  87. }
  88.  
  89. // Frees all memory previously allocated for the GameView toBeDeleted
  90. void
  91. disposeGameView (GameView toBeDeleted)
  92. {
  93.     // COMPLETE THIS IMPLEMENTATION
  94.     free (toBeDeleted);
  95. }
  96.  
  97. //// Functions to return simple information about the current state of the game
  98.  
  99. // Get the current round
  100. Round
  101. getRound (GameView gv)
  102. {
  103.     if (strcmp(gv->pastP, "\0") == 0)   return 0;
  104.     int i = 0;
  105.     int roundCount = 0;
  106.         while(gv->pastP[i] != '\0'){
  107.             if (i%LINE_LEN == 0){
  108.                 roundCount++;
  109.             }
  110.             i++;       
  111.         }
  112.     gv->roundNumber = roundCount;      
  113.     return roundCount;
  114. }
  115.  
  116. // Get the id of current player - ie whose turn is it?
  117. PlayerID
  118. getCurrentPlayer (GameView gv)
  119. {  
  120.     int pastPlaysLen = pastPlaysLength(gv);
  121.     int i = 0;
  122.     int currPlayer = 0;
  123.     while (i < pastPlaysLen){
  124.         if (i%8 == 0){
  125.             currPlayer++;
  126.             if (currPlayer > 4){
  127.                 currPlayer = 0;
  128.             }
  129.             //printf("i is divisible by 8, i is %d, currPlayer is %d\n", i, currPlayer);
  130.         }
  131.         i++;
  132.     }
  133.     //printf("%d is currplayer\n", currPlayer);
  134.     return currPlayer;
  135. }
  136.  
  137. // Get the current score
  138. int
  139. getScore (GameView gv)
  140. {
  141.     if (strcmp(gv->pastP, "\0") == 0){
  142.         return GAME_START_SCORE;
  143.     }
  144.  
  145.     return 0;
  146. }
  147.  
  148. // Get the current health points for a given player
  149. int
  150. getHealth (GameView gv, PlayerID player)
  151. {
  152.     //int turnsMade = pastPlaysLength(gv)/8;
  153.     int turnCount = 0;
  154.     int i = 3;
  155.     int playerAdd = 0;
  156.     int playerHealth = 0;
  157.     if (player == PLAYER_LORD_GODALMING) {
  158.         playerAdd = 3;
  159.         playerHealth = gv->godLife;
  160.     }
  161.     if (player == PLAYER_DR_SEWARD){
  162.         playerAdd = 11;
  163.         playerHealth = gv->drsLife;
  164.     }
  165.     if (player == PLAYER_VAN_HELSING){
  166.         playerAdd = 19;
  167.         playerHealth = gv->vanLife;
  168.     }
  169.     if (player == PLAYER_MINA_HARKER){
  170.         playerAdd = 27;
  171.         playerHealth = gv->minLife;
  172.     }
  173.     if (player == PLAYER_DRACULA){
  174.         playerAdd = 3;
  175.         playerHealth = gv->draLife;
  176.     }
  177.        
  178.     if (player != PLAYER_DRACULA) {
  179.         while (turnCount < getRound(gv)){
  180.             printf("looping turncount\n");
  181.             while (i < 5){
  182.                 if (gv->pastP[playerAdd+i] == 'D'){
  183.                     playerHealth = playerHealth - 4;
  184.                     printf("dracula encounters\n");
  185.                 }
  186.                 i++;
  187.             }
  188.             turnCount++;
  189.             playerAdd = playerAdd+39;
  190.             i = 0;
  191.         }
  192.         printf("%d is playerHealth\n", playerHealth);
  193.         return playerHealth;
  194.     }
  195.    
  196.     else if (player == PLAYER_DRACULA){
  197.         int i = 0;
  198.         char *string = gv->pastP;
  199.         while ((playerAdd + i) < pastPlaysLength(gv)){
  200.             while (i < 5){
  201.                 if (string[playerAdd + i] == 'D'){
  202.                     gv->draLife = gv->draLife - 10;
  203.                     printf("dracula encounetsszz\n");
  204.                 }
  205.                 i++;
  206.             }
  207.             i = 0;
  208.             playerAdd = playerAdd+8;
  209.             turnCount++;   
  210.         }
  211.         printf("dralife is %d\n", gv->draLife);
  212.         return gv->draLife;
  213.     }
  214.  
  215.     return 0;
  216. }
  217.  
  218. // Get the current location id of a given player
  219. LocationID
  220. getLocation (GameView gv, PlayerID player)
  221. {
  222.     int currPlayer = getCurrentPlayer(gv);
  223.     printf("%d is currPlayer\n", currPlayer);
  224.     char loca[2] = {0};
  225.     loca[2] = '\0';
  226.     int i = 1;
  227.     if (strcmp(gv->pastP, "\0") == 0){
  228.         return UNKNOWN_LOCATION;
  229.     }
  230.     if (player == PLAYER_LORD_GODALMING){
  231.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 1];
  232.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 2];
  233.     }
  234.     if (player == PLAYER_DR_SEWARD){
  235.         if (currPlayer < 1 && getRound(gv)>1) i = 2;
  236.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 9];
  237.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 10];
  238.     }
  239.     if (player == PLAYER_VAN_HELSING){
  240.         if (currPlayer < 2 && getRound(gv)>1) i = 2;
  241.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 17];
  242.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 18];
  243.     }
  244.     if (player == PLAYER_MINA_HARKER){
  245.         if (currPlayer < 3 && getRound(gv)>1) i = 2;
  246.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 25];
  247.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 26];
  248.     }
  249.     if (player == PLAYER_DRACULA){
  250.         if (currPlayer < 4 && getRound(gv)>1) i = 2;
  251.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 33];   
  252.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 34];
  253.     }
  254.    
  255.    
  256.    
  257.         printf("%s is loca\n", loca);
  258.     char *b = loca;
  259.     LocationID m = abbrevToID(b);
  260.     printf("%d is m\n", m);
  261.     if (player == PLAYER_DRACULA){
  262.         if (validPlace(m))  {
  263.             printf("validplace\n");
  264.             return m;
  265.         }
  266.         if (loca[0] == 'C' && loca[1] == '?') return CITY_UNKNOWN;
  267.         if (loca[0] == 'S' && loca[1] == '?') return SEA_UNKNOWN;
  268.         if (loca[0] == 'H' && loca[1] == 'I') return HIDE;
  269.         /*if (loca[0] == 'D' && loca[1] == '1') return DOUBLE_BACK_ONE;
  270.         if (loca[0] == 'D' && loca[1] == '2') return DOUBLE_BACK_TWO;
  271.         if (loca[0] == 'D' && loca[1] == '3') return DOUBLE_BACK_THREE;
  272.         if (loca[0] == 'D' && loca[1] == '4') return DOUBLE_BACK_FOUR;
  273.         if (loca[0] == 'D' && loca[1] == '5') return DOUBLE_BACK_FIVE;*/
  274.         if (loca[0] == 'T' && loca[1] == 'P') return TELEPORT;
  275.         else return UNKNOWN_LOCATION;
  276.     }
  277.     return m;      
  278. }
  279.  
  280. //// Functions that return information about the history of the game
  281.  
  282. // Fills the trail array with the location ids of the last 6 turns
  283. void
  284. getHistory (GameView gv, PlayerID player, LocationID trail[TRAIL_SIZE])
  285. {
  286.     // REPLACE THIS WITH YOUR OWN IMPLEMENTATION
  287. }
  288.  
  289. //// Functions that query the map to find information about connectivity
  290.  
  291. // Returns an array of LocationIDs for all directly connected locations
  292.  
  293. LocationID *
  294. connectedLocations (GameView gv, int *numLocations,
  295.     LocationID from, PlayerID player, Round round,
  296.     bool road, bool rail, bool sea)
  297. {
  298.     // REPLACE THIS WITH YOUR OWN IMPLEMENTATION
  299.     *numLocations = 0;
  300.     return NULL;
  301. }
  302.  
  303. // THIS PART JUST ADDED
  304. /*void getHistory (GameView gv, PlayerID player, LocationID trail[TRAIL_SIZE])
  305. {
  306.     //Check if its the beginning of the game
  307.     if (gv->roundNumber == 0) {
  308.         int trailC = 0;
  309.         while(trailC < TRAIL_SIZE){//add UNKNOWN_LOCATION to entire trail
  310.             trail[trailC] = UNKNOWN_LOCATION;
  311.             trailC++;
  312.         }
  313.     } else {
  314.         //find latest play index in pastP
  315.         int lastPlay = latestPlay(gv);
  316.         while(pastP[lastPlay] != player){
  317.             lastPlay = lastPlay - PLAY_LEN;
  318.         }//latest play by player
  319.        
  320.         //now we fill in the trail, finally.
  321.         int countr = 0;
  322.         while(countr < TRAIL_SIZE){
  323.             char currLoc[2];
  324.             //extract location abbreviation
  325.             currLoc[0] = pastP[lastPlay+1];
  326.             currLoc[1] = pastP[lastplay+2];
  327.             currLoc[2] = '\0';
  328.            
  329.             //turn location abbreviation to locationID
  330.             LocationID currLocation = abbrevToID(currLoc);
  331.            
  332.             //finally, add locationID to trail
  333.             pushToTrail(trail, currLocation);
  334.            
  335.             lastPlay = lastPlay - LINE_LEN;//go back to the previous round
  336.             countr++;//will only go to 6 to ensure trail is at most last 6 plays.
  337.         }
  338.         //after all of this the trail array should be updated for
  339.         //whichever player was parsed into the function.
  340.     }
  341. }
  342.  
  343. //local function to push location onto trail. oldest location falls off trail.
  344. static void pushToTrail(LocationID trail[TRAIL_SIZE], LocationID location){
  345.     assert(trail != NULL);
  346.     int i = 0;
  347.     LocationID temp = trail[i];
  348.     while(trail[i] != '\0'){
  349.         //tests to see if were adding to end of array
  350.         if(i+1 == TRAIL_SIZE){
  351.             trail[i] = location;
  352.             i++;//increments and then loop ends.
  353.        
  354.         } else {//operation to shift numbers along array.
  355.             trail[i] = location;
  356.             location = temp;
  357.             temp = trail[i+1];
  358.             i++;
  359.         }
  360.     }
  361. }
  362.  
  363. static int latestPlay(GameView gv){
  364.     int i = 0;
  365.     while(gv->pastP[i] != '\0'){//iterate to latest playt at end of pastP array
  366.         i++;
  367.     }
  368.     i = i - PLAY_LEN;//subtract to reach the beginning
  369.     return i;*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement