Advertisement
rkynaa

GameView.c

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