//Tests conditions for path, which needs 1 BPS and 1 BQN //Ensure paths are connected to other paths & are legal paths //Does not leave into the sea, and path is vacant int arcConditions (Game g, action a, int player) { int answer = TRUE; char *path = a.destination; edge arc; arc.pointA = pathMovement (path); arc.pointB = movement (arc.pointA, BACK); if ((getStudents (g, player, STUDENT_BPS) >= 1) && (getStudents (g, player, STUDENT_BQN) >= 1) && (a.destination[0] != BACK)) { //doesn't move back into the sea answer = TRUE; } else { answer = FALSE; } //check if they're inside the boundaries if (answer == TRUE && abs (arc.pointA.x) >= 3 && abs (arc.pointA.y) >= 3 && abs (arc.pointA.z) >= 3) { answer = FALSE; } //Checks to see that they are actual adjacent vertexes if (answer == TRUE && abs(arc.pointA.x+arc.pointA.y+arc.pointA.z) != 2 && (arc.pointA.x+arc.pointA.y+arc.pointA.z+arc.pointB.x+arc.pointB.y+arc.pointB.z != 0) && (getARC (g, a.destination) != VACANT_ARC)) { answer = FALSE; } return answer; }
/* * Start transmision... * */ void nRF24l01plus::startPTX() { // if(getCE() == false || isPWRUP() == false)return; if(isRX_MODE())return; tMsgFrame * packetToSend = getTXpacket(); if(packetToSend == NULL)return; //packet found in TX that is not ACK packet //load with TX address packetToSend->Address = getTXaddress(); emit sendMsgToET((tMsgFrame*)packetToSend); //check if ack expected if((packetToSend->Packet_Control_Field.NP_ACK == 0) && (getARC()!=0)) { //set for ACK recipt.. ACK_address = getTXaddress(); waitingForACK = true; clearARC_CNT(); theTimer->start(getARD()*10); TXpacket = packetToSend; } else {//no ack last transmited is the one that is if(lastTransmited != NULL) { delete lastTransmited; lastTransmited = NULL; } lastTransmited = packetToSend; setTX_DS_IRQ(); } }
//DONE //must have an arc connected to it & not blocked by campus //given destination --> if there is an arc on it --> cannot place //given resources static int isLegalBuildARC(Game g, action a) { int isLegal = TRUE; int arcTests[3] = {TRUE, TRUE, TRUE}; int player = getWhoseTurn(g); coordinates xycoord1; coordinates xycoord2; //if path is actual a legal path if(isLegalPathToARC(a.destination) == FALSE) { isLegal = FALSE; } //if already owned if(isLegal == TRUE) { if(getARC(g, a.destination) != 0) { isLegal = FALSE; } } //if not enough resources if(g->students[player][STUDENT_BPS] < 1 || g->students[player][STUDENT_BQN] < 1) { isLegal = FALSE; } //Board state exceptions xycoord1 = getCoords(a.destination); xycoord2 = newCoords(xycoord1, 'B'); //test if arc has campus next to it //test if has path but not blocked by campus //get what coordinate it is on and then get next coordinate //then test if next coordinates has arcs next to them if(hasVertexARC(g, xycoord1) == FALSE && (!(getCampusWithCoord(g, xycoord2) == player || getCampusWithCoord(g, xycoord2) == player + 3))) { arcTests[0] = FALSE; } if(hasVertexARC(g, xycoord2) == FALSE && (!(getCampusWithCoord(g, xycoord1) == player || getCampusWithCoord(g, xycoord1) == player + 3))) { arcTests[1] = FALSE; } if((hasVertexARC(g, xycoord1) == FALSE && hasVertexARC(g, xycoord2) == FALSE)) { arcTests[2] = FALSE; } if(isLegal == TRUE) { if(!(arcTests[0] || arcTests[1] || arcTests[2])) { isLegal = FALSE; } } return isLegal; }
//Returns true/false, tests conditions for campus, // which needs 1 of each but THD or M$, // and no two campuses can be on adjacent vertexes int cmpsConditions (Game g, action a, int player) { int answer = TRUE; char *path = a.destination; coord vertex = pathMovement (path); coord adj = movement (vertex, BACK); if ((getStudents (g, player, STUDENT_BPS) >= 1) && (getStudents (g, player, STUDENT_BQN) >= 1) && (getStudents (g, player, STUDENT_MJ) >= 1) && (getStudents (g, player, STUDENT_MTV) >= 1) && (getCampus (g, a.destination) == VACANT_VERTEX) && (getARC (g, a.destination) != VACANT_ARC)) { answer = TRUE; } else { answer = FALSE; } //check if they're inside the boundaries if (answer == TRUE && abs (vertex.x) >= 3 && abs (vertex.y) >= 3 && abs (vertex.z) >= 3) { answer = FALSE; } //Checks for adjacent campuses which would be illegal if (answer == TRUE && g->gameBoard.campus[adj.x][adj.y][adj.z] != NO_ONE) { answer = FALSE; } //Check that it's an actual vertex if (answer == TRUE && abs (vertex.x+vertex.y+vertex.z) != 2) { answer = FALSE; } return answer; }
void nRF24l01plus::noACKalarm() { if(getARC_CNT() == getARC()) {//number of max retransmits acchived //failed to reach targe set aproprite flags PLOS_CNT_INC(); setMAX_RT_IRQ(); if(lastTransmited != NULL) delete lastTransmited; lastTransmited = TXpacket; theTimer->stop(); waitingForACK = false; } else {//retransmit message and increasse reTransCounter and start timer again emit sendMsgToET(TXpacket); //setup wait for ack... ARC_CNT_INC(); theTimer->start(getARD() * 10);//10ms real life 250us } }
void testInitialisation (Game testGame){ action testAction; printf ("Testing initial conditions.\n"); printf ("Testing getDiscipline.\n"); assert (getDiscipline(testGame, 0) == STUDENT_BQN); assert (getDiscipline(testGame, 1) == STUDENT_MMONEY); assert (getDiscipline(testGame, 2) == STUDENT_MJ); assert (getDiscipline(testGame, 3) == STUDENT_MMONEY); assert (getDiscipline(testGame, 4) == STUDENT_MJ); assert (getDiscipline(testGame, 5) == STUDENT_BPS); assert (getDiscipline(testGame, 6) == STUDENT_MTV); assert (getDiscipline(testGame, 7) == STUDENT_MTV); assert (getDiscipline(testGame, 8) == STUDENT_BPS); assert (getDiscipline(testGame, 9) == STUDENT_MTV); assert (getDiscipline(testGame, 10) == STUDENT_BQN); assert (getDiscipline(testGame, 11) == STUDENT_MJ); assert (getDiscipline(testGame, 12) == STUDENT_BQN); assert (getDiscipline(testGame, 13) == STUDENT_THD); assert (getDiscipline(testGame, 14) == STUDENT_MJ); assert (getDiscipline(testGame, 15) == STUDENT_MMONEY); assert (getDiscipline(testGame, 16) == STUDENT_MTV); assert (getDiscipline(testGame, 17) == STUDENT_BQN); assert (getDiscipline(testGame, 18) == STUDENT_BPS); printf ("Testing getDiceValue\n"); assert (getDiceValue(testGame, 0) == 9); assert (getDiceValue(testGame, 1) == 10); assert (getDiceValue(testGame, 2) == 8); assert (getDiceValue(testGame, 3) == 12); assert (getDiceValue(testGame, 4) == 6); assert (getDiceValue(testGame, 5) == 5); assert (getDiceValue(testGame, 6) == 3); assert (getDiceValue(testGame, 7) == 11); assert (getDiceValue(testGame, 8) == 3); assert (getDiceValue(testGame, 9) == 11); assert (getDiceValue(testGame, 10) == 4); assert (getDiceValue(testGame, 11) == 6); assert (getDiceValue(testGame, 12) == 4); assert (getDiceValue(testGame, 13) == 7); assert (getDiceValue(testGame, 14) == 9); assert (getDiceValue(testGame, 15) == 2); assert (getDiceValue(testGame, 16) == 8); assert (getDiceValue(testGame, 17) == 10); assert (getDiceValue(testGame, 18) == 5); printf ("Testing Most ARC\n"); assert (getMostARCs(testGame) == NO_ONE); printf ("Testing most publications\n"); assert (getMostPublications (testGame) == NO_ONE); printf ("Testing turnNumber\n"); assert (getTurnNumber(testGame) == -1); printf ("Testing whoseTurn.\n"); assert (getWhoseTurn (testGame) == NO_ONE); printf("Testing getCampus (content of given vertex) \n"); assert (getCampus (testGame, "RLBLR") == VACANT_VERTEX); assert (getCampus (testGame, "RLBBR") == VACANT_VERTEX); assert (getCampus (testGame, "LRL") == VACANT_VERTEX); printf("Testing getARC (content of given edge) \n"); assert (getARC (testGame, "L") == VACANT_ARC); assert (getARC (testGame, "R") == VACANT_ARC); assert (getARC (testGame, "LR") == VACANT_ARC); assert (getARC (testGame, "RR") == VACANT_ARC); printf ("Testing isLegalAction\n"); assert (isLegalAction (testGame, testAction) == FALSE); printf ("Testing KPI points\n"); assert (getKPIpoints (testGame, UNI_A) == 0); assert (getKPIpoints (testGame, UNI_B) == 0); assert (getKPIpoints (testGame, UNI_C) == 0); printf ("Testing getARCs (number of ARC grants) \n"); assert (getARCs (testGame, UNI_A) == 0); assert (getARCs (testGame, UNI_B) == 0); assert (getARCs (testGame, UNI_C) == 0); printf ("Testing GO8 (number of G08 campus) \n"); assert (getGO8s (testGame, UNI_A) == 0); assert (getGO8s (testGame, UNI_B) == 0); assert (getGO8s (testGame, UNI_C) == 0); printf ("Testing getCampuses (number of normal campuses)\n"); assert (getCampuses (testGame, UNI_A) == 2); assert (getCampuses (testGame, UNI_B) == 2); assert (getCampuses (testGame, UNI_C) == 2); printf ("Testing IPs (number of IP patent) \n"); assert (getIPs(testGame, UNI_A) == 0); assert (getIPs(testGame, UNI_B) == 0); assert (getIPs(testGame, UNI_C) == 0); printf ("Testing publications\n"); assert (getPublications(testGame, UNI_A) == 0); assert (getPublications(testGame, UNI_B) == 0); assert (getPublications(testGame, UNI_C) == 0); printf ("Testing number of students of UNI_A\n"); assert (getStudents (testGame, UNI_A, STUDENT_THD) == 0); assert (getStudents (testGame, UNI_A, STUDENT_BPS) == 3); assert (getStudents (testGame, UNI_A, STUDENT_BQN) == 3); assert (getStudents (testGame, UNI_A, STUDENT_MJ) == 1); assert (getStudents (testGame, UNI_A, STUDENT_MTV) == 1); assert (getStudents (testGame, UNI_A, STUDENT_MMONEY) == 1); printf ("Testing number of students of UNI_B\n"); assert (getStudents (testGame, UNI_B, STUDENT_THD) == 0); assert (getStudents (testGame, UNI_B, STUDENT_BPS) == 3); assert (getStudents (testGame, UNI_B, STUDENT_BQN) == 3); assert (getStudents (testGame, UNI_B, STUDENT_MJ) == 1); assert (getStudents (testGame, UNI_B, STUDENT_MTV) == 1); assert (getStudents (testGame, UNI_B, STUDENT_MMONEY) == 1); printf ("Testing number of students of UNI_C\n"); assert (getStudents (testGame, UNI_C, STUDENT_THD) == 0); assert (getStudents (testGame, UNI_C, STUDENT_BPS) == 3); assert (getStudents (testGame, UNI_C, STUDENT_BQN) == 3); assert (getStudents (testGame, UNI_C, STUDENT_MJ) == 1); assert (getStudents (testGame, UNI_C, STUDENT_MTV) == 1); assert (getStudents (testGame, UNI_C, STUDENT_MMONEY) == 1); printf ("Testing getExchangeRate for UNI_A\n"); assert (getExchangeRate (testGame, UNI_A, STUDENT_BPS, STUDENT_BPS) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_BPS, STUDENT_BQN) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_BPS, STUDENT_MJ) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_BPS, STUDENT_MTV) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_BPS, STUDENT_MMONEY) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MMONEY, STUDENT_MMONEY) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MMONEY, STUDENT_BPS) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MMONEY, STUDENT_BQN) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MMONEY, STUDENT_MJ) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MMONEY, STUDENT_MTV) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MTV, STUDENT_MTV) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MTV, STUDENT_BPS) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MTV, STUDENT_BQN) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MTV, STUDENT_MJ) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MTV, STUDENT_MMONEY) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_BQN, STUDENT_BQN) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_BQN, STUDENT_BPS) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_BQN, STUDENT_MJ) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_BQN, STUDENT_MTV) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_BQN, STUDENT_MMONEY) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MJ, STUDENT_MJ) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MJ, STUDENT_BPS) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MJ, STUDENT_BQN) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MJ, STUDENT_MTV) == 3); assert (getExchangeRate (testGame, UNI_A, STUDENT_MJ, STUDENT_MMONEY) == 3); printf ("All tests passed\n\n"); }
action decideAction (Game g) { int turn = getWhoseTurn(g); path aiPathARC = {0}; if (turn == UNI_B) { strcpy(aiPathARC, "RRLRLL"); char prev = 'L'; char *next = 0; if (getARC(g, aiPathARC) != NO_ONE) { strcat(aiPathARC, "L"); } else { strcat(aiPathARC, "L"); int i = 0; while (i <= 30 && getARC(g, aiPathARC) != NO_ONE) { if (prev == 'R') { next = "L"; prev = 'L'; } else { next = "R"; prev = 'R'; } strcat(aiPathARC, next); i++; } } } else if (turn == UNI_C) { strcpy(aiPathARC, "LRLRLB"); char prev = 'B'; char *next = 0; int i = 0; while (i <= 30 && getARC(g, aiPathARC) != NO_ONE) { if (prev == 'R') { next = "L"; prev = 'L'; } else if (prev == 'L') { next = "R"; prev = 'R'; } else { next = "L"; prev = 'L'; } strcat(aiPathARC, next); i++; } } else { //for the first arc strcpy(aiPathARC, "R"); char prev = 'R'; char *next = 0; int i = 0; while (i <= 30 && getARC(g, aiPathARC) != NO_ONE) { if (prev == 'R'){ next = "L"; prev = 'L'; } else if (prev == 'L') { next = "R"; prev = 'R'; } else { next = "L"; prev = 'L'; } strcat(aiPathARC, next); i++; } } path aiPathCampus = {0}; //next part is simialer to arc pathing above //but since 2 campuses can't be made adjacent it //works slightly differently //needs to be checked if (turn == UNI_B) { strcpy(aiPathCampus, "RRLRLLL"); char *next = "RL"; int i = 0; while (i <= 30 && getCampus(g, aiPathCampus) != NO_ONE){ strcat(aiPathCampus, next); i++; } } else if (turn == UNI_C) { strcpy(aiPathCampus, "LRLRLB"); char *next = "LR"; int i = 0; while (i <= 30 && getCampus(g, aiPathCampus) != NO_ONE){ strcat(aiPathCampus, next); i++; } } else { strcpy(aiPathCampus, "R"); char *next = "LR"; int i = 0; while (i <= 30 && getCampus(g, aiPathCampus) != NO_ONE) { strcat(aiPathCampus, next); i++; } } action nextAction; int actionChosen = FALSE; if(turn != NO_ONE ) { //Build Campus nextAction.actionCode = BUILD_CAMPUS; strncpy(nextAction.destination, aiPathCampus, PATH_LIMIT); if (isLegalAction(g, nextAction)) { actionChosen = TRUE; } //Build Arc nextAction.actionCode = OBTAIN_ARC; strncpy(nextAction.destination, aiPathARC, PATH_LIMIT); if (!actionChosen && isLegalAction(g, nextAction) == TRUE) { actionChosen = TRUE; } //Spinoff nextAction.actionCode = START_SPINOFF; if (!actionChosen && isLegalAction(g, nextAction) == TRUE) { actionChosen = TRUE; } //before pass when we can we should add something that can //convert students so we can do more of the above actions //(maybe a while loop encompasing all actions) //Pass if (!actionChosen) { nextAction.actionCode = PASS; } } return nextAction; }
int main(int argc, char *argv[]) { int discipline[6] = {STUDENT_THD, STUDENT_BPS, STUDENT_BQN, STUDENT_MJ, STUDENT_MTV, STUDENT_MMONEY} int dice[6] = {1, 2, 3, 4, 5, 6} newGame(*discipline[], dice[]); //Tests void makeAction action a; a.actionCode = BUILD_CAMPUS; a.destination = L; a.disciplineFrom = STUDENT_BPS; a.disciplineTo = STUDENT_MTV; assert(a.actionCode == BUILD_CAMPUS); assert(a.destination == L); assert(a.disciplineFrom == STUDENT_BPS); assert(a.discplineTo == STUDENT_MTV); //Tests void throwDice assert(diceScore >= 2 && diceScore <= 12); //Tests int getDiscipline assert (getDiscipline(Game g, 0) == STUDENT_BGN); assert (getDiscipline(Game g, 1) == STUDENT_MMONEY); assert (getDiscipline(Game g, 2) == STUDENT_MJ); assert (getDiscipline(Game g, 3) == STUDENT_MMONEY); assert (getDiscipline(Game g, 4) == STUDENT_MJ); assert (getDiscipline(Game g, 5) == STUDENT_BPS); assert (getDiscipline(Game g, 6) == STUDENT_MTV); assert (getDiscipline(Game g, 7) == STUDENT_MTV); assert (getDiscipline(Game g, 8) == STUDENT_BPS); assert (getDiscipline(Game g, 9) == STUDENT_MTV); assert (getDiscipline(Game g, 10) == STUDENT_BQN); assert (getDiscipline(Game g, 11) == STUDENT_MJ); assert (getDiscipline(Game g, 12) == STUDENT_BQN); assert (getDiscipline(Game g, 13) == STUDENT_THD); assert (getDiscipline(Game g, 14) == STUDENT_MJ); assert (getDiscipline(Game g, 15) == STUDENT_MMONEY); assert (getDiscipline(Game g, 16) == STUDENT_MTV); assert (getDiscipline(Game g, 17) == STUDENT_BQN); assert (getDiscipline(Game g, 18) == STUDENT_BPS); //Tests int getDiceValue assert (getDiceValue(Game g, 0) == 9); assert (getDiceVale(Game g, 1) == 10); assert (getDicevalue(Game g, 2) == 8); assert (getDiceValue(Game g, 3) == 12); assert (getDiceValue(Game g, 4) == 6); assert (getDiceValue(Game g, 5) == 5); assert (getDiceValue(Game g, 6) == 3); assert (getDiceValue(Game g, 7) == 11); assert (getDiceValue(Game g, 8) == 3); assert (getDiceValue(Game g, 9) == 11); assert (getDiceValue(Game g, 10) == 4); assert (getDiceValue(Game g, 11) == 6); assert (getDiceValue(Game g, 12) == 4); assert (getDiceValue(Game g, 13) == 7); assert (getDiceValue(Game g, 14) == 9); assert (getDiceValue(Game g, 15) == 2); assert (getDiceValue(Game g, 16) == 8); assert (getDiceValue(Game g, 17) == 10); assert (getDiceValue(Game g, 18) == 5); //Tests int getMostARCs (Game g) assert(getMostARCs(newGame) == NO_ONE); //Tests int getMostPublications (Game g) assert(getMostPublications(newGame) == No_ONE); //Tests int getTurnNumber (Game g) assert(getTurnNumber(newGame) == -1); //Tests int getWhoseTurn (Game g) assert(getWhoseTurn(newGame) == NO_ONE); //Tests int getCampus(Game g, path pathToVertex) assert (getCampus(Game g, path {\0}) == CAMPUS_A); assert (getCampus(Game g, path {‘R’, ‘R’, ‘L’, ‘R’, ‘L’,\0}) == CAMPUS_B); assert (getCampus(Game g, path {‘L’, ‘R’, ‘L’, ‘R’, ‘L’, ‘R’,\0}) == CAMPUS_C); //Tests int getARC(Game g, path pathToEdge) assert (getARC(Game g, path {\0}) == VACANT_ARC); assert (getARC(Game g, path {‘R’,\0}) == ARC A); assert (getARC(Game g, path {‘R’, ‘R’, ‘L’, ‘R’, ‘L’, ‘L’,\0}) == ARC B); assert (getARC(Game g, path {‘L’, ‘R’, ‘L’, ‘R’, ‘L’, ‘R’, ‘B’,\0}) == ARC C); //Tests int isLegalAction (Game g, action a) if (actionCode != PASS) { assert (actionCode >= 0 && actionCode <= 7); if (actionCode == BUILD_CAMPUS) { assert ((sizeof(destination)-1)/4 >= 0 && (sizeof(destination) - 1)/4 <= PATH_LIMIT); assert (getCampus(Game g, path destination) == VACANT_VERTEX); assert (getArc(Game g, (destination + 'B')) == ARC_A && getCampus(Game g, (destination + 'L')) == VACANT_VERTEX && getCampus(Game g, (destination + 'R')) == VACANT_VERTEX); assert (students[STUDENT_BPS] >= 1); assert (students[STUDENTS_BQN] >= 1); assert (students[STUDENTS_MJ] >= 1); assert (students[STUDENTS_MTV] >= 1); } if (actionCode == BUILD_GO8) { assert ((sizeof(destination)-1)/4 >= 0 && (sizeof(destination) - 1)/4 <= PATH_LIMIT); assert (getCampus(Game g, path destination) == CAMPUS_A); assert (students[STUDENTS_MJ] >= 2); assert (students[STUDENTS_MMONEY] >= 3); } if (actionCode == OBTAIN_ARC) { assert ((sizeof(destination)-1)/4 >= 0 && (sizeof(destination) - 1)/4 <= PATH_LIMIT); assert (getArc(Game g, (destination + 'B')) == ARC_A); } if (actionCode == START_SPINNOFF) { assert (students[STUDENT_MJ] >= 1); assert (students[STUDENT_MTV] >= 1); assert (students[STUDENT_MMONEY] >= 1); } if (actionCode == RETRAIN_STUDENTS) { assert (students[disciplineFrom] >= getExchangeRate(Game g, int player, int disciplineFrom, int disciplineTo)); assert (disciplineFrom != STUDENT_THD); assert (disciplineTo != STUDENT_THD); }
action decideAction(Game g) { vertex vertices[NUM_INT_VERTICES]; arc arcs[NUM_INT_ARCS]; int currentPlayer = getWhoseTurn(g); int myCampus = 0; int myGO8 = 0; int myARC = 0; if (currentPlayer == UNI_A) { myCampus = CAMPUS_A; myGO8 = GO8_A; myARC = ARC_A; } else if (currentPlayer == UNI_B) { myCampus = CAMPUS_B; myGO8 = GO8_B; myARC = ARC_B; } else if (currentPlayer == UNI_C) { myCampus = CAMPUS_C; myGO8 = GO8_C; myARC = ARC_C; } char allPaths[NUM_INT_VERTICES][PATH_LIMIT] = ALL_PATHS; char arcPaths[NUM_INT_ARCS - NUM_INT_VERTICES][PATH_LIMIT] = ARC_PATHS; // Populate database int i = 0; while (i < NUM_INT_VERTICES) { vertex newVertex; arc newArc; strcpy(newVertex.path, allPaths[i]); strcpy(newArc.path, allPaths[i]); newVertex.object = getCampus(g, allPaths[i]); newArc.object = getARC(g, allPaths[i]); vertices[i] = newVertex; arcs[i] = newArc; i++; } while (i < NUM_INT_ARCS) { arc newArc; strcpy(newArc.path, arcPaths[i - NUM_INT_VERTICES]); newArc.object = getARC(g, newArc.path); arcs[i] = newArc; i++; } // printf("Populated database\n"); // If we have more than 5 campuses, plan for building GO8s // If we have enough resources to build a GO8... // Upgrade the most valued campus to a GO8 // Note that this AI will only build ARCs and campuses together at the same time // If we have enough resources to build an ARC and campus... // Get a list of all the "edge" vertices // Iterate through each connecting edge vertices, and give them a value // based off the hexes they are connected to. // For every campus of already existing resource, a point is subtracted // Their values is summed of the vertices they're connected to, too. // Cumalative values are halved for every vertex travelled to a maximum of 4 jumps // Then pick the highest scoring sub-vertex. // If there are multiple highest scoring sub-vertices, pick the one with the lowest index // Find vertices we own int myVertices[NUM_INT_VERTICES]; // Array of vertices that we own i = 0; int numMyVertices = 0; while (i < NUM_INT_VERTICES) { myVertices[i] = -1; if (vertices[i].object == myCampus || vertices[i].object == myGO8) { myVertices[numMyVertices] = i; numMyVertices++; // printf("%d\n", i); } i++; } // printf("Found our vertices\n"); // Now scan through each vertex and store neighbours // Array of vertices that are accessible and aren't owned fromToArc considerations[NUM_INT_VERTICES][2]; i = 0; int numConsiderations = 0; while (i < numMyVertices) { trio neighbouring = getNeighbouringVertices(myVertices[i]); if (neighbouring.a >= 0 && vertices[neighbouring.a].object == VACANT_VERTEX) { int arcId = getArcIdFromVertices(myVertices[i], neighbouring.a); if (arcs[arcId].object == VACANT_ARC || arcs[arcId].object == myARC) { considerations[numConsiderations][0].from = myVertices[i]; considerations[numConsiderations][0].to = neighbouring.a; if (arcs[arcId].object == myARC) { considerations[numConsiderations][0].alreadyOwned = TRUE; } else { considerations[numConsiderations][0].alreadyOwned = FALSE; } numConsiderations++; } } if (neighbouring.b >= 0 && vertices[neighbouring.b].object == VACANT_VERTEX) { int arcId = getArcIdFromVertices(myVertices[i], neighbouring.b); if (arcs[arcId].object == VACANT_ARC || arcs[arcId].object == myARC) { considerations[numConsiderations][0].from = myVertices[i]; considerations[numConsiderations][0].to = neighbouring.b; if (arcs[arcId].object == myARC) { considerations[numConsiderations][0].alreadyOwned = TRUE; } else { considerations[numConsiderations][0].alreadyOwned = FALSE; } numConsiderations++; } } if (neighbouring.c >= 0 && vertices[neighbouring.c].object == VACANT_VERTEX) { int arcId = getArcIdFromVertices(myVertices[i], neighbouring.c); if (arcs[arcId].object == VACANT_ARC || arcs[arcId].object == myARC) { considerations[numConsiderations][0].from = myVertices[i]; considerations[numConsiderations][0].to = neighbouring.c; if (arcs[arcId].object == myARC) { considerations[numConsiderations][0].alreadyOwned = TRUE; } else { considerations[numConsiderations][0].alreadyOwned = FALSE; } numConsiderations++; } } i++; } fromToArc subConsiderations[NUM_INT_VERTICES][2]; i = 0; int numSubConsiderations = 0; while (i < numConsiderations) { trio neighbouring = getNeighbouringVertices(considerations[i][0].to); if (neighbouring.a >= 0 && vertices[neighbouring.a].object == VACANT_VERTEX) { // Check that it's not within any other verticie if (canBuildCampusOn(vertices, neighbouring.a)) { int arcId = getArcIdFromVertices(considerations[i][0].to, neighbouring.a); if (arcs[arcId].object == VACANT_ARC || arcs[arcId].object == myARC) { subConsiderations[numSubConsiderations][0] = considerations[i][0]; subConsiderations[numSubConsiderations][1] = considerations[i][1]; subConsiderations[numSubConsiderations][1].from = considerations[i][0].to; subConsiderations[numSubConsiderations][1].to = neighbouring.a; if (arcs[arcId].object == myARC) { subConsiderations[numSubConsiderations][1].alreadyOwned = TRUE; } else { subConsiderations[numSubConsiderations][1].alreadyOwned = FALSE; } numSubConsiderations++; } } } if (neighbouring.b >= 0 && vertices[neighbouring.b].object == VACANT_VERTEX) { // Check that it's not within any other verticie if (canBuildCampusOn(vertices, neighbouring.b)) { int arcId = getArcIdFromVertices(considerations[i][0].to, neighbouring.b); if (arcs[arcId].object == VACANT_ARC || arcs[arcId].object == myARC) { subConsiderations[numSubConsiderations][0] = considerations[i][0]; subConsiderations[numSubConsiderations][1] = considerations[i][1]; subConsiderations[numSubConsiderations][1].from = considerations[i][0].to; subConsiderations[numSubConsiderations][1].to = neighbouring.b; if (arcs[arcId].object == myARC) { subConsiderations[numSubConsiderations][1].alreadyOwned = TRUE; } else { subConsiderations[numSubConsiderations][1].alreadyOwned = FALSE; } numSubConsiderations++; } } } if (neighbouring.c >= 0 && vertices[neighbouring.c].object == VACANT_VERTEX) { // Check that it's not within any other verticie if (canBuildCampusOn(vertices, neighbouring.c)) { int arcId = getArcIdFromVertices(considerations[i][0].to, neighbouring.c); if (arcs[arcId].object == VACANT_ARC || arcs[arcId].object == myARC) { subConsiderations[numSubConsiderations][0] = considerations[i][0]; subConsiderations[numSubConsiderations][1] = considerations[i][1]; subConsiderations[numSubConsiderations][1].from = considerations[i][0].to; subConsiderations[numSubConsiderations][1].to = neighbouring.c; if (arcs[arcId].object == myARC) { subConsiderations[numSubConsiderations][1].alreadyOwned = TRUE; } else { subConsiderations[numSubConsiderations][1].alreadyOwned = FALSE; } numSubConsiderations++; } } } i++; } // printf("Determined considerations\n"); // Remove duplicate paths to the same vertex i = 0; int numPossibilities = 0; fromToArc possibilities[NUM_INT_VERTICES][2]; while (i < numSubConsiderations) { // First result dominates others, unless it has an alreadyOwned flag // printf("%d: %d\n", i, considerations[i][1].to); int search = subConsiderations[i][1].to; int removeAll = FALSE; if (search >= 0) { possibilities[numPossibilities][0] = subConsiderations[i][0]; possibilities[numPossibilities][1] = subConsiderations[i][1]; int j = 0; while (j < numSubConsiderations) { if (subConsiderations[j][1].to == search) { // Same if (!removeAll && (subConsiderations[j][0].alreadyOwned || subConsiderations[j][1].alreadyOwned)) { // Better option removeAll = TRUE; fromToArc pos1 = subConsiderations[j][0]; fromToArc pos2 = subConsiderations[j][1]; possibilities[numPossibilities][0] = pos1; possibilities[numPossibilities][1] = pos2; subConsiderations[j][1].to = -1; numPossibilities++; } subConsiderations[j][1].to = -1; } j++; } if (!removeAll) { numPossibilities++; } } i++; } // Now get the weight values of each consideration weightedVertex sortedWeights[numPossibilities]; i = 0; while (i < numPossibilities) { int weight = getRecursiveVertexWeight(g, vertices, myVertices, numMyVertices, possibilities[i][1].to); weightedVertex newVertex; newVertex.weight = weight; newVertex.arcPath[0] = possibilities[i][0]; newVertex.arcPath[1] = possibilities[i][1]; sortedWeights[i] = newVertex; i++; } sortWeights(sortedWeights, numPossibilities); // printf("Weighted considerations\n"); // Buy in order of weighting // i = 0; // while (i < numPossibilities) { // printf("%d\n", sortedWeights[i].arcPath[1].to); // i++; // } int attempt = 0; int domination = FALSE; if (numPossibilities == 0) { printf("Reached campus domination\n"); domination = TRUE; } int GO8domination = FALSE; if (domination) { // Build GO8s weightedVertex sortedMyCampuses[numMyVertices]; int numMyCampuses = 0; i = 0; while (i < numMyVertices) { if (vertices[myVertices[i]].object == myCampus) { int weight = getSingleVertexWeight(g, vertices, myVertices, numMyVertices, myVertices[i]); weightedVertex newVertex; newVertex.weight = weight; fromToArc newDestination = {0}; newDestination.to = myVertices[i]; newVertex.arcPath[0] = newDestination; sortedMyCampuses[numMyCampuses] = newVertex; numMyCampuses++; } i++; } sortWeights(sortedMyCampuses, numMyCampuses); int totalGO8s = getGO8s(g, UNI_A) + getGO8s(g, UNI_B) + getGO8s(g, UNI_C); if (numMyCampuses == 0 || totalGO8s >= 8) { printf("Reached GO8 Domiation\n"); GO8domination = TRUE; numMyCampuses = 0; } i = 0; while (i < numMyCampuses) { if (enoughToBuildGO8(g, currentPlayer)) { action go8Action; go8Action.actionCode = BUILD_GO8; strcpy(go8Action.destination, vertices[sortedMyCampuses[i].arcPath[0].to].path); if (isLegalAction(g, go8Action)) { return go8Action; printf("Built GO8 at %d\n", sortedMyCampuses[i].arcPath[0].to); } else { printf("Could not build GO8 at %d\n", sortedMyCampuses[i].arcPath[0].to); } } i++; } } if (GO8domination) { // Wow pls, make spinoffs while (enoughToStartSpinoff(g, currentPlayer)) { action spinoffAction; spinoffAction.actionCode = START_SPINOFF; return spinoffAction; } } while (attempt < numPossibilities && enoughToBuildCampus(g, currentPlayer, sortedWeights[attempt].arcPath)) { int firstArc = getArcIdFromVertices(sortedWeights[attempt].arcPath[0].from, sortedWeights[attempt].arcPath[0].to); if (firstArc < 0) { printf("Could not find path between %d and %d!\n", sortedWeights[attempt].arcPath[0].from, sortedWeights[attempt].arcPath[0].to); } else { if (!sortedWeights[attempt].arcPath[0].alreadyOwned) { action pathAction; pathAction.actionCode = OBTAIN_ARC; strcpy(pathAction.destination, arcs[firstArc].path); if (!isLegalAction(g, pathAction)) { printf("ARC between %d and %d is not legal?\n", sortedWeights[attempt].arcPath[0].from, sortedWeights[attempt].arcPath[0].to); } else { printf("Bought ARC between %d and %d\n", sortedWeights[attempt].arcPath[0].from, sortedWeights[attempt].arcPath[0].to); return pathAction; } } int secondArc = getArcIdFromVertices(sortedWeights[attempt].arcPath[1].from, sortedWeights[attempt].arcPath[1].to); if (secondArc < 0) { printf("Could not find path between %d and %d!\n", sortedWeights[attempt].arcPath[1].from, sortedWeights[attempt].arcPath[1].to); } else { if (!sortedWeights[attempt].arcPath[1].alreadyOwned) { action pathAction; pathAction.actionCode = OBTAIN_ARC; strcpy(pathAction.destination, arcs[secondArc].path); if (!isLegalAction(g, pathAction)) { printf("ARC between %d and %d is not legal?\n", sortedWeights[attempt].arcPath[1].from, sortedWeights[attempt].arcPath[1].to); } else { printf("Bought ARC between %d and %d\n", sortedWeights[attempt].arcPath[1].from, sortedWeights[attempt].arcPath[1].to); return pathAction; } } int vertexId = sortedWeights[attempt].arcPath[1].to; action campusAction; strcpy(campusAction.destination, vertices[vertexId].path); campusAction.actionCode = BUILD_CAMPUS; if (!isLegalAction(g, campusAction)) { printf("Campus at %d is not legal?\n", vertexId); } else { printf("Built campus at %d\n", vertexId); return campusAction; } } } attempt++; } action passAction; passAction.actionCode = PASS; return passAction; }
//returns path to campus to be built //returns NULL if cannot find a place to build static char * determineCampusPath(Game g, int player) { char * pathToCampus = malloc(PATH_LIMIT); pathToCampus = NULL; if (player == UNI_A) { if (getARC(g, "RL") == UNI_A) { if (getCampus(g, "RLL") || getCampus(g, "RLR") || getCampus(g, "RLB") || getCampus(g, "RL")) { } else { pathToCampus = "RL"; } } else if (getARC(g, "RLRLRLRLRLLLR") == UNI_A) { if (getCampus(g, "RLRLRLRLRLLLRL") || getCampus(g, "RLRLRLRLRLLLRR") || getCampus(g, "RLRLRLRLRLLLRB") || getCampus(g, "RLRLRLRLRLLLR")) { } else { pathToCampus = "RLRLRLRLRLLLR"; } } } else if (player == UNI_B) { if (getARC(g, "LRLRRLRLL") == UNI_B) { if (getCampus(g, "LRLRRLRLL") || getCampus(g, "LRLRRLRLR") || getCampus(g, "LRLRRLRLB") || getCampus(g, "LRLRRLRL")) { } else { pathToCampus = "LRLRRLRL"; } } if (getARC(g, "LRLRRLRLLL") == UNI_B) { if (getCampus(g, "LRLRRLRLLLL") || getCampus(g, "LRLRRLRLLLB") || getCampus(g, "LRLRRLRLLL")) { } else { pathToCampus = "LRLRRLRLLL"; } } } else if (player == UNI_C) { if (getARC(g, "RRLLRLRRLLLL") == UNI_C) { if (getCampus(g, "RRLLRLRRLLLL") || getCampus(g, "RRLLRLRRLLLLR") || getCampus(g, "RRLLRLRRLLLLB") || getCampus(g, "RRLLRLRRLLLLL")) { } else { pathToCampus = "RRLLRLRRLLLL"; } } if (getARC(g, "RRLLRLRRLLLLRL") == UNI_C) { if (getCampus(g, "RRLLRLRRLLLLRL") || getCampus(g, "RRLLRLRRLLLLRLL") || getCampus(g, "RRLLRLRRLLLLRLR") || getCampus(g, "RRLLRLRRLLLLRLB")) { } else { pathToCampus = "RRLLRLRRLLLLRL"; } } } return pathToCampus; }
//determines the path of an arc to be built //returns NULL if cannot find a path static char * determineARCPath(Game g, int player) { char * pathToARC = malloc(PATH_LIMIT); assert(pathToARC != NULL); pathToARC = NULL; if (player == UNI_A) { if (getARC(g, "RLRLRLRLRLLL") == NO_ONE) { pathToARC = "RLRLRLRLRLLL"; } else if (getARC(g, "RLRLRLRLRLLL") == ARC_A && getARC(g, "RLRLRLRLRLLLR") == NO_ONE) { pathToARC = "RLRLRLRLRLLLR"; } else if (getARC(g, "RLRLRLRLRLLL") == ARC_A && getARC(g, "RLRLRLRLRLLLL") == NO_ONE) { pathToARC = "RLRLRLRLRLLLL"; } else if (getARC(g, "RLRLRLRLRLLLL") == ARC_A && getARC(g, "RLRLRLRLRLLLLR") == NO_ONE) { pathTpARC = "RLRLRLRLRLLLLR"; } else if (getARC(g, "R") == NO_ONE) { pathToARC = "R"; } else if (getARC(g, "R") == ARC_A && getARC(g, "RL") == NO_ONE) { pathToARC = "RL"; } else if (getARC(g, "RL") == ARC_A && getARC(g, "RLR") == NO_ONE) { pathToARC = "RLR"; } /*} else if (getARC(g, "RL") == ARC_A && getARC(g, "RLR") == NO_ONE) { pathToARC = "RLR"; } else if (getARC(g, "RLR") == ARC_A && getARC(g, "RLRL") == NO_ONE) { pathToARC = "RLRL";*/ } else if (player == UNI_B) { if (getARC(g, "LRLRRLRLLR") == NO_ONE) { pathToARC = "LRLRRLRLLR"; } else if (getARC(g, "LRLRRLRLLR") == ARC_B && getARC(g, "LRLRRLRLL") == NO_ONE) { pathToARC = "LRLRRLRLL"; } else if (getARC(g, "LRLRRLRLL") == ARC_B && getARC(g, "LRLRRLRLLL") == NO_ONE) { pathToARC = "LRLRRLRLLL"; } else if (getARC(g, "RRLRLL") == NO_ONE) { pathToARC = "RRLRLL"; } else if (getARC(g, "RRLRLL") == ARC_B && getARC(g, "RRLRLLL") == NO_ONE) { pathToARC = "RRLRLLL"; } } else if (player == UNI_C) { if (getARC(g, "RRLLRLRRLLL") == NO_ONE) { pathToARC = "RRLLRLRRLLL"; } else if (getARC(g, "RRLLRLRRLLL") == ARC_C && getARC(g, "RRLLRLRRLLLL") == NO_ONE) { pathToARC = "RRLLRLRRLLLL"; } else if (getARC(g, "RRLLRLRRLLLL") == ARC_C && getARC(g, "RRLLRLRRLLLLR") == NO_ONE) { pathToARC = "RRLLRLRRLLLLR"; } else if (getARC(g, "RRLLRLRRLLLLR") == ARC_C && getARC(g, "RRLLRLRRLLLLRL") == NO_ONE) { pathToARC = "RRLLRLRRLLLLRL"; } } return pathToARC; }
int main(int argc, char *argv[]) { int disciplines[] = DEFAULT_DISCIPLINES; disciplines[2] = STUDENT_BPS; disciplines[7] = STUDENT_BPS; disciplines[11] = STUDENT_BQN; disciplines[16] = STUDENT_BQN; int dice[] = DEFAULT_DICE; int i = 0; int automation = TRUE; int diceRoll = 1; char *vertices[54]; char *sides[72]; //nanosecond seeding struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); populatePaths(vertices); populateSides(sides); printf("Automatic(1) or Human(0): "); scanf("%d", &automation); //seeding starts now srand((time_t)ts.tv_nsec); Game g = newGame(disciplines, dice); action a; while(getKPILeader(g) < 150 && getTurnNumber(g) < 9003) { if(automation == FALSE) { printf("Enter dice roll: "); scanf("%d", &diceRoll); } else { diceRoll = rand() % 10 + 2; } throwDice(g, diceRoll); printf("diceRoll = %d\n", diceRoll); printf("\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); printf("Turn: %d\n", getTurnNumber(g)); printf("Player %d's turn\n", getWhoseTurn(g)); printf("\n"); printf("You have %d ARCs\n", getARCs(g, getWhoseTurn(g))); i = 0; while(i < 72) { if(getARC(g, sides[i]) == getWhoseTurn(g)) { printf("You have an ARC on %s\\0\n", sides[i]); } i++; } printf("You have %d Campuses\n", getCampuses(g, getWhoseTurn(g))); printf("You have %d GO8s\n", getGO8s(g, getWhoseTurn(g))); i = 0; while(i < 54) { if(getCampus(g, vertices[i]) == getWhoseTurn(g)) { printf("You have a campus on %s\\0\n", vertices[i]); } if(getCampus(g, vertices[i]) == getWhoseTurn(g) + 3) { printf("You have a GO8 on %s\\0\n", vertices[i]); } i++; } printf("You have %d Publications\n", getPublications(g, getWhoseTurn(g))); printf("You have %d IP Patents\n", getIPs(g, getWhoseTurn(g))); printf("\n"); printf("Students:\n"); printf("You have %d THD Students\n", getStudents(g, getWhoseTurn(g), STUDENT_THD)); printf("You have %d BPS Students\n", getStudents(g, getWhoseTurn(g), STUDENT_BPS)); printf("You have %d BQN Students\n", getStudents(g, getWhoseTurn(g), STUDENT_BQN)); printf("You have %d MJ Students\n", getStudents(g, getWhoseTurn(g), STUDENT_MJ)); printf("You have %d MTV Students\n", getStudents(g, getWhoseTurn(g), STUDENT_MTV)); printf("You have %d MMONEY Students\n", getStudents(g, getWhoseTurn(g), STUDENT_MMONEY)); printf("\n"); if(automation == FALSE) { a = getHumanAction(); } else { a = getAIAction(g); } while(a.actionCode != PASS) { if(isLegalAction(g, a)) { if(a.actionCode == START_SPINOFF) { if(rand() % 3 > 1) { a.actionCode = OBTAIN_IP_PATENT; } else { a.actionCode = OBTAIN_PUBLICATION; } } makeAction(g, a); } else { printf("Illegal Action"); } printf("\n"); printf("Player %d's turn\n", getWhoseTurn(g)); printf("You have %d ARCs\n", getARCs(g, getWhoseTurn(g))); printf("You have %d Campuses\n", getCampuses(g, getWhoseTurn(g))); printf("You have %d GO8s\n", getGO8s(g, getWhoseTurn(g))); printf("You have %d Publications\n", getPublications(g, getWhoseTurn(g))); printf("You have %d IP Patents\n", getIPs(g, getWhoseTurn(g))); printf("\n"); printf("Students:\n"); printf("You have %d THD Students\n", getStudents(g, getWhoseTurn(g), STUDENT_THD)); printf("You have %d BPS Students\n", getStudents(g, getWhoseTurn(g), STUDENT_BPS)); printf("You have %d BQN Students\n", getStudents(g, getWhoseTurn(g), STUDENT_BQN)); printf("You have %d MJ Students\n", getStudents(g, getWhoseTurn(g), STUDENT_MJ)); printf("You have %d MTV Students\n", getStudents(g, getWhoseTurn(g), STUDENT_MTV)); printf("You have %d MMONEY Students\n", getStudents(g, getWhoseTurn(g), STUDENT_MMONEY)); printf("\n"); if(automation == FALSE) { a = getHumanAction(); } else { a = getAIAction(g); } } printf("Player 1 KPI points : %d\n", getKPIpoints(g, UNI_A)); printf("Player 2 KPI points : %d\n", getKPIpoints(g, UNI_B)); printf("Player 3 KPI points : %d\n", getKPIpoints(g, UNI_C)); } printf("Winner: Player %d\n", getWinner(g)); disposeGame(g); return EXIT_SUCCESS; }