Advertisement
Guest User

Chances of neighbouring sits

a guest
Jan 19th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. import java.util.Vector;
  2.  
  3. int[] spots;
  4. int favourableCases;
  5. int totalCases;
  6. int NUMBER_OF_PEOPLE = 7;
  7. int CYCLES_PER_FRAME = 1000;
  8.  
  9. void setup() {
  10.  
  11.   frameRate(60);
  12.  
  13.   size(600,180);
  14.  
  15.   spots = new int[NUMBER_OF_PEOPLE];
  16.  
  17.   for (int i = 0; i < NUMBER_OF_PEOPLE; i++) {
  18.    
  19.     spots[i] = i+1;
  20.    
  21.   }
  22.  
  23. }
  24.  
  25. void draw() {
  26.  
  27.   background(255);
  28.  
  29.   for (int i = 0; i < CYCLES_PER_FRAME; i++) {
  30.    
  31.     totalCases++;
  32.    
  33.     shufflePeople();
  34.    
  35.     checkNeightbour();
  36.    
  37.   }
  38.  
  39.   fill(0, 0, 0);
  40.   noFill();
  41.   rect(30, 30, 50 * NUMBER_OF_PEOPLE, 50);
  42.   for (int j = 0; j < NUMBER_OF_PEOPLE; j++) {
  43.      
  44.     line(30 + 50*j, 30, 30 + 50*j, 80);
  45.      
  46.   }
  47.   ellipseMode(CORNER);
  48.   fill(0, 255, 0);
  49.   pushMatrix();
  50.   translate(30 + (50 * positionOf(1)), 30);
  51.   ellipse(0, 0, 50, 50);
  52.   popMatrix();
  53.   fill(255, 0, 0);
  54.   pushMatrix();
  55.   translate(30 + (50 * positionOf(2)), 30);
  56.   ellipse(0, 0, 50, 50);
  57.   popMatrix();
  58.  
  59.   fill(0);
  60.   text("Favourable cases: " + favourableCases, 30, 100);
  61.   text("Total cases: " + totalCases, 30, 115);
  62.   text("Chance: " + (100 * ((double)favourableCases/(double)totalCases)) + "%", 30, 130);
  63.   text("Expected Chance: " + (10.0/36.0)*100 + "%", 300, 100);
  64. }
  65.  
  66. public int positionOf(int n) {
  67.  
  68.   int position = 0;
  69.  
  70.   for (int i = 0; i < NUMBER_OF_PEOPLE; i++) {
  71.    
  72.     if (spots[i] == n) {
  73.       position = i;
  74.     }
  75.    
  76.   }
  77.  
  78.   return position;
  79.  
  80. }
  81.  
  82. public void checkNeightbour() {
  83.  
  84.   if (spots[0] == 1 && spots[1] == 2) {
  85.     favourableCases++;
  86.   } else if (spots[NUMBER_OF_PEOPLE - 1] == 1 && spots[NUMBER_OF_PEOPLE - 2] == 2) {
  87.     favourableCases++;
  88.   } else {
  89.     for (int i = 1; i < NUMBER_OF_PEOPLE - 1; i++) {
  90.       if ((spots[i] == 1 && spots[i + 1] == 2) || (spots[i] == 1 && spots[i - 1] == 2)) {
  91.         favourableCases++;
  92.       }
  93.     }
  94.   }
  95.  
  96. }
  97.  
  98. public void shufflePeople() {
  99.  
  100.   Vector<Integer> aviablePeople = new Vector<Integer>();
  101.  
  102.   for (int i = 0; i < NUMBER_OF_PEOPLE; i++) {
  103.    
  104.     aviablePeople.add(spots[i]);
  105.    
  106.   }
  107.  
  108.   for (int i = 0; i < NUMBER_OF_PEOPLE; i++) {
  109.    
  110.     int randPosition = (int)(Math.random() * aviablePeople.size());
  111.     spots[i] = aviablePeople.get(randPosition);
  112.     aviablePeople.remove(randPosition);
  113.    
  114.   }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement