Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.73 KB | None | 0 0
  1. // Written by Stanley Hon
  2. // 2011 15-MAY-2011
  3. // An Artificial Intelligence for the COMP1917 Task3
  4. // University Simulation
  5.  
  6. // N A T A S H A - T H R E E - P O I N T - F I V E
  7. // 3.5
  8.  
  9. /* REFERENCE MATERIAL
  10. typedef struct _action {
  11.    int actionCode;  // see #defines above
  12.    path destination; // if the action operates on a vertex or ARC this
  13.                      // specifies *which* vertex or path.  unused
  14.                      // otherwise
  15.    int disciplineFrom;  // used for the retrain students action
  16.    int disciplineTo;    // used for the retrain students action
  17. } action;
  18.  */
  19.  
  20. /*
  21.  * PHASE 1 - Passing AI
  22.  * COMPLETE
  23.  */
  24.  
  25. /*
  26.  * PHASE 2 - SPAM spinoffs
  27.  * COMPLETE BUT NEEDS PROOFING
  28.  */
  29.  
  30. /*
  31.  * PHASE 3 - SPAM ARC grants
  32.  * IN TESTING
  33.  */
  34.  
  35. #include <stdio.h>
  36. #include <assert.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39.  
  40. #include "Game.h"
  41. #include "ai.h"
  42.  
  43. /*=========== ARC GRANT RELATED =============*/
  44. action tryGetARC (int numBPS, int numBQN, int numMJ, int numMTV,
  45.                   int numMMONEY,  int exchangeRate, Game g);
  46. int getMissingForARC (int numBQN, int numBPS);
  47. int ARCAvailable (int numBQN, int numBPS);
  48. int rankARC (path p);
  49. action getARCaction (Game g);
  50.  
  51. /*=========== SPINOFF RELATED ===============*/
  52. action tryGetSpinoff (int numBPS, int numBQN, int numMJ, int numMTV,
  53.                       int numMMONEY, int exchangeRate);
  54. int spinoffAvailable (int numMJ, int numMTV, int numMMONEY);
  55. int getMissingForSpinoff (int numMJ, int numMTV, int numMMONEY);
  56.  
  57.  
  58. action decideAction (Game g) {
  59.    assert (g!=NULL);
  60.    
  61.    int natasha = getWhoseTurn(g);
  62.    int numBPS = getStudents (g, natasha, STUDENT_BPS);
  63.    int numBQN = getStudents (g, natasha, STUDENT_BQN);
  64.    int numMJ = getStudents (g, natasha, STUDENT_MJ);
  65.    int numMTV = getStudents (g, natasha, STUDENT_MTV);
  66.    int numMMONEY = getStudents (g, natasha, STUDENT_MMONEY);
  67.  
  68.    printf ("\nNumber of BPS = %d", numBPS);
  69.    printf ("\nNumber of BQN = %d", numBQN);
  70.    printf ("\nNumber of MJ = %d", numMJ);
  71.    printf ("\nNumber of MTV = %d", numMTV);
  72.    printf ("\nNumber of MMONEY = %d\n\n", numMMONEY);
  73.    action nextAction;
  74.    nextAction.actionCode = PASS;
  75.    
  76.    // TODO: Implement a way to give the function access to
  77.    // conversion rates
  78.    // TODO: Give the function a way to choose most efficient trade
  79.    // method.
  80.  
  81.    if (getTurnNumber (g)) {
  82.       nextAction = tryGetARC (numBPS, numBQN, numMJ,
  83.                               numMTV, numMMONEY,  
  84.                               3, g);
  85.    } else {
  86.       /*nextAction = tryGetSpinoff (numBPS, numBQN, numMJ, numMTV,
  87.                                   numMMONEY, 3);
  88.                                  */
  89.    }                                                    
  90.    return nextAction;
  91. }
  92.  
  93. // **********************************************************
  94. // ************ GENERAL HELPER FUNCTIONS ********************
  95. // **********************************************************
  96.  
  97.  
  98.  
  99. // ************************************************************
  100. // ************ ARC GRANT HELPER FUNCTIONS ********************
  101. // ************************************************************
  102. action getARCaction (Game g) {
  103.    action a;
  104.    a.actionCode = OBTAIN_ARC;
  105.  
  106.    // initialise array of possible paths
  107.    char * possibilities[PATH_LIMIT]; // Array of strings
  108.    char * testPath[PATH_LIMIT]; // A single path
  109.    testPath = "";
  110.    
  111.    int x = 0;
  112.    while (x < PATH_LIMIT) {
  113.       possibilities[x] = " ";
  114.       x++;
  115.    }
  116.    
  117.    // Here is the path that goes through all possible edges
  118.    path allARC = "RRLRLBRLRLRRRRLRLRLLLLRLRLRLRRRRLRLRLRLLLLRLRLRRRRL"
  119.                  "RLRRLRLRRRRLRLRLLLLRLRLRLRRRRLRLRLRLLLLRLRLRRRRLRL";
  120.                  
  121.    // Iterate through all edges to record possibilities
  122.    int possibilitiesPosition = 0; // stores where we store possibilties
  123.    int maxARCposition = (strlen (allARC)); // stops overflow
  124.    int position = 6; // move path to starting edge
  125.  
  126.    while (position < maxARCposition) {
  127.       strncpy (*testPath, allARC, position); // put new test string in
  128.       // using position as index since position is a count, and we want
  129.       // to reach the next index in the array.
  130.       // testPath[position] = EOF;
  131.       strcpy (a.destination, *testPath); // put test string into action
  132.  
  133.       // if the path is a legal move, put it into possibilities
  134.       if (isLegalAction (g, a) == TRUE) {
  135.          strcpy (possibilities[possibilitiesPosition], *testPath);
  136.          possibilitiesPosition++;
  137.       }
  138.       // try one more step in the path
  139.       position++;
  140.    }
  141.  
  142.    // make sure atleast one possibility, if not return empty path
  143.    if (strcmp (possibilities[0], " ") == 0) {
  144.       strcpy (a.destination, " ");
  145.    // otherwise, literally go and put the path into the action
  146.    } else {
  147.       strcpy (a.destination, possibilities[0]);
  148.    }
  149.    
  150.    return a;
  151. }
  152.      
  153. int rankARC (path p) {
  154.    return 0; // should return ranking
  155. }
  156.    
  157. action tryGetARC (int numBPS, int numBQN, int numMJ, int numMTV,
  158.                   int numMMONEY, int exchangeRate, Game g) {
  159.    action a;
  160.    a.disciplineFrom = -1;
  161.    a.disciplineTo = -1;
  162.    int haveDecided = FALSE;
  163.    
  164.    while (haveDecided == FALSE) {
  165.       if (ARCAvailable (numBQN, numBPS) == TRUE) {
  166.          action test;
  167.          test = getARCaction (g);
  168.          if (strcmp (test.destination, " ") != 0) {
  169.             haveDecided = TRUE;
  170.             a = test;
  171.          } else {
  172.             a.actionCode = PASS;
  173.             haveDecided = TRUE;
  174.          }
  175.       } else {
  176.          // identify missing resources for ARC
  177.          int missing = getMissingForARC (numBQN, numBPS);
  178.          // You can trade BQN or BQS for needed resources if we have
  179.          // one left after we trade.
  180.          if (numBQN >= exchangeRate + 1) {
  181.             // printf ("\nI can trade BQNs");
  182.             a.actionCode = RETRAIN_STUDENTS;
  183.             a.disciplineFrom = STUDENT_BQN;
  184.             a.disciplineTo = missing;
  185.             haveDecided = TRUE;
  186.          } else if (numBPS >= exchangeRate + 1) {
  187.             // printf ("\nI can trade BPSs");
  188.             a.actionCode = RETRAIN_STUDENTS;
  189.             a.disciplineFrom = STUDENT_BPS;
  190.             a.disciplineTo = missing;
  191.             haveDecided = TRUE;
  192.          } else if (numMJ >= exchangeRate) {
  193.             // printf ("\nI can trade MJs");
  194.             a.actionCode = RETRAIN_STUDENTS;
  195.             a.disciplineFrom = STUDENT_MJ;
  196.             a.disciplineTo = missing;
  197.             haveDecided = TRUE;
  198.          } else if (numMTV >= exchangeRate) {
  199.             // printf ("\nI can trade MTVs");
  200.             a.actionCode = RETRAIN_STUDENTS;
  201.             a.disciplineFrom = STUDENT_MTV;
  202.             a.disciplineTo = missing;
  203.             haveDecided = TRUE;
  204.          } else if (numMMONEY >= exchangeRate) {
  205.             // printf ("\nI can trade MMONEYs");
  206.             a.actionCode = RETRAIN_STUDENTS;
  207.             a.disciplineFrom = STUDENT_MMONEY;
  208.             a.disciplineTo = missing;
  209.             haveDecided = TRUE;
  210.             // no non-spinoff required resources are available for trade
  211.          } else {
  212.             a.actionCode = PASS;
  213.             haveDecided = TRUE;
  214.          }
  215.       }
  216.    }
  217.    
  218.    return a;
  219. }
  220.  
  221. // returns whether or not a ARC is afforable
  222. int ARCAvailable (int numBQN, int numBPS) {
  223.    int available = FALSE;
  224.    if (numBQN >= 1 && numBPS >= 1) {
  225.       available = TRUE;
  226.    }
  227.    return available;
  228. }
  229.  
  230. // returns a type of student that is missing to achieve a ARC
  231. int getMissingForARC (int numBQN, int numBPS) {
  232.    int missing = -1;
  233.    if (numBQN == 0) {
  234.       missing = STUDENT_BQN;
  235.    } else if (numBPS == 0) {
  236.       missing = STUDENT_BPS;
  237.    }
  238.    return missing;
  239. }
  240.  
  241. // *****************************************************************
  242. // ************ OBTAIN_SPINOFF HELPER FUNCTIONS ********************
  243. // *****************************************************************
  244. action tryGetSpinoff (int numBPS, int numBQN, int numMJ, int numMTV,
  245.                       int numMMONEY, int exchangeRate) {
  246.    action a;
  247.    a.disciplineFrom = -1;
  248.    a.disciplineTo = -1;
  249.    int haveDecided = FALSE;
  250.    
  251.    while (haveDecided == FALSE) {
  252.       if (spinoffAvailable (numMJ, numMTV, numMMONEY) == TRUE) {
  253.          /*
  254.          printf ("\nNumber of BPS = %d", numBPS);
  255.          printf ("\nNumber of BQN = %d", numBQN);
  256.          printf ("\nNumber of MJ = %d", numMJ);
  257.          printf ("\nNumber of MTV = %d", numMTV);
  258.          printf ("\nNumber of MMONEY = %d", numMMONEY);
  259.           */
  260.          a.actionCode = START_SPINOFF;
  261.          haveDecided = TRUE;
  262.       } else {
  263.          // identify missing resources
  264.          int missing = getMissingForSpinoff (numMJ, numMTV, numMMONEY);
  265.          
  266.          if (numBQN >= exchangeRate) {
  267.             // printf ("\nI can trade BQNs");
  268.             a.actionCode = RETRAIN_STUDENTS;
  269.             a.disciplineFrom = STUDENT_BQN;
  270.             a.disciplineTo = missing;
  271.             haveDecided = TRUE;
  272.          } else if (numBPS >= exchangeRate) {
  273.             // printf ("\nI can trade BPSs");
  274.             a.actionCode = RETRAIN_STUDENTS;
  275.             a.disciplineFrom = STUDENT_BPS;
  276.             a.disciplineTo = missing;
  277.             haveDecided = TRUE;
  278.          } else if (numMJ >= exchangeRate + 1) {
  279.             // printf ("\nI can trade MJs");
  280.             a.actionCode = RETRAIN_STUDENTS;
  281.             a.disciplineFrom = STUDENT_MJ;
  282.             a.disciplineTo = missing;
  283.             haveDecided = TRUE;
  284.          } else if (numMTV >= exchangeRate + 1) {
  285.             // printf ("\nI can trade MTVs");
  286.             a.actionCode = RETRAIN_STUDENTS;
  287.             a.disciplineFrom = STUDENT_MTV;
  288.             a.disciplineTo = missing;
  289.             haveDecided = TRUE;
  290.          } else if (numMMONEY >= exchangeRate + 1) {
  291.             // printf ("\nI can trade MMONEYs");
  292.             a.actionCode = RETRAIN_STUDENTS;
  293.             a.disciplineFrom = STUDENT_MMONEY;
  294.             a.disciplineTo = missing;
  295.             haveDecided = TRUE;
  296.          
  297.          // no resources are available for trade
  298.          } else {
  299.             a.actionCode = PASS;
  300.             haveDecided = TRUE;
  301.          }
  302.       }
  303.    }
  304.  
  305.    return a;
  306. }
  307.  
  308. // returns whether or not a spinoff is afforable
  309. int spinoffAvailable (int numMJ, int numMTV, int numMMONEY) {
  310.    int available = FALSE;
  311.    if (numMJ >= 1 && numMTV >= 1 && numMMONEY >= 1) {
  312.       available = TRUE;
  313.    }
  314.    return available;
  315. }
  316.  
  317. // returns a type of student that is missing to achieve a spinoff
  318. int getMissingForSpinoff (int numMJ, int numMTV, int numMMONEY) {
  319.    int missing = -1;
  320.    if (numMJ == 0) {
  321.       missing = STUDENT_MJ;
  322.    } else if (numMTV == 0) {
  323.       missing = STUDENT_MTV;
  324.    } else if (numMMONEY == 0) {
  325.       missing = STUDENT_MMONEY;
  326.    }
  327.    return missing;
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement