[JAVA] Implement the box ball system with Processing

Meiji University Comprehensive Mathematics Advent Calender 2017 has also exceeded one week. It's long. ..

In this paper, the box ball system that appeared in Last time "About the box ball system" is implemented by Processing. Find the equation of motion (because it's not fun to implement it) and try to implement it using it.

Box-ball system equation of motion

To implement the box ball system, first how the time evolution of the box ball system Need to be asked.

Reconsider the box ball system as a binary CA in which $ u_n ^ t $ takes the value of $ 0,1 $. box-ball2_3.016.jpeg box-ball2_3.017.jpeg

If the number of balls on the left side does not decrease, According to the box ball rule (from left to right, move to the nearest empty box on the right) It will definitely be $ 0 $ next time. box-ball2_3.018.jpeg box-ball2_3.019.jpeg box-ball2_3.020.jpeg This is the equation of motion of the box ball system. (It can finally be implemented.)

Box ball system implementation

Implemented in Processing.

Consists of the main body and Box class. (It's a wasteful calculation, but is it okay?)

Code ([GitHub](https://github.com/massss/box-and-ball))

Main


final int boxSize = 40;
final int num = 25;

int Width = num * boxSize;
boolean isRunning = false;
Box[] boxes = new Box[num];
int[] tempBoxes = new int[num];

int t = 0;
int delay = 50;
int framerate;

void settings() {
  size(Width, boxSize + 50);
}

void setup() {
  background(255);
  for (int i = 0; i < num; i++) {
    boxes[i] = new Box(i, 0);
  }
}

void draw() {
  background(255);
  if (t % delay == 0) {
    framerate = (int)frameRate;
    if (isRunning) {
      for (int i = 0; i < num; i++) {
        tempBoxes[i] = boxes[i].ball;
      }
      for (int i = 0; i < num; i++) {
        boxes[i].calcState();
      }
    }
    t = 0;
  }
  t++;
  for (int i = 0; i < num; i++) {
    boxes[i].display();
  }

  fill(250, 0, 0);
  textAlign(LEFT);
  textSize(30);
  if (isRunning) {
    text("Run", 0, height - 10);
  } else {
    text("Stop", 0, height - 10);
  }
  textAlign(RIGHT);
  textSize(18);
  text("frameRate: " + framerate +" / " + delay, width - 10, height - 10);
}

void mouseClicked() {
  int cellColumn = (int)(mouseX / boxSize);
  if (cellColumn < boxes.length) {
    boxes[cellColumn].changeState();
  }
}

void mouseDragged() {
  int cellColumn = (int)(mouseX / boxSize);
  if (0 < mouseX && mouseX < Width && 0 < mouseY && mouseY < boxSize) {
    boxes[cellColumn].changeState();
  }
}

void keyReleased() {
  if (key == ' ') {
    isRunning = !isRunning;
  } else if (key == 'r' || key == 'R') {
    randomSetup();
  } else if (key == 'c' || key == 'C') {
    setup();
  } else if (keyCode == UP) {
    if (delay != 100) {
      delay += 10;
    }
  } else if (keyCode == DOWN) {
    if (delay != 10) {
      delay -= 10;
    }
  } else if (keyCode == ENTER) {
    printInfo();
  }
}

void randomSetup() {
  for (int i = 0; i < num; i++) {
    float coinSpeed = random(1);
    int coin;
    if (coinSpeed > 0.8) {
      coin = 1;
    } else {
      coin = 0;
    }
    boxes[i].ball = coin;
  }
}

void printInfo() {
  for (int i = 0; i < num; i++) {
    if (i == num - 1) {
      println(boxes[i].ball);
    } else {
      print(boxes[i].ball + ", ");
    }
  }
}

BoxClass


class Box {
  int pos;
  int num;
  int ball;

  Box (int num, int ball) {
    this.pos = num * boxSize;
    this.num = num;
    this.ball = ball;
  }
  
  void changeState() {
    if (ball == 1) {
      this.ball = 0;
    }else {
      this.ball = 1;
    }
  }
  
  void calcState() {
    int countPast = 0;
    int countNext = 0;
    for (int i = 0; i < this.num; i++) {
      if (boxes[i].ball == 1) {
        countNext++;
      }
      if (tempBoxes[i] == 1) {
        countPast++;
      }
    }
    this.ball = min(countPast - countNext, 1 - this.ball);
  }

  void display() {
    fill(255);
    rect(pos, 0, boxSize, boxSize);
    fill(0);
    if (ball == 1) {
      ellipse(pos + boxSize / 2, boxSize / 2, boxSize * 2/3, boxSize * 2/3);
    }
  }
}

Command

Key or mouse Processing
click add or remove ball
drag
r, R setup balls randomly
c, C clear all balls
up delay rate
down delay rate
space run or stop
ENTER print data in console

Sample hakodama-1.gif

hakodama-2.gif

Recommended Posts

Implement the box ball system with Processing
Image processing: Let's play with the image
Check the processing contents with [rails] binding.pry
Use the Mac menu bar with Processing 3 apps
Implement the Like feature in Ajax with Rails.
Christmas with Processing
Implement the UICollectionView of iOS14 with the minimum required code.
Control the processing flow of Spring Batch with JavaConfig.
[Swift] Implement swipe processing
Behind the 10-year system
I tried to increase the processing speed with spiritual engineering
[Rails] I tried to implement batch processing with Rake task
Implement the same function as C, C ++ system ("cls"); in Java