Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Written by Stanley Hon
- // 2011 15-MAY-2011
- // An Artificial Intelligence for the COMP1917 Task3
- // University Simulation
- // N A T A S H A - T H R E E - P O I N T - F I V E
- // 3.5
- /* REFERENCE MATERIAL
- typedef struct _action {
- int actionCode; // see #defines above
- path destination; // if the action operates on a vertex or ARC this
- // specifies *which* vertex or path. unused
- // otherwise
- int disciplineFrom; // used for the retrain students action
- int disciplineTo; // used for the retrain students action
- } action;
- */
- /*
- * PHASE 1 - Passing AI
- * COMPLETE
- */
- /*
- * PHASE 2 - SPAM spinoffs
- * COMPLETE BUT NEEDS PROOFING
- */
- /*
- * PHASE 3 - SPAM ARC grants
- * IN TESTING
- */
- #include <stdio.h>
- #include <assert.h>
- #include <string.h>
- #include <stdlib.h>
- #include "Game.h"
- #include "ai.h"
- /*=========== ARC GRANT RELATED =============*/
- action tryGetARC (int numBPS, int numBQN, int numMJ, int numMTV,
- int numMMONEY, int exchangeRate, Game g);
- int getMissingForARC (int numBQN, int numBPS);
- int ARCAvailable (int numBQN, int numBPS);
- int rankARC (path p);
- action getARCaction (Game g);
- /*=========== SPINOFF RELATED ===============*/
- action tryGetSpinoff (int numBPS, int numBQN, int numMJ, int numMTV,
- int numMMONEY, int exchangeRate);
- int spinoffAvailable (int numMJ, int numMTV, int numMMONEY);
- int getMissingForSpinoff (int numMJ, int numMTV, int numMMONEY);
- action decideAction (Game g) {
- assert (g!=NULL);
- int natasha = getWhoseTurn(g);
- int numBPS = getStudents (g, natasha, STUDENT_BPS);
- int numBQN = getStudents (g, natasha, STUDENT_BQN);
- int numMJ = getStudents (g, natasha, STUDENT_MJ);
- int numMTV = getStudents (g, natasha, STUDENT_MTV);
- int numMMONEY = getStudents (g, natasha, STUDENT_MMONEY);
- printf ("\nNumber of BPS = %d", numBPS);
- printf ("\nNumber of BQN = %d", numBQN);
- printf ("\nNumber of MJ = %d", numMJ);
- printf ("\nNumber of MTV = %d", numMTV);
- printf ("\nNumber of MMONEY = %d\n\n", numMMONEY);
- action nextAction;
- nextAction.actionCode = PASS;
- // TODO: Implement a way to give the function access to
- // conversion rates
- // TODO: Give the function a way to choose most efficient trade
- // method.
- if (getTurnNumber (g)) {
- nextAction = tryGetARC (numBPS, numBQN, numMJ,
- numMTV, numMMONEY,
- 3, g);
- } else {
- /*nextAction = tryGetSpinoff (numBPS, numBQN, numMJ, numMTV,
- numMMONEY, 3);
- */
- }
- return nextAction;
- }
- // **********************************************************
- // ************ GENERAL HELPER FUNCTIONS ********************
- // **********************************************************
- // ************************************************************
- // ************ ARC GRANT HELPER FUNCTIONS ********************
- // ************************************************************
- action getARCaction (Game g) {
- action a;
- a.actionCode = OBTAIN_ARC;
- // initialise array of possible paths
- char * possibilities[PATH_LIMIT]; // Array of strings
- char * testPath[PATH_LIMIT]; // A single path
- testPath = "";
- int x = 0;
- while (x < PATH_LIMIT) {
- possibilities[x] = " ";
- x++;
- }
- // Here is the path that goes through all possible edges
- path allARC = "RRLRLBRLRLRRRRLRLRLLLLRLRLRLRRRRLRLRLRLLLLRLRLRRRRL"
- "RLRRLRLRRRRLRLRLLLLRLRLRLRRRRLRLRLRLLLLRLRLRRRRLRL";
- // Iterate through all edges to record possibilities
- int possibilitiesPosition = 0; // stores where we store possibilties
- int maxARCposition = (strlen (allARC)); // stops overflow
- int position = 6; // move path to starting edge
- while (position < maxARCposition) {
- strncpy (*testPath, allARC, position); // put new test string in
- // using position as index since position is a count, and we want
- // to reach the next index in the array.
- // testPath[position] = EOF;
- strcpy (a.destination, *testPath); // put test string into action
- // if the path is a legal move, put it into possibilities
- if (isLegalAction (g, a) == TRUE) {
- strcpy (possibilities[possibilitiesPosition], *testPath);
- possibilitiesPosition++;
- }
- // try one more step in the path
- position++;
- }
- // make sure atleast one possibility, if not return empty path
- if (strcmp (possibilities[0], " ") == 0) {
- strcpy (a.destination, " ");
- // otherwise, literally go and put the path into the action
- } else {
- strcpy (a.destination, possibilities[0]);
- }
- return a;
- }
- int rankARC (path p) {
- return 0; // should return ranking
- }
- action tryGetARC (int numBPS, int numBQN, int numMJ, int numMTV,
- int numMMONEY, int exchangeRate, Game g) {
- action a;
- a.disciplineFrom = -1;
- a.disciplineTo = -1;
- int haveDecided = FALSE;
- while (haveDecided == FALSE) {
- if (ARCAvailable (numBQN, numBPS) == TRUE) {
- action test;
- test = getARCaction (g);
- if (strcmp (test.destination, " ") != 0) {
- haveDecided = TRUE;
- a = test;
- } else {
- a.actionCode = PASS;
- haveDecided = TRUE;
- }
- } else {
- // identify missing resources for ARC
- int missing = getMissingForARC (numBQN, numBPS);
- // You can trade BQN or BQS for needed resources if we have
- // one left after we trade.
- if (numBQN >= exchangeRate + 1) {
- // printf ("\nI can trade BQNs");
- a.actionCode = RETRAIN_STUDENTS;
- a.disciplineFrom = STUDENT_BQN;
- a.disciplineTo = missing;
- haveDecided = TRUE;
- } else if (numBPS >= exchangeRate + 1) {
- // printf ("\nI can trade BPSs");
- a.actionCode = RETRAIN_STUDENTS;
- a.disciplineFrom = STUDENT_BPS;
- a.disciplineTo = missing;
- haveDecided = TRUE;
- } else if (numMJ >= exchangeRate) {
- // printf ("\nI can trade MJs");
- a.actionCode = RETRAIN_STUDENTS;
- a.disciplineFrom = STUDENT_MJ;
- a.disciplineTo = missing;
- haveDecided = TRUE;
- } else if (numMTV >= exchangeRate) {
- // printf ("\nI can trade MTVs");
- a.actionCode = RETRAIN_STUDENTS;
- a.disciplineFrom = STUDENT_MTV;
- a.disciplineTo = missing;
- haveDecided = TRUE;
- } else if (numMMONEY >= exchangeRate) {
- // printf ("\nI can trade MMONEYs");
- a.actionCode = RETRAIN_STUDENTS;
- a.disciplineFrom = STUDENT_MMONEY;
- a.disciplineTo = missing;
- haveDecided = TRUE;
- // no non-spinoff required resources are available for trade
- } else {
- a.actionCode = PASS;
- haveDecided = TRUE;
- }
- }
- }
- return a;
- }
- // returns whether or not a ARC is afforable
- int ARCAvailable (int numBQN, int numBPS) {
- int available = FALSE;
- if (numBQN >= 1 && numBPS >= 1) {
- available = TRUE;
- }
- return available;
- }
- // returns a type of student that is missing to achieve a ARC
- int getMissingForARC (int numBQN, int numBPS) {
- int missing = -1;
- if (numBQN == 0) {
- missing = STUDENT_BQN;
- } else if (numBPS == 0) {
- missing = STUDENT_BPS;
- }
- return missing;
- }
- // *****************************************************************
- // ************ OBTAIN_SPINOFF HELPER FUNCTIONS ********************
- // *****************************************************************
- action tryGetSpinoff (int numBPS, int numBQN, int numMJ, int numMTV,
- int numMMONEY, int exchangeRate) {
- action a;
- a.disciplineFrom = -1;
- a.disciplineTo = -1;
- int haveDecided = FALSE;
- while (haveDecided == FALSE) {
- if (spinoffAvailable (numMJ, numMTV, numMMONEY) == TRUE) {
- /*
- printf ("\nNumber of BPS = %d", numBPS);
- printf ("\nNumber of BQN = %d", numBQN);
- printf ("\nNumber of MJ = %d", numMJ);
- printf ("\nNumber of MTV = %d", numMTV);
- printf ("\nNumber of MMONEY = %d", numMMONEY);
- */
- a.actionCode = START_SPINOFF;
- haveDecided = TRUE;
- } else {
- // identify missing resources
- int missing = getMissingForSpinoff (numMJ, numMTV, numMMONEY);
- if (numBQN >= exchangeRate) {
- // printf ("\nI can trade BQNs");
- a.actionCode = RETRAIN_STUDENTS;
- a.disciplineFrom = STUDENT_BQN;
- a.disciplineTo = missing;
- haveDecided = TRUE;
- } else if (numBPS >= exchangeRate) {
- // printf ("\nI can trade BPSs");
- a.actionCode = RETRAIN_STUDENTS;
- a.disciplineFrom = STUDENT_BPS;
- a.disciplineTo = missing;
- haveDecided = TRUE;
- } else if (numMJ >= exchangeRate + 1) {
- // printf ("\nI can trade MJs");
- a.actionCode = RETRAIN_STUDENTS;
- a.disciplineFrom = STUDENT_MJ;
- a.disciplineTo = missing;
- haveDecided = TRUE;
- } else if (numMTV >= exchangeRate + 1) {
- // printf ("\nI can trade MTVs");
- a.actionCode = RETRAIN_STUDENTS;
- a.disciplineFrom = STUDENT_MTV;
- a.disciplineTo = missing;
- haveDecided = TRUE;
- } else if (numMMONEY >= exchangeRate + 1) {
- // printf ("\nI can trade MMONEYs");
- a.actionCode = RETRAIN_STUDENTS;
- a.disciplineFrom = STUDENT_MMONEY;
- a.disciplineTo = missing;
- haveDecided = TRUE;
- // no resources are available for trade
- } else {
- a.actionCode = PASS;
- haveDecided = TRUE;
- }
- }
- }
- return a;
- }
- // returns whether or not a spinoff is afforable
- int spinoffAvailable (int numMJ, int numMTV, int numMMONEY) {
- int available = FALSE;
- if (numMJ >= 1 && numMTV >= 1 && numMMONEY >= 1) {
- available = TRUE;
- }
- return available;
- }
- // returns a type of student that is missing to achieve a spinoff
- int getMissingForSpinoff (int numMJ, int numMTV, int numMMONEY) {
- int missing = -1;
- if (numMJ == 0) {
- missing = STUDENT_MJ;
- } else if (numMTV == 0) {
- missing = STUDENT_MTV;
- } else if (numMMONEY == 0) {
- missing = STUDENT_MMONEY;
- }
- return missing;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement