This method chooses the number of sticks to pick up based
/**
* This method chooses the number of sticks to pick up based on the
* sticksRemaining and actionRanking parameters.
* Config.MAX_ACTION =3,Config.MIN_ACTION=1
* Algorithm: If there are less than Config.MAX_ACTION sticks remaining
* then the chooser must pick the minimum number of sticks (Config.MIN_ACTION ).
* For Config.MAX_ACTION or more sticks remaining then pick based on the
* actionRanking parameter.
*
* The actionRanking array has one element for each possible action. The 0
* index corresponds to Config.MIN_ACTION and the highest index corresponds
* to Config.MAX_ACTION. For example, if Config.MIN_ACTION is 1 and
* Config.MAX_ACTION is 3, an action can be to pick up 1, 2 or 3 sticks.
* actionRanking[0] corresponds to 1, actionRanking[1] corresponds to 2, etc.
* The higher the element for an action in comparison to other elements,
* the more likely the action should be chosen.
*
* First calculate the total number of possibilities by summing all the
* element values. Then choose a particular action based on the relative
* frequency of the various rankings.
* For example, if Config.MIN_ACTION is 1 and Config.MAX_ACTION is 3:
* If the action rankings are {9,90,1}, the total is 100. Since
* actionRanking[0] is 9, then an action of picking up 1 should be chosen
* about 9/100 times. 2 should be chosen about 90/100 times and 3 should
* be chosen about 1/100 times. Use Config.RNG.nextInt(?) method to
* generate appropriate random numbers.
*
* @param sticksRemaining
* The number of sticks remaining to be picked up.
* @param actionRanking
* The counts of each action to take. The 0 index corresponds to
* Config.MIN_ACTION and the highest index corresponds to
* Config.MAX_ACTION.
* @return The number of sticks to pick up. 0 is returned for the following
* conditions: actionRanking is null, actionRanking has a length of
* 0, or sticksRemaining is <= 0.
*
*/
static int aiChooseAction(int sticksRemaining, int[] actionRanking) {
}
Solution
//Define the config variables and the random method
Config.java
import java.util.Random;
public class Config{
//declaring random variables
static int MAX_ACTION = 3;
static int MIN_ACTION = 1;
public static class RNG{
public static int nextInt(int n){
return new Random().nextInt(n); //returning random int
}
}
}
ai.java
public class ai{
static int aiChooseAction(int sticksRemaining, int[] actionRanking){
int size = actionRanking.length;
if (size == 0 || sticksRemaining<0 || actionRanking==null){
return 0; //boundary conditions (sanity checks)
}
else if(sticksRemaining<Config.MAX_ACTION){
return Config.MIN_ACTION; //return min if less than max
}
else{
int total = 0;
for(int i=0; i<size; i++){
total += actionRanking[i]; //calculating total
}
int random_no = Config.RNG.nextInt(total);
System.out.println(\"random: \"+random_no);
int sum = 0;
for(int i=0; i<size; i++){
if(random_no < sum+actionRanking[i]){
return i+1;
}
else{
sum+=actionRanking[i];
}
}
}
return 0;
}
public static void main(String[] args) {
int[] actionRanking = new int[] {9, 90, 1};
int sticksRemaining = 10;
System.out.println(aiChooseAction(sticksRemaining, actionRanking));
}
}

