Advertisement
rkynaa

gameview.c (original)

Jan 19th, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.26 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. if (player == PLAYER_LORD_GODALMING) {
  170. playerAdd = 3;
  171. playerHealth = gv->godLife;
  172. } else if (player == PLAYER_DR_SEWARD) {
  173. playerAdd = 11;
  174. playerHealth = gv->drsLife;
  175. } else if (player == PLAYER_VAN_HELSING) {
  176. playerAdd = 19;
  177. playerHealth = gv->vanLife;
  178. } else if (player == PLAYER_MINA_HARKER) {
  179. playerAdd = 27;
  180. playerHealth = gv->minLife;
  181. } else if (player == PLAYER_DRACULA) {
  182. playerAdd = 3;
  183. playerHealth = gv->draLife;
  184. }
  185. if (player != PLAYER_DRACULA) {
  186. while (turnCount < getRound(gv)){
  187. printf("looping turncount\n");
  188. for (int i = 3; i < 5; i++){
  189. if (gv -> pastP[playerAdd + i] == 'D'){
  190. playerHealth = playerHealth - 4;
  191. printf("dracula encounters\n");
  192. }
  193. }
  194. turnCount++;
  195. playerAdd = playerAdd+39;
  196. }
  197. printf("%d is playerHealth\n", playerHealth);
  198. return playerHealth;
  199. }
  200. else if (player == PLAYER_DRACULA){
  201. char *string = gv -> pastP;
  202. int i = 0;
  203. while ((playerAdd + i) < pastPlaysLength(gv)){
  204. while (i < 5){
  205. if (string[playerAdd + i] == 'D'){
  206. gv -> draLife = gv -> draLife - 10;
  207. printf("dracula encounetsszz\n");
  208. }
  209. i++;
  210. }
  211. i = 0;
  212. playerAdd += 8;
  213. turnCount++;
  214. }
  215. printf("dralife is %d\n", gv -> draLife);
  216. return gv -> draLife;
  217. }
  218. return 0;
  219. }
  220.  
  221. // Get the current location id of a given player
  222. LocationID
  223. getLocation (GameView gv, PlayerID player)
  224. {
  225. int currPlayer = getCurrentPlayer(gv), i = 1;
  226. printf("%d is currPlayer\n", currPlayer);
  227. char loca[2] = {0};
  228. loca[2] = '\0';
  229. if (strcmp(gv->pastP, "\0") == 0){
  230. return UNKNOWN_LOCATION;
  231. }
  232. if (player == PLAYER_LORD_GODALMING) {
  233. loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 1];
  234. loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 2];
  235. } else if (player == PLAYER_DR_SEWARD) {
  236. if (currPlayer < 1 && getRound(gv) > 1) i = 2;
  237. loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 9];
  238. loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 10];
  239. } else 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. } else if (player == PLAYER_MINA_HARKER) {
  244. if (currPlayer < 3 && getRound(gv) > 1) i = 2;
  245. loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 25];
  246. loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 26];
  247. } else if (player == PLAYER_DRACULA) {
  248. if (currPlayer < 4 && getRound(gv) > 1) i = 2;
  249. loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 33];
  250. loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 34];
  251. }
  252. printf("%s is loca\n", loca);
  253. char *b = loca;
  254. LocationID m = abbrevToID(b);
  255. printf("%d is m\n", m);
  256. if (player == PLAYER_DRACULA){
  257. if (validPlace(m)) {
  258. printf("validplace\n");
  259. return m;
  260. }
  261. if (loca[0] == 'C' && loca[1] == '?') return CITY_UNKNOWN;
  262. if (loca[0] == 'S' && loca[1] == '?') return SEA_UNKNOWN;
  263. if (loca[0] == 'H' && loca[1] == 'I') return HIDE;
  264. /*if (loca[0] == 'D' && loca[1] == '1') return DOUBLE_BACK_ONE;
  265. if (loca[0] == 'D' && loca[1] == '2') return DOUBLE_BACK_TWO;
  266. if (loca[0] == 'D' && loca[1] == '3') return DOUBLE_BACK_THREE;
  267. if (loca[0] == 'D' && loca[1] == '4') return DOUBLE_BACK_FOUR;
  268. if (loca[0] == 'D' && loca[1] == '5') return DOUBLE_BACK_FIVE;*/
  269. if (loca[0] == 'T' && loca[1] == 'P') return TELEPORT;
  270. else return UNKNOWN_LOCATION;
  271. }
  272. return m;
  273. }
  274.  
  275. //// Functions that return information about the history of the game
  276.  
  277. // Fills the trail array with the location ids of the last 6 turns
  278. void
  279. getHistory (GameView gv, PlayerID player, LocationID trail[TRAIL_SIZE])
  280. {
  281. //Check if its the beginning of the game
  282. if (gv -> roundNumber == 0) {
  283. for (int trailC = 0; trailC < TRAIL_SIZE; trailC++){//add UNKNOWN_LOCATION to entire trail
  284. trail[trailC] = UNKNOWN_LOCATION;
  285. }
  286. } else {
  287. //find latest play index in pastP
  288. int lastPlay = latestPlay(gv);
  289. char *pastP_hist = gv->pastP;
  290. for (char *lastPlayInd = gv->pastP; lastPlayInd[lastPlay] != player; lastPlay -= PLAY_LEN);
  291. if (player == 'D'){
  292. for (int count = 0; count < TRAIL_SIZE; count++){
  293. char tempLoc[2];
  294. tempLoc[0] = pastP_hist[lastPlay+1];
  295. tempLoc[1] = pastP_hist[lastPlay+2];
  296. tempLoc[2] = '\0';
  297. int trailAdd;
  298. if (tempLoc[0]=='C' && tempLoc[1]=='?'){
  299. trailAdd = CITY_UNKNOWN;
  300. } else if(tempLoc[0]=='S' && tempLoc[1]=='?'){
  301. trailAdd = SEA_UNKNOWN;
  302. } else if(tempLoc[0]=='H' && tempLoc[1]=='I'){
  303. trailAdd = HIDE;
  304. } else if(tempLoc[0]=='T' && tempLoc[1]=='P'){
  305. trailAdd = TELEPORT;
  306. } else if(tempLoc[0]=='D' && tempLoc[1]=='1'){
  307. trailAdd = DOUBLE_BACK_ONE;
  308. } else if(tempLoc[0]=='D' && tempLoc[1]=='2'){
  309. trailAdd = DOUBLE_BACK_TWO;
  310. } else if(tempLoc[0]=='D' && tempLoc[1]=='3'){
  311. trailAdd = DOUBLE_BACK_THREE;
  312. } else if(tempLoc[0]=='D' && tempLoc[1]=='4'){
  313. trailAdd = DOUBLE_BACK_FOUR;
  314. } else if(tempLoc[0]=='D' && tempLoc[1]=='5'){
  315. trailAdd = DOUBLE_BACK_FIVE;
  316. }
  317. pushToTrail(trail, trailAdd);
  318. lastPlay -= LINE_LEN;
  319. }
  320. } else {
  321. for (int countr = 0; countr < TRAIL_SIZE; countr++){
  322. char currLoc[2];
  323. currLoc[0] = pastP_hist[lastPlay+1];
  324. currLoc[1] = pastP_hist[lastPlay+2];
  325. currLoc[2] = '\0';
  326. LocationID currLocation = abbrevToID(currLoc);
  327. pushToTrail(trail, currLocation);
  328. lastPlay -= LINE_LEN;
  329. }
  330. }
  331. }
  332. }
  333.  
  334.  
  335.  
  336. //// Functions that query the map to find information about connectivity
  337.  
  338. // Returns an array of LocationIDs for all directly connected locations
  339.  
  340. LocationID *
  341. connectedLocations (GameView gv, int *numLocations,
  342. LocationID from, PlayerID player, Round round,
  343. bool road, bool rail, bool sea)
  344. {
  345. // REPLACE THIS WITH YOUR OWN IMPLEMENTATION
  346. *numLocations = 0;
  347. return NULL;
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement