Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Vector;
- int[] spots;
- int favourableCases;
- int totalCases;
- int NUMBER_OF_PEOPLE = 7;
- int CYCLES_PER_FRAME = 1000;
- void setup() {
- frameRate(60);
- size(600,180);
- spots = new int[NUMBER_OF_PEOPLE];
- for (int i = 0; i < NUMBER_OF_PEOPLE; i++) {
- spots[i] = i+1;
- }
- }
- void draw() {
- background(255);
- for (int i = 0; i < CYCLES_PER_FRAME; i++) {
- totalCases++;
- shufflePeople();
- checkNeightbour();
- }
- fill(0, 0, 0);
- noFill();
- rect(30, 30, 50 * NUMBER_OF_PEOPLE, 50);
- for (int j = 0; j < NUMBER_OF_PEOPLE; j++) {
- line(30 + 50*j, 30, 30 + 50*j, 80);
- }
- ellipseMode(CORNER);
- fill(0, 255, 0);
- pushMatrix();
- translate(30 + (50 * positionOf(1)), 30);
- ellipse(0, 0, 50, 50);
- popMatrix();
- fill(255, 0, 0);
- pushMatrix();
- translate(30 + (50 * positionOf(2)), 30);
- ellipse(0, 0, 50, 50);
- popMatrix();
- fill(0);
- text("Favourable cases: " + favourableCases, 30, 100);
- text("Total cases: " + totalCases, 30, 115);
- text("Chance: " + (100 * ((double)favourableCases/(double)totalCases)) + "%", 30, 130);
- text("Expected Chance: " + (10.0/36.0)*100 + "%", 300, 100);
- }
- public int positionOf(int n) {
- int position = 0;
- for (int i = 0; i < NUMBER_OF_PEOPLE; i++) {
- if (spots[i] == n) {
- position = i;
- }
- }
- return position;
- }
- public void checkNeightbour() {
- if (spots[0] == 1 && spots[1] == 2) {
- favourableCases++;
- } else if (spots[NUMBER_OF_PEOPLE - 1] == 1 && spots[NUMBER_OF_PEOPLE - 2] == 2) {
- favourableCases++;
- } else {
- for (int i = 1; i < NUMBER_OF_PEOPLE - 1; i++) {
- if ((spots[i] == 1 && spots[i + 1] == 2) || (spots[i] == 1 && spots[i - 1] == 2)) {
- favourableCases++;
- }
- }
- }
- }
- public void shufflePeople() {
- Vector<Integer> aviablePeople = new Vector<Integer>();
- for (int i = 0; i < NUMBER_OF_PEOPLE; i++) {
- aviablePeople.add(spots[i]);
- }
- for (int i = 0; i < NUMBER_OF_PEOPLE; i++) {
- int randPosition = (int)(Math.random() * aviablePeople.size());
- spots[i] = aviablePeople.get(randPosition);
- aviablePeople.remove(randPosition);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement