arrays - Combinatorial game in java -


i've got assignment computer science class. i've been thinking answer more 2 weeks without success decided give try in here.
game consists of 4 cia agents using 4 adjacent telephone booths. arrive @ set of booths 1 @ time, following rules:

1 - agent chooses booth has 2 neighbouring booths unoccupied (this includes booths @ edges of set);
2 - if condition above isn't met, agent chooses booth has 1 neighbouring booth unoccupied; and
3 - if both conditions fail, agent chooses booth.

there 8 different ways of occupying booths given 4 agents , 4 booths, according rules. example: agent #1 occupies first booth (the left edge one), agent #2 occupies third booth (from left right), agent #3 occupies fourth booth , agent #4 occupies second booth.

we asked write code counts number of ways 19 agents occupy 19 booths, according these rules.

my natural choice problem creating array of boolean, in false represents vacant booth , true represents occupied booth:

public class agentgame {      private boolean[] booths;     private int numberofbooths, counter;      public agentgame(int nb) {          counter = 0;         numberofbooths = nb;         booths = new boolean[numberofbooths];      } 

i've been trying think of way of implementing 2 methods, 1 filling booths according rules , 1 counting different combinations in booths can occupied. wasn't able figure out, not yet. ideas here? in advance.

somewhat inefficient, hope clear solution. can't guarantee output right answer (as love make stupid mistakes), should give idea how solve it.

package whynotzoidberg;  import java.util.arraylist; import java.util.collections; import java.util.comparator;  public class agents {      public static final int n = 5;  // amount of total agents / booths       static int position[] = new int[n]; // index = agent, value = boot     static boolean occupied[] = new boolean[n];     static arraylist<integer> answers = new arraylist<>();      public static void main(string[] args) {          (int = 0; < n; i++) {             position[i] = -1;         }          system.out.println("solutions: ");         int answer = getways(0);          system.out.println();         system.out.println("the answer ");         system.out.println(answer);         system.out.println();         collections.sort(answers,new comparator<integer>() {              @override             public int compare(integer o1, integer o2) {                 if (o1 > o2) {                     return 1;                 }                 if (o2 > o1) {                     return -1;                 }                 system.out.println("repetition between answers detected. quiting.");                 system.exit(0);                 return 0;             }         });         system.out.println("the solutions following (the number agent number)");         (int = 0; < answers.size(); i++) {             system.out.println(answers.get(i));         }     }      static int getways(int agentnr) {         if (agentnr == n-1) {              position[n-1] = viablebootoptions().get(0);              int number = 0;             (int = 0; < n; i++) {                  (int j = 0; j < n; j++) {                     if (position[j] == i) {                         number += (int) ((j+1) * math.pow(10, n - - 1));                     }                 }             }              answers.add(number);              return 1;               }          int result = 0;         arraylist<integer> options = viablebootoptions();          (int = 0; < options.size(); i++) {             position[agentnr] = options.get(i);             (int j = agentnr + 1; j < n; j++) {                 position[j] = -1;             }             updateoccupied();             result += getways (agentnr + 1);         }          return result;      }      static arraylist<integer> viablebootoptions() {         int ruleselected = 3;         arraylist<integer> result = new arraylist<>();          if (!occupied[0] && !occupied[1]) {             ruleselected = 1;             result.add(0);         }         if (!occupied[n-2] && !occupied[n-1]) {             ruleselected = 1;             result.add(n-1);         }         if (!occupied[0]) {             if (ruleselected != 1) {                 ruleselected = 2;                 result.add(0);             }         }         if (!occupied[n-1]) {             if (ruleselected != 1) {                 ruleselected = 2;                 result.add(n-1);             }         }          (int = 1; < n-1; i++) {             if (!occupied[i]) {                 if (!occupied[i - 1] && !occupied[i + 1]) {                     if (ruleselected > 1) {                         ruleselected = 1;                         result.clear();                     }                     result.add(i);                     continue;                 }                 if ((!occupied[i - 1] || !occupied[i + 1]) && ruleselected != 1) {                      if (ruleselected > 1) {                         if (ruleselected == 3) {                             result.clear();                         }                         ruleselected = 2;                            result.add(i);                         continue;                     }                  }                  if (ruleselected == 3) {                     result.add(i);                 }              }         }         return result;      }      static void updateoccupied() {         (int = 0; < n; i++) {             occupied[i] = false;         }         (int = 0; < n; i++) {             if (position[i] > -1) {                 occupied[position[i]] = true;             }         }     } } 

edit: program prints answers found, terminates if of answers repeats. can change amount of agents changing value of n.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -