Advertisement
zynamo

HunterView.c

Jan 21st, 2018
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.90 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////
  2. // COMP2521 18x1 ... the Fury of Dracula
  3. // HunterView.c: the HunterView 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.  
  14. #include "Game.h"
  15. #include "GameView.h"
  16. #include "Globals.h"
  17. #include "HunterView.h"
  18. #include "Map.h"
  19.  
  20. struct hunterView {
  21.     int dracLife;
  22.     char *dracPlist;
  23.     int roundNumber;
  24.     int currPlayer;
  25.     int gameScore;
  26.    
  27.     int godLoca;
  28.     int drsLoca;
  29.     int vanLoca;
  30.     int minLoca;
  31.     int draLoca;
  32.    
  33.     int godLife;
  34.     int drsLife;
  35.     int vanLife;
  36.     int minLife;
  37.     int draLife;
  38.    
  39.     Map dracMap;
  40. };
  41.  
  42. // Creates a new HunterView to summarise the current state of the game
  43. HunterView
  44. newHunterView (char *pastPlays, PlayerMessage messages[])
  45. {
  46.     HunterView new = malloc (sizeof *new);
  47.     GameView dragame = newGameView(pastPlays, messages);
  48.     if (new == NULL) err (EX_OSERR, "couldn't allocate HunterView");
  49.         new->dracLife = getHealth(dragame, PLAYER_DRACULA);
  50.         new->dracPlist = pastPlays;
  51.         new->roundNumber = getRound(dragame);
  52.         if (new->roundNumber < 0) new->roundNumber = 0;
  53.        
  54.         new->godLoca = getLocation(dragame, PLAYER_LORD_GODALMING);
  55.         new->drsLoca = getLocation(dragame, PLAYER_DR_SEWARD);
  56.         new->vanLoca = getLocation(dragame, PLAYER_VAN_HELSING);
  57.         new->minLoca = getLocation(dragame, PLAYER_MINA_HARKER);
  58.         new->draLoca = getLocation(dragame, PLAYER_DRACULA);
  59.            
  60.         new->godLife = getHealth (dragame, PLAYER_LORD_GODALMING);
  61.         new->drsLife = getHealth (dragame, PLAYER_DR_SEWARD);
  62.         new->vanLife = getHealth (dragame, PLAYER_VAN_HELSING);
  63.         new->minLife = getHealth (dragame, PLAYER_MINA_HARKER);
  64.         new->draLife = getHealth (dragame, PLAYER_DRACULA);
  65.         new->gameScore = getScore(dragame);
  66.         new->dracMap = newMap();
  67.         new->currPlayer = getCurrentPlayer(dragame);
  68.         disposeGameView(dragame);
  69.  
  70.     return new;
  71. }
  72.  
  73. // Frees all memory previously allocated for the HunterView toBeDeleted
  74. void
  75. disposeHunterView (HunterView toBeDeleted)
  76. {
  77.     disposeMap(toBeDeleted->dracMap);
  78.     free (toBeDeleted);
  79. }
  80.  
  81. //// Functions to return simple information about the current state of the game
  82.  
  83. // Get the current round
  84. Round
  85. giveMeTheRound (HunterView hv)
  86. {
  87.     return hv->roundNumber;
  88. }
  89.  
  90. // Get the id of current player
  91. PlayerID
  92. whoAmI (HunterView hv)
  93. {
  94.  
  95.     return hv->currPlayer;
  96. }
  97.  
  98. // Get the current score
  99. int
  100. giveMeTheScore (HunterView hv)
  101. {
  102.     return hv->gameScore;
  103. }
  104.  
  105. // Get the current health points for a given player
  106. int
  107. howHealthyIs (HunterView hv, PlayerID player)
  108. {
  109.     if (player == PLAYER_LORD_GODALMING) return hv->godLife;
  110.     if (player == PLAYER_MINA_HARKER) return hv->minLife;
  111.     if (player == PLAYER_VAN_HELSING) return hv->vanLife;
  112.     if (player == PLAYER_DR_SEWARD) return hv->drsLife;
  113.     if (player == PLAYER_DRACULA) return hv->draLife;
  114.     return 0;
  115. }
  116.  
  117. // Get the current location id of a given player
  118. LocationID
  119. whereIs (HunterView hv, PlayerID player)
  120. {
  121.     if (player == PLAYER_LORD_GODALMING) return hv->godLoca;
  122.     if (player == PLAYER_MINA_HARKER) return hv->minLoca;
  123.     if (player == PLAYER_VAN_HELSING) return hv->vanLoca;
  124.     if (player == PLAYER_DR_SEWARD) return hv->drsLoca;
  125.     if (player == PLAYER_DRACULA) return hv->draLoca;
  126.     return 0;
  127. }
  128.  
  129. //// Functions that return information about the history of the game
  130.  
  131. // Fills the trail array with the location ids of the last 6 turns
  132. void
  133. giveMeTheTrail (HunterView hv, PlayerID player,
  134.     LocationID trail[TRAIL_SIZE])
  135. {
  136.     PlayerMessage messages[5] = { " ", " ", " "};//just garbage
  137.     GameView hunterGame = newGameView(hv->dracPlist, messages);
  138.     if (hunterGame == NULL) err (EX_OSERR, "couldn't allocate DracView)");
  139.    
  140.     getHistory(hunterGame, player, trail);
  141. }
  142.  
  143. //// Functions that query the map to find information about connectivity
  144.  
  145. // What are my possible next moves (locations)
  146. LocationID *
  147. whereCanIgo (HunterView hv, int *numLocations,
  148.     bool road, bool rail, bool sea)
  149. {
  150.     LocationID *b;
  151.     PlayerID currPlayer = hv->currPlayer;
  152.     LocationID from = whereIs(hv, currPlayer);
  153.     b = locationsConnected(hv->dracMap, from, 10, road, rail, sea);
  154.     int connCount = numberOfConnections(hv->dracMap, from, road, rail, sea);
  155.    
  156.     *numLocations = connCount;
  157.    
  158.     return b;
  159. }
  160.  
  161. // What are the specified player's next possible moves
  162. LocationID *
  163. whereCanTheyGo (HunterView hv, int *numLocations, PlayerID player,
  164.     bool road, bool rail, bool sea)
  165. {
  166.        
  167.     LocationID *b;
  168.     LocationID from = whereIs(hv, player);
  169.    
  170.     b = locationsConnected(hv->dracMap, from, 10, road, rail, sea);
  171.     int connCount = numberOfConnections(hv->dracMap, from, road, rail, sea);
  172.  
  173.     *numLocations = connCount;
  174.  
  175.     return b;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement