Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////
- // COMP2521 18x1 ... the Fury of Dracula
- // GameView.c: GameView ADT implementation
- //
- // 2014-07-01 v1.0 Team Dracula <cs2521@cse.unsw.edu.au>
- // 2017-12-01 v1.1 Team Dracula <cs2521@cse.unsw.edu.au>
- #include <assert.h>
- #include <err.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <sysexits.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "Game.h"
- #include "GameView.h"
- #include "Globals.h"
- #include "Map.h"// ... if you decide to use the Map ADT
- #define MAX_SIZE 1000
- #define LINE_LEN 40
- #define PLAY_LEN 8
- struct gameView {
- Round roundNumber;
- int currPlayer;
- int currScore;
- //pList players[NUM_PLAYERS];
- int godLife;
- int drsLife;
- int vanLife;
- int minLife;
- int draLife;
- int godLoca;
- int drsLoca;
- int vanLoca;
- int minLoca;
- int draLoca;
- char *pastP;
- };
- //static void pushToTrail(LocationID trail[TRAIL_SIZE], LocationID location);
- //static int latestPlay(GameView gv);
- static int pastPlaysLength(GameView gv){
- char *string = gv->pastP;
- int i;
- for (i = 0; string[i] != '\0'; i++);
- return i;
- }
- //local function to push location onto trail. oldest location falls off trail.
- static void pushToTrail(LocationID trail[TRAIL_SIZE], LocationID location){
- assert(trail != NULL);
- LocationID temp = trail[0];
- for (int i = 0; trail[i] != '\0'; i++){
- //tests to see if were adding to end of array
- if (i + 1 == TRAIL_SIZE){
- trail[i] = location;
- } else {//operation to shift numbers along array.
- trail[i] = location;
- location = temp;
- temp = trail[i+1];
- }
- }
- }
- static int latestPlay(GameView gv){
- int i;
- for (i = 0; gv->pastP[i] != '\0'; i++); //iterate to latest playt at end of pastP array
- i -= PLAY_LEN; //subtract to reach the beginning
- return i;
- }
- // Creates a new GameView to summarise the current state of the game
- GameView
- newGameView (char *pastPlays, PlayerMessage messages[])
- {
- GameView new = malloc (sizeof *new);
- //printf("%c is pastPlays[5]\n", pastPlays[5]);
- if (new == NULL) err (EX_OSERR, "couldn't allocate GameView");
- new->pastP = pastPlays;
- new->godLife = GAME_START_HUNTER_LIFE_POINTS;
- new->drsLife = GAME_START_HUNTER_LIFE_POINTS;
- new->vanLife = GAME_START_HUNTER_LIFE_POINTS;
- new->minLife = GAME_START_HUNTER_LIFE_POINTS;
- new->draLife = GAME_START_BLOOD_POINTS;
- new->godLoca = UNKNOWN_LOCATION;
- new->drsLoca = UNKNOWN_LOCATION;
- new->vanLoca = UNKNOWN_LOCATION;
- new->minLoca = UNKNOWN_LOCATION;
- new->draLoca = UNKNOWN_LOCATION;
- new->roundNumber = 0;
- new->currPlayer = 0;
- new->currScore = GAME_START_SCORE;
- return new;
- }
- // Frees all memory previously allocated for the GameView toBeDeleted
- void
- disposeGameView (GameView toBeDeleted)
- {
- // COMPLETE THIS IMPLEMENTATION
- free (toBeDeleted);
- }
- //// Functions to return simple information about the current state of the game
- // Get the current round
- Round
- getRound (GameView gv)
- {
- if (strcmp(gv->pastP, "\0") == 0) return 0;
- int roundCount = 0;
- for (int i = 0; gv->pastP[i] != '\0'; i++){
- if (i % LINE_LEN == 0){
- roundCount++;
- }
- }
- gv->roundNumber = roundCount;
- return roundCount;
- }
- // Get the id of current player - ie whose turn is it?
- PlayerID
- getCurrentPlayer (GameView gv)
- {
- int pastPlaysLen = pastPlaysLength(gv), currPlayer = 0;
- for (int i = 0; i < pastPlaysLen; i++){
- if (i % 8 == 0){
- currPlayer++;
- if (currPlayer > 4){
- currPlayer = 0;
- }
- //printf("i is divisible by 8, i is %d, currPlayer is %d\n", i, currPlayer);
- }
- }
- //printf("%d is currplayer\n", currPlayer);
- return currPlayer;
- }
- // Get the current score
- int
- getScore (GameView gv)
- {
- if (strcmp(gv -> pastP, "\0") == 0){
- return GAME_START_SCORE;
- }
- return 0;
- }
- // Get the current health points for a given player
- int
- getHealth (GameView gv, PlayerID player)
- {
- //int turnsMade = pastPlaysLength(gv)/8;
- int turnCount = 0, playerAdd = 0, playerHealth = 0;
- if (player == PLAYER_LORD_GODALMING) {
- playerAdd = 3;
- playerHealth = gv->godLife;
- } else if (player == PLAYER_DR_SEWARD) {
- playerAdd = 11;
- playerHealth = gv->drsLife;
- } else if (player == PLAYER_VAN_HELSING) {
- playerAdd = 19;
- playerHealth = gv->vanLife;
- } else if (player == PLAYER_MINA_HARKER) {
- playerAdd = 27;
- playerHealth = gv->minLife;
- } else if (player == PLAYER_DRACULA) {
- playerAdd = 3;
- playerHealth = gv->draLife;
- }
- if (player != PLAYER_DRACULA) {
- while (turnCount < getRound(gv)){
- printf("looping turncount\n");
- for (int i = 3; i < 5; i++){
- if (gv -> pastP[playerAdd + i] == 'D'){
- playerHealth = playerHealth - 4;
- printf("dracula encounters\n");
- }
- }
- turnCount++;
- playerAdd = playerAdd+39;
- }
- printf("%d is playerHealth\n", playerHealth);
- return playerHealth;
- }
- else if (player == PLAYER_DRACULA){
- char *string = gv -> pastP;
- int i = 0;
- while ((playerAdd + i) < pastPlaysLength(gv)){
- while (i < 5){
- if (string[playerAdd + i] == 'D'){
- gv -> draLife = gv -> draLife - 10;
- printf("dracula encounetsszz\n");
- }
- i++;
- }
- i = 0;
- playerAdd += 8;
- turnCount++;
- }
- printf("dralife is %d\n", gv -> draLife);
- return gv -> draLife;
- }
- return 0;
- }
- // Get the current location id of a given player
- LocationID
- getLocation (GameView gv, PlayerID player)
- {
- int currPlayer = getCurrentPlayer(gv), i = 1;
- printf("%d is currPlayer\n", currPlayer);
- char loca[2] = {0};
- loca[2] = '\0';
- if (strcmp(gv->pastP, "\0") == 0){
- return UNKNOWN_LOCATION;
- }
- if (player == PLAYER_LORD_GODALMING) {
- loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 1];
- loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 2];
- } else if (player == PLAYER_DR_SEWARD) {
- if (currPlayer < 1 && getRound(gv) > 1) i = 2;
- loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 9];
- loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 10];
- } else if (player == PLAYER_VAN_HELSING) {
- if (currPlayer < 2 && getRound(gv) > 1) i = 2;
- loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 17];
- loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 18];
- } else if (player == PLAYER_MINA_HARKER) {
- if (currPlayer < 3 && getRound(gv) > 1) i = 2;
- loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 25];
- loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 26];
- } else if (player == PLAYER_DRACULA) {
- if (currPlayer < 4 && getRound(gv) > 1) i = 2;
- loca[0] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 33];
- loca[1] = gv->pastP[(LINE_LEN * (getRound(gv)-i)) + 34];
- }
- printf("%s is loca\n", loca);
- char *b = loca;
- LocationID m = abbrevToID(b);
- printf("%d is m\n", m);
- if (player == PLAYER_DRACULA){
- if (validPlace(m)) {
- printf("validplace\n");
- return m;
- }
- if (loca[0] == 'C' && loca[1] == '?') return CITY_UNKNOWN;
- if (loca[0] == 'S' && loca[1] == '?') return SEA_UNKNOWN;
- if (loca[0] == 'H' && loca[1] == 'I') return HIDE;
- /*if (loca[0] == 'D' && loca[1] == '1') return DOUBLE_BACK_ONE;
- if (loca[0] == 'D' && loca[1] == '2') return DOUBLE_BACK_TWO;
- if (loca[0] == 'D' && loca[1] == '3') return DOUBLE_BACK_THREE;
- if (loca[0] == 'D' && loca[1] == '4') return DOUBLE_BACK_FOUR;
- if (loca[0] == 'D' && loca[1] == '5') return DOUBLE_BACK_FIVE;*/
- if (loca[0] == 'T' && loca[1] == 'P') return TELEPORT;
- else return UNKNOWN_LOCATION;
- }
- return m;
- }
- //// Functions that return information about the history of the game
- // Fills the trail array with the location ids of the last 6 turns
- void
- getHistory (GameView gv, PlayerID player, LocationID trail[TRAIL_SIZE])
- {
- //Check if its the beginning of the game
- if (gv -> roundNumber == 0) {
- for (int trailC = 0; trailC < TRAIL_SIZE; trailC++){//add UNKNOWN_LOCATION to entire trail
- trail[trailC] = UNKNOWN_LOCATION;
- }
- } else {
- //find latest play index in pastP
- int lastPlay = latestPlay(gv);
- char *pastP_hist = gv->pastP;
- for (char *lastPlayInd = gv->pastP; lastPlayInd[lastPlay] != player; lastPlay -= PLAY_LEN);
- if (player == 'D'){
- for (int count = 0; count < TRAIL_SIZE; count++){
- char tempLoc[2];
- tempLoc[0] = pastP_hist[lastPlay+1];
- tempLoc[1] = pastP_hist[lastPlay+2];
- tempLoc[2] = '\0';
- int trailAdd;
- if (tempLoc[0]=='C' && tempLoc[1]=='?'){
- trailAdd = CITY_UNKNOWN;
- } else if(tempLoc[0]=='S' && tempLoc[1]=='?'){
- trailAdd = SEA_UNKNOWN;
- } else if(tempLoc[0]=='H' && tempLoc[1]=='I'){
- trailAdd = HIDE;
- } else if(tempLoc[0]=='T' && tempLoc[1]=='P'){
- trailAdd = TELEPORT;
- } else if(tempLoc[0]=='D' && tempLoc[1]=='1'){
- trailAdd = DOUBLE_BACK_ONE;
- } else if(tempLoc[0]=='D' && tempLoc[1]=='2'){
- trailAdd = DOUBLE_BACK_TWO;
- } else if(tempLoc[0]=='D' && tempLoc[1]=='3'){
- trailAdd = DOUBLE_BACK_THREE;
- } else if(tempLoc[0]=='D' && tempLoc[1]=='4'){
- trailAdd = DOUBLE_BACK_FOUR;
- } else if(tempLoc[0]=='D' && tempLoc[1]=='5'){
- trailAdd = DOUBLE_BACK_FIVE;
- }
- pushToTrail(trail, trailAdd);
- lastPlay -= LINE_LEN;
- }
- } else {
- for (int countr = 0; countr < TRAIL_SIZE; countr++){
- char currLoc[2];
- currLoc[0] = pastP_hist[lastPlay+1];
- currLoc[1] = pastP_hist[lastPlay+2];
- currLoc[2] = '\0';
- LocationID currLocation = abbrevToID(currLoc);
- pushToTrail(trail, currLocation);
- lastPlay -= LINE_LEN;
- }
- }
- }
- }
- //// Functions that query the map to find information about connectivity
- // Returns an array of LocationIDs for all directly connected locations
- LocationID *
- connectedLocations (GameView gv, int *numLocations,
- LocationID from, PlayerID player, Round round,
- bool road, bool rail, bool sea)
- {
- // REPLACE THIS WITH YOUR OWN IMPLEMENTATION
- *numLocations = 0;
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement