Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.98 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.                     gv->draLife = gv->draLife - 10;
  204.                 }
  205.                 i++;
  206.             }
  207.             i = 0;
  208.             playerAdd = playerAdd+8;
  209.             turnCount++;   
  210.         }
  211.         int m = 33;
  212.         char loca[2] = {0};
  213.         loca[2] = '\0';
  214.         int prevLocaSea = 0;
  215.        
  216.         while (m < pastPlaysLength(gv)){
  217.             loca[0] = gv->pastP[m];
  218.             loca[1] = gv->pastP[m+1];
  219.             char *b = loca;
  220.             PlaceType z = UNKNOWN;
  221.             LocationID y = abbrevToID(b);
  222.             if (validPlace(y))  z = idToType(y);
  223.                
  224.             if (loca[0] == 'S' && loca[1] == '?'){
  225.                 gv->draLife = gv->draLife - 2;
  226.                 prevLocaSea = 1;
  227.             }else if (z == SEA){
  228.                 gv->draLife = gv->draLife -2;
  229.                 prevLocaSea = 1;
  230.             }else if (loca[0] == 'D' && (loca[1] >= 1)){
  231.                 if (prevLocaSea == 1) gv->draLife = gv->draLife-2;
  232.             }else{
  233.                 prevLocaSea = 0;
  234.             }
  235.                
  236.             m = m+LINE_LEN;
  237.         }
  238.            
  239.    
  240.         return gv->draLife;
  241.     }
  242.  
  243.     return 0;
  244. }
  245.  
  246. // Get the current location id of a given player
  247. LocationID
  248. getLocation (GameView gv, PlayerID player)
  249. {
  250.     int currPlayer = getCurrentPlayer(gv);
  251.     char loca[2] = {0};
  252.     loca[2] = '\0';
  253.     int i = 1;
  254.     if (strcmp(gv->pastP, "\0") == 0){
  255.         return UNKNOWN_LOCATION;
  256.     }
  257.     if (player == PLAYER_LORD_GODALMING){
  258.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 1];
  259.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 2];
  260.     }
  261.     if (player == PLAYER_DR_SEWARD){
  262.         if (currPlayer <= 1 && getRound(gv)>1) i = 2;
  263.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 9];
  264.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 10];
  265.     }
  266.     if (player == PLAYER_VAN_HELSING){
  267.         if (currPlayer <= 2 && getRound(gv)>1) i = 2;
  268.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 17];
  269.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 18];
  270.     }
  271.     if (player == PLAYER_MINA_HARKER){
  272.         if (currPlayer <= 3 && getRound(gv)>1) i = 2;
  273.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 25];
  274.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 26];
  275.     }
  276.     if (player == PLAYER_DRACULA){
  277.         if (currPlayer <= 4 && getRound(gv)>1) i = 2;
  278.         loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 33];   
  279.         loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 34];
  280.     }
  281.    
  282.     printf("%d is getround\n", getRound(gv));
  283.     printf("%s is loca\n", loca);
  284.     char *b = loca;
  285.     LocationID m = abbrevToID(b);
  286.     if (player == PLAYER_DRACULA){
  287.         if (validPlace(m))  {
  288.             return m;
  289.         }
  290.         if (loca[0] == 'C' && loca[1] == '?') return CITY_UNKNOWN;
  291.         if (loca[0] == 'S' && loca[1] == '?') return SEA_UNKNOWN;
  292.         if (loca[0] == 'H' && loca[1] == 'I') return HIDE;
  293.         if (loca[0] == 'D' && loca[1] == '1') return DOUBLE_BACK_1;
  294.         if (loca[0] == 'D' && loca[1] == '2') return DOUBLE_BACK_2;
  295.         if (loca[0] == 'D' && loca[1] == '3') return DOUBLE_BACK_3;
  296.         if (loca[0] == 'D' && loca[1] == '4') return DOUBLE_BACK_4;
  297.         if (loca[0] == 'D' && loca[1] == '5') return DOUBLE_BACK_5;
  298.         if (loca[0] == 'T' && loca[1] == 'P') return TELEPORT;
  299.         else return UNKNOWN_LOCATION;
  300.     }
  301.     return m;  
  302. }
  303.  
  304. //// Functions that return information about the history of the game
  305.  
  306. // Fills the trail array with the location ids of the last 6 turns
  307.  
  308. void
  309. getHistory (GameView gv, PlayerID player, LocationID trail[TRAIL_SIZE])
  310. {
  311.     //fill trail with -1 to begin with
  312.     for(int j = 0; j < TRAIL_SIZE; j++){
  313.         trail[j] = UNKNOWN_LOCATION;
  314.     }
  315.    
  316.     //find latest play index in pastP
  317.     int lastPlay = latestPlay(gv);
  318.     char tempPlayer = playerToChar(player);
  319.    
  320.     //lastPlay = lastPlay-6;
  321.    
  322.     //find latest play of our current player
  323.     while(gv->pastP[lastPlay] != tempPlayer){
  324.         lastPlay = lastPlay - PLAY_LEN;
  325.     }//index should now be at specified player
  326.    
  327.     //Dracula case:---------------
  328.     if(player == PLAYER_DRACULA){
  329.         int count = 0;
  330.         while(count < TRAIL_SIZE){
  331.                
  332.             if(lastPlay <= 0){//fills in rest of trail with unknown loc
  333.                 count = TRAIL_SIZE;//exit while loop
  334.             } else {
  335.                 char tempLoc[2];//temporary location
  336.                 tempLoc[0] = gv->pastP[lastPlay+1];
  337.                 tempLoc[1] = gv->pastP[lastPlay+2];
  338.  
  339.                 LocationID trailAdd;
  340.                
  341.                 if(tempLoc[0]=='C' && tempLoc[1]=='?'){//if unknown city location
  342.                     trailAdd = CITY_UNKNOWN;
  343.                 } else if(tempLoc[0]=='S' && tempLoc[1]=='?'){//if unknown sea location
  344.                     trailAdd = SEA_UNKNOWN;
  345.                 } else if(tempLoc[0]=='H' && tempLoc[1]=='I'){//if hide move
  346.                     trailAdd = HIDE;
  347.                 } else if(tempLoc[0]=='T' && tempLoc[1]=='P'){//if teleport move
  348.                     trailAdd = TELEPORT;
  349.                 }
  350.                 else if(tempLoc[0]=='D' && tempLoc[1]=='1'){ //all below are doubleback
  351.                     trailAdd = DOUBLE_BACK_1;
  352.                 } else if(tempLoc[0]=='D' && tempLoc[1]=='2'){
  353.                     trailAdd = DOUBLE_BACK_2;
  354.                 } else if(tempLoc[0]=='D' && tempLoc[1]=='3'){
  355.                     trailAdd = DOUBLE_BACK_3;
  356.                 } else if(tempLoc[0]=='D' && tempLoc[1]=='4'){
  357.                     trailAdd = DOUBLE_BACK_4;
  358.                 } else if(tempLoc[0]=='D' && tempLoc[1]=='5'){
  359.                     trailAdd = DOUBLE_BACK_5;
  360.                 }
  361.            
  362.                 else {//known location
  363.                     trailAdd = abbrevToID(tempLoc);
  364.                 }
  365.                
  366.                 pushToTrail(trail, trailAdd);//add to trail
  367.                 lastPlay = lastPlay - LINE_LEN;
  368.                 count++;
  369.             }
  370.         }//--------------
  371.     } else {
  372.         //hunter case:
  373.            
  374.         //now we fill in the trail, finally.
  375.         int countr = 0;
  376.         while(countr < TRAIL_SIZE){
  377.  
  378.             if(lastPlay < 0){
  379.                 countr = TRAIL_SIZE;
  380.             } else {
  381.            
  382.                 char currLoc[2];//current location
  383.                 //extract location abbreviation
  384.                 currLoc[0] = gv->pastP[lastPlay+1];
  385.                 currLoc[1] = gv->pastP[lastPlay+2];
  386.            
  387.                 //turn location abbreviation to locationID using places.c
  388.                 LocationID currLocation = abbrevToID(currLoc);
  389.            
  390.                 //finally, add locationID to trail
  391.                 pushToTrail(trail, currLocation);
  392.                
  393.                 lastPlay = lastPlay - LINE_LEN;//go back to the previous round
  394.                 countr++;//will only go to 6 to ensure trail is at most last 6 plays.
  395.             }
  396.         }
  397.         //after all of this the trail array should be updated for
  398.         //whichever player was parsed into the function.
  399.     }
  400. }
  401.  
  402.  
  403. //// Functions that query the map to find information about connectivity
  404.  
  405. // Returns an array of LocationIDs for all directly connected locations
  406.  
  407. LocationID *
  408. connectedLocations (GameView gv, int *numLocations,
  409.     LocationID from, PlayerID player, Round round,
  410.     bool road, bool rail, bool sea)
  411. {
  412.  
  413.     //int numConn = numberOfConnections(gv->dracMap, from);
  414.     //printf("numConn = %d\n", numConn);
  415.     //*numLocations = numConn;
  416.    
  417.     //LocationID locArray[50];
  418.     //printf("%d is locArray\n", locArray[0]);
  419.     LocationID *b;
  420.     b = locationsConnected(gv->dracMap, from, 100, road, rail, sea);
  421.     int i = 0;
  422.     int connCount = numberOfConnections(gv->dracMap, from, road, rail, sea);
  423.     //printf("%d is connCount\n", connCount);
  424.     while (i < connCount){
  425.         //printf("%s is id----------\n", idToName(b[i]));
  426.         i++;
  427.     }
  428.     *numLocations = connCount;
  429.  
  430.    
  431.     //LocationID locArr[numLocations];
  432.     // REPLACE THIS WITH YOUR OWN IMPLEMENTATION
  433.     return b;
  434. }
  435.  
  436. static void pushToTrail(LocationID trail[TRAIL_SIZE], LocationID location){
  437.     assert(trail != NULL);
  438.     int i = 0;
  439.     while(i < TRAIL_SIZE){
  440.         if(trail[i] == UNKNOWN_LOCATION){
  441.             trail[i] = location;
  442.             i = TRAIL_SIZE;
  443.         } else {
  444.             i++;
  445.         }
  446.     }
  447. }
  448. static int latestPlay(GameView gv){
  449.     int i = pastPlaysLength(gv);
  450.     i -= (PLAY_LEN - 1);
  451.     return i;
  452. }
  453. static char playerToChar(PlayerID player){//helper function
  454.  
  455.     char playerChar;
  456.     if(player == PLAYER_DRACULA){
  457.         playerChar = 'D';
  458.     } else if (player == PLAYER_LORD_GODALMING){
  459.         playerChar = 'G';
  460.     } else if (player == PLAYER_DR_SEWARD){
  461.         playerChar = 'S';
  462.     } else if (player == PLAYER_VAN_HELSING){
  463.         playerChar = 'H';
  464.     } else /*player == PLAYER_MINA_HARKER*/{
  465.         playerChar = 'M';
  466.     }
  467.     return playerChar;
  468. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement