Advertisement
Cinestra

Recommend Move Function & Assess Risk

Feb 6th, 2023
2,513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. bool recommendMove(const Arena& a, int r, int c, int& bestDir)
  2. {
  3.     // TODO:  Implement this function
  4.     // Delete the following line and replace it with your code.
  5.  
  6.     int idle_risk = assessRisk(a, r, c);
  7.  
  8.     if (idle_risk == 0)
  9.         return false;
  10.    
  11.     int least_risk = idle_risk;
  12.  
  13.     int north_risk = assessRisk(a, r - 1, c);
  14.     int south_risk = assessRisk(a, r + 1, c);
  15.     int west_risk = assessRisk(a, r, c - 1);
  16.     int east_risk = assessRisk(a, r, c + 1);
  17.  
  18.     if (north_risk < least_risk && r != 1) {
  19.         least_risk = north_risk;
  20.         bestDir = NORTH;
  21.     }
  22.     if (south_risk < least_risk && r != a.rows()) {
  23.         least_risk = south_risk;
  24.         bestDir = SOUTH;
  25.     }
  26.     if (west_risk < least_risk && c!=1) {
  27.         least_risk = west_risk;
  28.         bestDir = WEST;
  29.     }
  30.     if (east_risk < least_risk && c!=a.cols()) {
  31.         least_risk = east_risk;
  32.         bestDir = EAST;
  33.     }
  34.  
  35.     // Now let's check if least risk is different from idle risk
  36.     // If it is different, then we would recommend moving
  37.     if (idle_risk < least_risk)
  38.     {
  39.         return true;
  40.     }
  41.  
  42.     // If it isn't different, simply return false.
  43.     return false;
  44.  
  45.     // Your replacement implementation should do something intelligent.
  46.     // You don't have to be any smarter than the following, although
  47.     // you can if you want to be:  If staying put runs the risk of a
  48.     // rabbit possibly moving onto the player's location when the rabbits
  49.     // move, yet moving in a particular direction puts the player in a
  50.     // position that is safe when the rabbits move, then the chosen
  51.     // action is to move to a safer location.  Similarly, if staying put
  52.     // is safe, but moving in certain directions puts the player in
  53.     // danger of dying when the rabbits move, then the chosen action should
  54.     // not be to move in one of the dangerous directions; instead, the player
  55.     // should stay put or move to another safe position.  In general, a
  56.     // position that may be moved to by many rabbits is more dangerous than
  57.     // one that may be moved to by few.
  58.     //
  59.     // Unless you want to, you do not have to take into account that a
  60.     // rabbit might be poisoned and thus sometimes less dangerous than one
  61.     // that is not.  That requires a more sophisticated analysis that
  62.     // we're not asking you to do.
  63. }
  64.  
  65. int assessRisk(Arena& a, int row, int col)
  66. {
  67.  
  68.     int risk = 0;
  69.  
  70.     if (a.numberOfRabbitsAt(row, col) > 0)
  71.     {
  72.         risk += 99;
  73.         return risk;
  74.         // Do not move here lmao
  75.     }
  76.  
  77.     else
  78.     {
  79.         risk += a.numberOfRabbitsAt(row - 1, col); // Rabbits above indicated location
  80.         risk += a.numberOfRabbitsAt(row + 1, col); // Rabbits below indicated location
  81.         risk += a.numberOfRabbitsAt(row, col - 1); // Rabbits left of indicated location
  82.         risk += a.numberOfRabbitsAt(row, col + 1); //Rabbits right of indicated location
  83.  
  84.         return risk;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement