Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Game.c
- * 1917 v2.0
- * Add to and change this file as you wish to implement the
- * interface functions in Game.h
- *
- * Created by Richard Buckland on 20/04/11.
- * Copyright 2011 Licensed under Creative Commons SA-BY-NC 3.0.
- *
- */
- //By Anne, Ben, Nat, Sheryl, Yere
- //12 MAY 2011
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include "Game.h"
- #define EAST 0
- #define SOUTH_EAST 1
- #define SOUTH_WEST 2
- #define WEST 3
- #define NORTH_WEST 4
- #define NORTH_EAST 5
- #define NUM_STUDENT_TYPES 6
- #define NUM_DIRECTIONS 6
- #define NUM_VERTICES 54
- #define NUM_ARCS 72
- #define NUM_RETRAININGCENTRES 9
- //-------Data structures
- typedef struct _playerCodes {
- int playersARC; //code for the given players' arcs
- int playersCampus; //code for the given players' campuses
- int playersGO8; //code for the given players' GO8s
- } playerCodes;
- typedef struct _vertex {
- int x; //horizontal axis -
- int y; //axis facing the same way as a forward slash
- int z; // axis facing the same way as a backslash
- int content;
- } vertex;
- typedef struct _arc {
- int leftPoint; //the leftmost point the arc is on
- int rightPoint; //the rightmost point the arc is on
- int content;
- } arc;
- typedef struct _region {
- int x;
- int y;
- int z;
- int diceNumber;
- int discipline;
- } region;
- typedef struct _retrainingCentre {
- int arc; //The arc the retraining centre is on
- int retrainingType; //2 or 3
- int discipline; //The discipline of student it produces (type 2 only)
- } retrainingCentre;
- typedef struct _game {
- //Current status
- int currentPlayer;
- int currentTurn;
- int currentDice;
- //'Most' status
- int mostARCs;
- int mostPubs;
- //Player stats
- int patents[NUM_UNIS+1];
- int publications[NUM_UNIS+1];
- int KPI[NUM_UNIS+1];
- //An array of size 3, each element is another array of size 6
- //(stores how many of each student a uni has)
- int students[NUM_UNIS+1][NUM_STUDENT_TYPES];
- //An array of region 'structs'
- region regions[NUM_REGIONS];
- //An array of retraining centres
- retrainingCentre retrainingCentres[NUM_RETRAININGCENTRES];
- //Vertices and arcs
- vertex vertices[NUM_VERTICES];
- arc arcs[NUM_ARCS];
- } game;
- //---------------Shared helper functions
- //returns a value >NUM_VERTICES if path invalid
- static int getVertexID (Game g, path pathToVertex);
- //returns a value >NUM_ARCS if path invalid
- static int getARCID (Game g, path pathToEdge);
- //set up the #define of the ARC/GO8/Campus for the player
- static playerCodes getPlayerCodes (Game g, int player);
- //convert path to coordinate
- static void shiftCoordinate (path pathTo, int *x, int *y, int*z);
- static int getVertexID (Game g, path pathToVertex) {
- int vx, vy, vz;
- int theVertex;
- int i;
- theVertex = NUM_VERTICES+1; //Set it to an invalid vertex initially
- shiftCoordinate(pathToVertex, &vx, &vy, &vz);
- i = 0;
- //once the vertex is found or searched through all, exit while
- while ((i < NUM_VERTICES) && (theVertex > NUM_VERTICES)) {
- if ((vx == g->vertices[i].x) && (vy == g->vertices[i].y) &&
- (vz == g->vertices[i].z)) {
- theVertex = i;
- }
- i++;
- }
- return theVertex;
- }
- static int getARCID (Game g, path pathToEdge) {
- int theARC;
- int firstVertex, secondVertex;
- path backPath;
- int i;
- theARC = NUM_ARCS+1; //Set it to an invalid arc initially
- firstVertex = getVertexID (g, pathToEdge);
- memcpy(backPath, pathToEdge, strlen(pathToEdge)+1);
- strcat(backPath, "B");
- secondVertex = getVertexID (g, backPath);
- //printf("first vertex is %s\n", pathToEdge);
- //printf("second vertex is %s", backPath);
- i = 0;
- //once the arc is found or searched through all the arcs, exit while
- while ((i < NUM_ARCS) && (theARC > NUM_ARCS)) {
- if (((g->arcs[i].leftPoint == firstVertex) &&
- (g->arcs[i].rightPoint == secondVertex))
- ||((g->arcs[i].rightPoint == firstVertex) &&
- (g->arcs[i].leftPoint == secondVertex))) {
- theARC = i;
- }
- i++;
- }
- return theARC;
- }
- static playerCodes getPlayerCodes (Game g, int player) {
- playerCodes codes;
- codes.playersARC = 0;
- codes.playersCampus = 0;
- codes.playersGO8 = 0;
- if (player == UNI_A) {
- codes.playersARC = ARC_A;
- codes.playersCampus = CAMPUS_A;
- codes.playersGO8 = GO8_A;
- } else if (player == UNI_B) {
- codes.playersARC = ARC_B;
- codes.playersCampus = CAMPUS_B;
- codes.playersGO8 = GO8_B;
- } else if (player == UNI_C) {
- codes.playersARC = ARC_C;
- codes.playersCampus = CAMPUS_C;
- codes.playersGO8 = GO8_C;
- } else {
- codes.playersARC = 0;
- codes.playersCampus = 0;
- codes.playersGO8 = 0;
- }
- return codes;
- }
- //coordinate/path system
- static void shiftCoordinate(path pathTo, int *x, int *y, int *z) {
- int facingPath;
- int i;
- //initialise start position
- *x = 0;
- *y = 2;
- *z = 7;
- //initial facing position
- facingPath = SOUTH_EAST;
- i = 0;
- while ((pathTo[i] != '\0') && (i < PATH_LIMIT)) {
- if (pathTo[i] == 'L') {
- facingPath = ((facingPath - 1) % NUM_DIRECTIONS);
- while (facingPath < 0) {
- facingPath = (facingPath + NUM_DIRECTIONS)% NUM_DIRECTIONS;
- }
- } else if (pathTo[i] == 'R') {
- facingPath = ((facingPath + 1) % NUM_DIRECTIONS);
- } else if (pathTo[i] == 'B') {
- facingPath = ((facingPath + 3) % NUM_DIRECTIONS);
- }
- if (facingPath == EAST) {
- *y+=1;
- *z+=1;
- } else if (facingPath == SOUTH_EAST) {
- *x+=1;
- *y+=1;
- } else if (facingPath == SOUTH_WEST) {
- *x+=1;
- *z-=1;
- } else if (facingPath == WEST) {
- *y-=1;
- *z-=1;
- } else if (facingPath == NORTH_WEST) {
- *x-=1;
- *y-=1;
- } else if (facingPath == NORTH_EAST) {
- *x-=1;
- *z+=1;
- }
- i++;
- }
- }
- //---------'Setters'
- Game newGame (int discipline[], int dice[]) {
- Game g = malloc(sizeof(game));
- int uni, student;
- g->currentTurn = -1;
- g->currentDice = 0;
- g->currentPlayer = NO_ONE;
- g->mostARCs = NO_ONE;
- g->mostPubs = NO_ONE;
- uni = UNI_A;
- student = STUDENT_THD;
- while (uni <= UNI_C) {
- student = 0;
- while (student < NUM_STUDENT_TYPES) {
- g->students[uni][student] = 0;
- student++;
- }
- g->patents[uni] = 0;
- g->publications[uni] = 0;
- g->KPI[uni] = 24;
- uni++;
- }
- //Set all the regions - 19 times
- region allRegions[NUM_REGIONS] = {
- //blue 0
- {
- .x = 3,
- .y = 1,
- .z = 3,
- },
- //red 1
- {
- .x = 5,
- .y = 2,
- .z = 2,
- },
- //light blue 2
- {
- .x = 7,
- .y = 3,
- .z = 1,
- },
- //yellow 3
- {
- .x = 2,
- .y = 2,
- .z = 5,
- },
- //green 4
- {
- .x = 4,
- .y = 3,
- .z = 4,
- } ,
- //purple 5
- {
- .x = 6,
- .y = 4,
- .z = 3,
- },
- //blue 6
- {
- .x = 8,
- .y = 5,
- .z = 2,
- },
- //red 7
- {
- .x = 1,
- .y = 3,
- .z = 7,
- },
- //light blue 8
- {
- .x = 3,
- .y = 4,
- .z = 6,
- },
- //yellow 9
- {
- .x = 5,
- .y = 5,
- .z = 5,
- },
- //green 10
- {
- .x = 7,
- .y = 6,
- .z = 4,
- },
- //purple 11
- {
- .x = 9,
- .y = 7,
- .z = 3,
- },
- //blue 12
- {
- .x = 2,
- .y = 5,
- .z = 8,
- },
- //red 13
- {
- .x = 4,
- .y = 6,
- .z = 7,
- },
- //light blue 14
- {
- .x = 6,
- .y = 7,
- .z = 6,
- },
- //yellow 15
- {
- .x = 8,
- .y = 8,
- .z = 5,
- },
- //green 16
- {
- .x = 3,
- .y = 7,
- .z = 9,
- },
- //purple 17
- {
- .x = 5,
- .y = 8,
- .z = 8,
- },
- //blue 18
- {
- .x = 7,
- .y = 9,
- .z = 7,
- },
- };
- //Copy this set of regions into game struct
- int i;
- i = 0;
- while (i < NUM_REGIONS) {
- g->regions[i] = allRegions[i];
- i++;
- }
- //Set all the dice and disciplines for the regions
- i = 0;
- while (i < NUM_REGIONS) {
- g->regions[i].diceNumber = dice[i];
- g->regions[i].discipline = discipline[i];
- i++;
- }
- //Set all the vertices. Also set up the initial campuses for the unis
- vertex allVertices[NUM_VERTICES] = {
- //1
- {
- .x = 0,
- .y = 2,
- .z = 7,
- .content = CAMPUS_A
- },
- //2
- {
- .x = 0,
- .y = 3,
- .z = 8,
- .content = VACANT_VERTEX
- },
- //3
- {
- .x = 1,
- .y = 4,
- .z = 8,
- .content = VACANT_VERTEX
- },
- //4
- {
- .x = 1,
- .y = 5,
- .z = 9,
- .content = VACANT_VERTEX
- },
- //5
- {
- .x = 2,
- .y = 6,
- .z = 9,
- .content = VACANT_VERTEX
- },
- //6
- {
- .x = 2,
- .y = 7,
- .z = 10,
- .content = CAMPUS_C
- },
- //7
- {
- .x = 3,
- .y = 8,
- .z = 10,
- .content = VACANT_VERTEX
- },
- //8
- {
- .x = 4,
- .y = 8,
- .z = 9,
- .content = VACANT_VERTEX
- },
- //9
- {
- .x = 5,
- .y = 9,
- .z = 9,
- .content = VACANT_VERTEX
- },
- //10
- {
- .x = 6,
- .y = 9,
- .z = 8,
- .content = VACANT_VERTEX
- },
- //11
- {
- .x = 7,
- .y = 10,
- .z = 8,
- .content = CAMPUS_B
- },
- //12
- {
- .x = 8,
- .y = 10,
- .z = 7,
- .content = VACANT_VERTEX
- },
- //13
- {
- .x = 8,
- .y = 9,
- .z = 6,
- .content = VACANT_VERTEX
- },
- //14
- {
- .x = 9,
- .y = 9,
- .z = 5,
- .content = VACANT_VERTEX
- },
- //15
- {
- .x = 9,
- .y = 8,
- .z = 4,
- .content = VACANT_VERTEX
- },
- //16
- {
- .x = 10,
- .y = 8,
- .z = 3,
- .content = CAMPUS_A
- },
- //17
- {
- .x = 10,
- .y = 7,
- .z = 2,
- .content = VACANT_VERTEX
- },
- //18
- {
- .x = 9,
- .y = 6,
- .z = 2,
- .content = VACANT_VERTEX
- },
- //19
- {
- .x = 9,
- .y = 5,
- .z = 1,
- .content = VACANT_VERTEX
- },
- //20
- {
- .x = 8,
- .y = 4,
- .z = 1,
- .content = VACANT_VERTEX
- },
- //21
- {
- .x = 8,
- .y = 3,
- .z = 0,
- .content = CAMPUS_C
- },
- //22
- {
- .x = 7,
- .y = 2,
- .z = 0,
- .content = VACANT_VERTEX
- },
- //23
- {
- .x = 6,
- .y = 2,
- .z = 1,
- .content = VACANT_VERTEX
- },
- //24
- {
- .x = 5,
- .y = 1,
- .z = 1,
- .content = VACANT_VERTEX
- },
- //25
- {
- .x = 4,
- .y = 1,
- .z = 2,
- .content = VACANT_VERTEX
- },
- //26
- {
- .x = 3,
- .y = 0,
- .z = 2,
- .content = CAMPUS_B
- },
- //27
- {
- .x = 2,
- .y = 0,
- .z = 3,
- .content = VACANT_VERTEX
- },
- //28
- {
- .x = 2,
- .y = 1,
- .z = 4,
- .content = VACANT_VERTEX
- },
- //29
- {
- .x = 1,
- .y = 1,
- .z = 5,
- .content = VACANT_VERTEX
- },
- //30
- {
- .x = 1,
- .y = 2,
- .z = 6,
- .content = VACANT_VERTEX
- },
- //31
- {
- .x = 2,
- .y = 3,
- .z = 6,
- .content = VACANT_VERTEX
- },
- //32
- {
- .x = 2,
- .y = 4,
- .z = 7,
- .content = VACANT_VERTEX
- },
- //33
- {
- .x = 3,
- .y = 5,
- .z = 7,
- .content = VACANT_VERTEX
- },
- //34
- {
- .x = 3,
- .y = 6,
- .z = 8,
- .content = VACANT_VERTEX
- },
- //35
- {
- .x = 4,
- .y = 7,
- .z = 8,
- .content = VACANT_VERTEX
- },
- //36
- {
- .x = 5,
- .y = 7,
- .z = 7,
- .content = VACANT_VERTEX
- },
- //37
- {
- .x = 6,
- .y = 8,
- .z = 7,
- .content = VACANT_VERTEX
- },
- //38
- {
- .x = 7,
- .y = 8,
- .z = 6,
- .content = VACANT_VERTEX
- },
- //39
- {
- .x = 7,
- .y = 7,
- .z = 5,
- .content = VACANT_VERTEX
- },
- //40
- {
- .x = 8,
- .y = 7,
- .z = 4,
- .content = VACANT_VERTEX
- },
- //41
- {
- .x = 8,
- .y = 6,
- .z = 3,
- .content = VACANT_VERTEX
- },
- //42
- {
- .x = 7,
- .y = 5,
- .z = 3,
- .content = VACANT_VERTEX
- },
- //43
- {
- .x = 7,
- .y = 4,
- .z = 2,
- .content = VACANT_VERTEX
- },
- //44
- {
- .x = 6,
- .y = 3,
- .z = 2,
- .content = VACANT_VERTEX
- },
- //45
- {
- .x = 5,
- .y = 3,
- .z = 3,
- .content = VACANT_VERTEX
- },
- //46
- {
- .x = 4,
- .y = 2,
- .z = 3,
- .content = VACANT_VERTEX
- },
- //47
- {
- .x = 3,
- .y = 2,
- .z = 4,
- .content = VACANT_VERTEX
- },
- //48
- {
- .x = 3,
- .y = 3,
- .z = 5,
- .content = VACANT_VERTEX
- },
- //49
- {
- .x = 4,
- .y = 4,
- .z = 5,
- .content = VACANT_VERTEX
- },
- //50
- {
- .x = 4,
- .y = 5,
- .z = 6,
- .content = VACANT_VERTEX
- },
- //51
- {
- .x = 5,
- .y = 6,
- .z = 6,
- .content = VACANT_VERTEX
- },
- //52
- {
- .x = 6,
- .y = 6,
- .z = 5,
- .content = VACANT_VERTEX
- },
- //53
- {
- .x = 6,
- .y = 5,
- .z = 4,
- .content = VACANT_VERTEX
- },
- //54
- {
- .x = 5,
- .y = 4,
- .z = 4,
- .content = VACANT_VERTEX
- }
- };
- //Copies this set of vertices into game struct
- i = 0;
- while (i < NUM_VERTICES) {
- g->vertices[i] = allVertices[i];
- i++;
- }
- //Set all the arcs = 72
- arc allArcs[NUM_ARCS] = {
- //0
- {
- .leftPoint = 29,
- .rightPoint = 0,
- .content = ARC_A
- },
- //1
- {
- .leftPoint = 15,
- .rightPoint = 14,
- .content = ARC_A
- },
- //2
- {
- .leftPoint = 9,
- .rightPoint = 10,
- .content = ARC_B
- },
- //3
- {
- .leftPoint = 25,
- .rightPoint = 24,
- .content = ARC_B
- },
- //4
- {
- .leftPoint = 4,
- .rightPoint = 5,
- .content = ARC_C
- },
- //5
- {
- .leftPoint = 20,
- .rightPoint = 19,
- .content = ARC_C
- },
- //6
- {
- .leftPoint = 0,
- .rightPoint = 1,
- },
- //7
- {
- .leftPoint = 1,
- .rightPoint = 2,
- },
- //8
- {
- .leftPoint = 2,
- .rightPoint = 3,
- },
- //9
- {
- .leftPoint = 3,
- .rightPoint = 4,
- },
- //10
- {
- .leftPoint = 28,
- .rightPoint = 29,
- },
- //11
- {
- .leftPoint = 27,
- .rightPoint = 28,
- },
- //12
- {
- .leftPoint = 26,
- .rightPoint = 27,
- },
- //13
- {
- .leftPoint = 25,
- .rightPoint = 26,
- },
- //14
- {
- .leftPoint = 24,
- .rightPoint = 45,
- },
- //15
- {
- .leftPoint = 45,
- .rightPoint = 46,
- },
- //16
- {
- .leftPoint = 27,
- .rightPoint = 46,
- },
- //17
- {
- .leftPoint = 46,
- .rightPoint = 47,
- },
- //18
- {
- .leftPoint = 47,
- .rightPoint = 30,
- },
- //19
- {
- .leftPoint = 29,
- .rightPoint = 30,
- },
- //20
- {
- .leftPoint = 30,
- .rightPoint = 31,
- },
- //21
- {
- .leftPoint = 31,
- .rightPoint = 32,
- },
- //22
- {
- .leftPoint = 31,
- .rightPoint = 2,
- },
- //23
- {
- .leftPoint = 32,
- .rightPoint = 33,
- },
- //24
- {
- .leftPoint = 33,
- .rightPoint = 4,
- },
- //25
- {
- .leftPoint = 5,
- .rightPoint = 6,
- },
- //26
- {
- .leftPoint = 7,
- .rightPoint = 6,
- },
- //27
- {
- .leftPoint = 34,
- .rightPoint = 7,
- },
- //28
- {
- .leftPoint = 33,
- .rightPoint = 34,
- },
- //29
- {
- .leftPoint = 35,
- .rightPoint = 34,
- },
- //30
- {
- .leftPoint = 50,
- .rightPoint = 35,
- },
- //31
- {
- .leftPoint = 49,
- .rightPoint = 50,
- },
- //32
- {
- .leftPoint = 49,
- .rightPoint = 32,
- },
- //33
- {
- .leftPoint = 48,
- .rightPoint = 49,
- },
- //34
- {
- .leftPoint = 53,
- .rightPoint = 48,
- },
- //35
- {
- .leftPoint = 47,
- .rightPoint = 48,
- },
- //36
- {
- .leftPoint = 45,
- .rightPoint = 44,
- },
- //37
- {
- .leftPoint = 23,
- .rightPoint = 24,
- },
- //38
- {
- .leftPoint = 23,
- .rightPoint = 22,
- },
- //39
- {
- .leftPoint = 22,
- .rightPoint = 43,
- },
- //40
- {
- .leftPoint = 53,
- .rightPoint = 52,
- },
- //41
- {
- .leftPoint = 52,
- .rightPoint = 51,
- },
- //42
- {
- .leftPoint = 51,
- .rightPoint = 50,
- },
- //43
- {
- .leftPoint = 35,
- .rightPoint = 36,
- },
- //44
- {
- .leftPoint = 36,
- .rightPoint = 9,
- },
- //45
- {
- .leftPoint = 9,
- .rightPoint = 8,
- },
- //46
- {
- .leftPoint = 7,
- .rightPoint = 8,
- },
- //47
- {
- .leftPoint = 16,
- .rightPoint = 15,
- },
- //48
- {
- .leftPoint = 17,
- .rightPoint = 16,
- },
- //49
- {
- .leftPoint = 18,
- .rightPoint = 17,
- },
- //50
- {
- .leftPoint = 14,
- .rightPoint = 13,
- },
- //51
- {
- .leftPoint = 19,
- .rightPoint = 18,
- },
- //52
- {
- .leftPoint = 17,
- .rightPoint = 40,
- },
- //53
- {
- .leftPoint = 39,
- .rightPoint = 14,
- },
- //54
- {
- .leftPoint = 13,
- .rightPoint = 12,
- },
- //55
- {
- .leftPoint = 40,
- .rightPoint = 39,
- },
- //56
- {
- .leftPoint = 12,
- .rightPoint = 11,
- },
- //57
- {
- .leftPoint = 21,
- .rightPoint = 20,
- },
- //58
- {
- .leftPoint = 19,
- .rightPoint = 42,
- },
- //59
- {
- .leftPoint = 41,
- .rightPoint = 40,
- },
- //60
- {
- .leftPoint = 39,
- .rightPoint = 38,
- },
- //61
- {
- .leftPoint = 37,
- .rightPoint = 12,
- },
- //62
- {
- .leftPoint = 11,
- .rightPoint = 10,
- },
- //63
- {
- .leftPoint = 42,
- .rightPoint = 41,
- },
- //64
- {
- .leftPoint = 38,
- .rightPoint = 37,
- },
- //65
- {
- .leftPoint = 21,
- .rightPoint = 22,
- },
- //66
- {
- .leftPoint = 43,
- .rightPoint = 42,
- },
- //67
- {
- .leftPoint = 41,
- .rightPoint = 52,
- },
- //68
- {
- .leftPoint = 51,
- .rightPoint = 38,
- },
- //69
- {
- .leftPoint = 37,
- .rightPoint = 36,
- },
- //70
- {
- .leftPoint = 43,
- .rightPoint = 44,
- },
- //71
- {
- .leftPoint = 44,
- .rightPoint = 53,
- },
- };
- //Copy this set of arcs into game struct
- i = 0;
- while (i < NUM_ARCS) {
- g->arcs[i] = allArcs[i];
- i++;
- }
- //Set up all the retraining centres
- g->retrainingCentres[0].arc = 25;
- g->retrainingCentres[0].retrainingType = 3;
- g->retrainingCentres[1].arc = 47;
- g->retrainingCentres[1].retrainingType = 3;
- g->retrainingCentres[2].arc = 38;
- g->retrainingCentres[2].retrainingType = 3;
- g->retrainingCentres[3].arc = 13;
- g->retrainingCentres[3].retrainingType = 3;
- g->retrainingCentres[4].arc = 10;
- g->retrainingCentres[4].retrainingType = 2;
- g->retrainingCentres[4].discipline = STUDENT_MTV;
- g->retrainingCentres[5].arc = 8;
- g->retrainingCentres[5].retrainingType = 2;
- g->retrainingCentres[5].discipline = STUDENT_MMONEY;
- g->retrainingCentres[6].arc = 45;
- g->retrainingCentres[6].retrainingType = 2;
- g->retrainingCentres[6].discipline = STUDENT_BQN;
- g->retrainingCentres[7].arc = 51;
- g->retrainingCentres[7].retrainingType = 2;
- g->retrainingCentres[7].discipline = STUDENT_BPS;
- g->retrainingCentres[8].arc = 54;
- g->retrainingCentres[8].retrainingType = 2;
- g->retrainingCentres[8].discipline = STUDENT_MJ;
- return g;
- }
- void disposeGame (Game g) {
- assert (g != NULL);
- free (g);
- }
- void makeAction (Game g, action a) {
- int uni;
- int currentPlayer;
- currentPlayer = getWhoseTurn(g);
- playerCodes codes = getPlayerCodes(g, currentPlayer);
- if (a.actionCode == BUILD_CAMPUS) {
- g->vertices[getVertexID(g, a.destination)].content
- = codes.playersCampus;
- g->students[currentPlayer][STUDENT_BPS]--;
- g->students[currentPlayer][STUDENT_BQN]--;
- g->students[currentPlayer][STUDENT_MJ]--;
- g->students[currentPlayer][STUDENT_MTV]--;
- } else if (a.actionCode == BUILD_GO8) {
- g->vertices[getVertexID(g, a.destination)].content
- = codes.playersGO8;
- g->students[currentPlayer][STUDENT_MJ] -= 2;
- g->students[currentPlayer][STUDENT_MMONEY] -= 3;
- } else if (a.actionCode == OBTAIN_ARC) {
- g->arcs[getARCID(g, a.destination)].content = codes.playersARC;
- g->students[currentPlayer][STUDENT_BPS]--;
- g->students[currentPlayer][STUDENT_BQN]--;
- } else if (a.actionCode == OBTAIN_PUBLICATION) {
- g->publications[currentPlayer]++;
- g->students[currentPlayer][STUDENT_MJ]--;
- g->students[currentPlayer][STUDENT_MTV]--;
- g->students[currentPlayer][STUDENT_MMONEY]--;
- } else if (a.actionCode == OBTAIN_IP_PATENT) {
- g->patents[currentPlayer]++;
- g->students[currentPlayer][STUDENT_MJ]--;
- g->students[currentPlayer][STUDENT_MTV]--;
- g->students[currentPlayer][STUDENT_MMONEY]--;
- } else if (a.actionCode == RETRAIN_STUDENTS) {
- int exchange = getExchangeRate(g, currentPlayer, a.disciplineFrom,
- a.disciplineTo);
- g->students[currentPlayer][a.disciplineFrom] -= exchange;
- g->students[currentPlayer][a.disciplineTo] += 1;
- }
- g->mostARCs = getMostARCs(g);
- g->mostPubs = getMostPublications(g);
- //Recalculate KPI
- uni = UNI_A;
- while (uni <= UNI_C) {
- g->KPI[uni] = 0;
- g->KPI[uni] += (getCampuses (g, uni)) * 10;
- g->KPI[uni] += (getGO8s (g, uni)) * 20;
- g->KPI[uni] += (getARCs (g, uni)) * 2;
- g->KPI[uni] += (getIPs (g, uni)) * 10;
- if (getMostARCs(g) == uni) {
- g->KPI[uni] += 10;
- }
- if (getMostPublications(g) == uni) {
- g->KPI[uni] += 10;
- }
- uni++;
- }
- }
- void throwDice (Game g, int diceScore) {
- int i, j;
- int currentUni;
- int x, y, z;
- int vx, vy, vz;
- int student, content;
- currentUni = g->currentPlayer - 1;
- currentUni = (currentUni+1) % NUM_UNIS;
- g->currentPlayer = currentUni + 1;
- g->currentTurn++;
- g->currentDice = diceScore;
- //Distribute students
- //Find all regions with that dice score
- i = 0;
- while (i < NUM_REGIONS) {
- if (g->regions[i].diceNumber == diceScore) {
- x = g->regions[i].x;
- y = g->regions[i].y;
- z = g->regions[i].z;
- student = g->regions[i].discipline;
- //Each of the vertices on the region with campus/GO8
- j = 0;
- while (j < NUM_VERTICES) {
- vx = g->vertices[j].x;
- vy = g->vertices[j].y;
- vz = g->vertices[j].z;
- content = g->vertices[j].content;
- if ((((vx-x)*(vx-x) + (vy-y)*(vy-y) + (vz-z)*(vz-z))
- == 2) && (content != VACANT_VERTEX)) {
- //Distribute to owner
- if (content == CAMPUS_A) {
- g->students[UNI_A][student] += 1;
- } else if (content == CAMPUS_B) {
- g->students[UNI_B][student] += 1;
- } else if (content == CAMPUS_C) {
- g->students[UNI_C][student] += 1;
- } else if (content == GO8_A) {
- g->students[UNI_A][student] += 2;
- } else if (content == GO8_B) {
- g->students[UNI_B][student] += 2;
- } else if (content == GO8_C) {
- g->students[UNI_C][student] += 2;
- }
- }
- j++;
- }
- }
- i++;
- }
- //If 7 is rolled, replace with THD
- if (diceScore == 7) {
- i = UNI_A;
- while (i <= UNI_C) {
- g->students[i][STUDENT_THD] += g->students[i][STUDENT_MTV] +
- g->students[i][STUDENT_MMONEY];
- g->students[i][STUDENT_MTV] = 0;
- g->students[i][STUDENT_MMONEY] = 0;
- i++;
- }
- }
- }
- //---------'Getters'
- // what type of students are produced by the specified region?
- // see discipline codes above
- int getDiscipline (Game g, int regionID) {
- return g->regions[regionID].discipline;
- }
- // what dice value produces students in the specified region?
- // 2..12
- int getDiceValue (Game g, int regionID) {
- return g->regions[regionID].diceNumber;
- }
- int getMostARCs (Game g) {
- int uni;
- int i;
- uni = g->mostARCs;
- i = UNI_A;
- if (getARCs(g, 0) != 66) {
- uni = UNI_A;
- }
- while (i <= UNI_C) {
- if (getARCs (g, i) > getARCs (g, uni)) {
- uni = i;
- }
- i++;
- }
- return uni;
- }
- int getMostPublications (Game g) {
- int uni;
- uni = g->mostPubs;
- int i = UNI_A;
- if (g->mostPubs != NO_ONE){
- i = UNI_A;
- }
- while (i <= UNI_C) {
- if (getPublications (g, i) > getPublications (g, uni)) {
- uni = i;
- }
- i++;
- }
- return uni;
- }
- int getTurnNumber (Game g) {
- return g->currentTurn;
- }
- int getWhoseTurn (Game g) {
- return g->currentPlayer;
- }
- int getCampus (Game g, path pathToVertex) {
- int vertex = getVertexID (g, pathToVertex);
- return g->vertices[vertex].content;
- }
- int getARC (Game g, path pathToEdge) {
- int arc = getARCID (g, pathToEdge);
- return g->arcs[arc].content;
- }
- //------Legal action
- int isLegalAction (Game g, action a) {
- int pos;
- int isLegal = TRUE;
- int currentPlayer = getWhoseTurn(g);
- playerCodes codes = getPlayerCodes (g, currentPlayer);
- //checking for each of the action codes
- if (a.actionCode == PASS) {
- } else if (a.actionCode == BUILD_CAMPUS) {
- int vertexToBuild = getVertexID (g, a.destination);
- //has enough students
- if (((getStudents (g, currentPlayer, STUDENT_BPS) >= 1) &&
- (getStudents (g, currentPlayer, STUDENT_BQN) >= 1) &&
- (getStudents (g, currentPlayer, STUDENT_MJ) >= 1) &&
- (getStudents (g, currentPlayer, STUDENT_MTV) >= 1))
- == FALSE) {
- isLegal = FALSE;
- }
- pos = 0;
- while (a.destination[pos] != '\0') {
- pos++;
- }
- //valid vertex/path - size & is in array
- if ((pos >= PATH_LIMIT) || vertexToBuild >= NUM_VERTICES) {
- //need to test - edge cases
- //printf("pos is %d and vertex is %d\n",pos, vertexToBuild);
- // printf("path is %s", a.destination);
- printf("edge case failed\n");
- isLegal = FALSE;
- }
- //is vacant
- if (getCampus(g, a.destination) != VACANT_VERTEX) {
- isLegal = FALSE;
- printf("not vacant\n");
- }
- //has arc on it and no campus on vertex next to it
- int i = 0;
- int ARCsEmpty = TRUE;
- int ARCsOwned = FALSE;
- while ((i < NUM_ARCS) && (ARCsEmpty == TRUE)) {
- int leftPoint = g->arcs[i].leftPoint;
- int rightPoint = g->arcs[i].rightPoint;
- int leftContent = g->vertices[leftPoint].content;
- int rightContent = g->vertices[rightPoint].content;
- if (leftPoint == vertexToBuild) {
- if (rightContent != VACANT_VERTEX) {
- ARCsEmpty = FALSE;
- }
- if (g->arcs[i].content == codes.playersARC) {
- ARCsOwned = TRUE;
- }
- } else if (rightPoint == vertexToBuild) {
- if (leftContent != VACANT_VERTEX) {
- ARCsEmpty = FALSE;
- }
- if (g->arcs[i].content == codes.playersARC) {
- ARCsOwned = TRUE;
- }
- }
- i++;
- }
- if ((ARCsEmpty == FALSE) || (ARCsOwned == FALSE)) {
- isLegal = FALSE;
- }
- } else if (a.actionCode == BUILD_GO8) {
- //has enough students
- if (((getStudents (g, currentPlayer, STUDENT_MJ) >= 2) &&
- (getStudents (g, currentPlayer, STUDENT_MMONEY) >= 3))
- == FALSE) {
- isLegal = FALSE;
- }
- //has campus there
- if (getCampus(g, a.destination) != codes.playersCampus) {
- isLegal = FALSE;
- }
- //less than 8 GO8s
- if (getGO8s (g, currentPlayer) >= 8) {
- isLegal = FALSE;
- }
- } else if (a.actionCode == OBTAIN_ARC) {
- int arcToBuild = getARCID (g, a.destination);
- //has enough students
- if (((getStudents (g, currentPlayer, STUDENT_BPS) >= 1) &&
- (getStudents (g, currentPlayer, STUDENT_BQN) >= 1)) == FALSE) {
- printf("not enough students");
- isLegal = FALSE;
- }
- //valid ARC
- //printf ("This is arc number %d\n", arcToBuild);
- //counting how long the destination path is
- pos = 0;
- while (a.destination[pos] != '\0') {
- pos++;
- }
- //printf ("size of desination is %d\n", pos);
- //printf ("arcToBuild has value %d\n", arcToBuild);
- if ((pos >= PATH_LIMIT) || (arcToBuild >= NUM_ARCS)) {
- //check edge cases
- isLegal = FALSE;
- printf("invalid arc\n");
- }
- //is vacant
- if (getARC(g, a.destination) != VACANT_ARC) {
- isLegal = FALSE;
- printf("not vacant arc");
- }
- //has arc or campus near it belonging to the player
- int hasTouching = FALSE;
- int leftPoint = g->arcs[arcToBuild].leftPoint;
- int rightPoint = g->arcs[arcToBuild].rightPoint;
- int leftContent = g->vertices[leftPoint].content;
- int rightContent = g->vertices[rightPoint].content;
- if ((leftContent == codes.playersCampus) ||
- (rightContent == codes.playersCampus)) {
- hasTouching = TRUE;
- }
- int i = 0;
- while ((i < NUM_ARCS) && (hasTouching == FALSE)) {
- if ((g->arcs[i].leftPoint == leftPoint) ||
- (g->arcs[i].leftPoint == rightPoint) ||
- (g->arcs[i].rightPoint == leftPoint) ||
- (g->arcs[i].rightPoint == rightPoint)) {
- if (g->arcs[i].content == codes.playersARC) {
- hasTouching = TRUE;
- }
- }
- i++;
- }
- if (hasTouching == FALSE) {
- printf("nothing near this arc belonging to the player");
- isLegal = FALSE;
- }
- } else if (a.actionCode == START_SPINOFF) {
- //has enough students
- if (((getStudents (g, currentPlayer, STUDENT_MJ) >= 1) &&
- (getStudents (g, currentPlayer, STUDENT_MTV) >= 1) &&
- (getStudents (g, currentPlayer, STUDENT_MMONEY) >= 1))
- == FALSE ) {
- isLegal = FALSE;
- }
- } else if ((a.actionCode == OBTAIN_PUBLICATION) ||
- (a.actionCode == OBTAIN_IP_PATENT)) {
- isLegal = FALSE;
- } else if (a.actionCode == RETRAIN_STUDENTS) {
- //is not THD
- if (a.disciplineFrom == STUDENT_THD) {
- isLegal = FALSE;
- }
- //has enough students
- if (getStudents (g, currentPlayer, a.disciplineFrom) <
- getExchangeRate
- (g, currentPlayer, a.disciplineFrom, a.disciplineTo)) {
- isLegal = FALSE;
- }
- } else {
- isLegal = FALSE;
- }
- return isLegal;
- }
- //-------Player stats
- int getKPIpoints (Game g, int player) {
- return g->KPI[player];
- }
- int getARCs (Game g, int player) {
- playerCodes codes = getPlayerCodes (g, player);
- int numArcs = 0;
- int i = 0;
- while (i < NUM_ARCS) {
- if (g->arcs[i].content == codes.playersARC) {
- numArcs++;
- }
- i++;
- }
- return numArcs;
- }
- int getGO8s (Game g, int player) {
- playerCodes codes = getPlayerCodes (g, player);
- int numGO8s = 0;
- int i = 0;
- while (i < NUM_VERTICES) {
- if (g->vertices[i].content == codes.playersGO8) {
- numGO8s++;
- }
- i++;
- }
- return numGO8s;
- }
- int getCampuses (Game g, int player) {
- playerCodes codes = getPlayerCodes (g, player);
- int numCampuses = 0;
- int i = 0;
- while (i < NUM_VERTICES) {
- if (g->vertices[i].content == codes.playersCampus) {
- numCampuses++;
- }
- i++;
- }
- return numCampuses;
- }
- int getIPs (Game g, int player) {
- return g->patents[player];
- }
- int getPublications (Game g, int player) {
- return g->publications[player];
- }
- int getStudents (Game g, int player, int discipline) {
- assert (player != NO_ONE);
- return g->students[player][discipline];
- }
- //------Exchange Rate
- int getExchangeRate (Game g, int player,
- int disciplineFrom, int disciplineTo) {
- playerCodes codes = getPlayerCodes (g, player);
- int i;
- int exchangeRate = 4;
- i = 0;
- while ((i < NUM_RETRAININGCENTRES) && (exchangeRate > 2)) {
- int arc = g->retrainingCentres[i].arc;
- int leftPoint = g->arcs[arc].leftPoint;
- int rightPoint = g->arcs[arc].rightPoint;
- int leftContent = g->vertices[leftPoint].content;
- int rightContent = g->vertices[rightPoint].content;
- if ((leftContent == codes.playersCampus) ||
- (rightContent == codes.playersCampus) ||
- (leftContent == codes.playersGO8) ||
- (rightContent == codes.playersGO8)) {
- int retrainingType = g->retrainingCentres[i].retrainingType;
- int discipline = g->retrainingCentres[i].discipline;
- if ((retrainingType == 2) && (discipline == disciplineFrom)) {
- exchangeRate = 2;
- } else if (retrainingType == 3) {
- exchangeRate = 3;
- }
- }
- i++;
- }
- return exchangeRate;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement