OneHourProcessing 九日目

OneHourProcessing 九日目

今日作ったのはこちら

作るのにかかった時間は「41分30秒」
久々の時間以内+最速

コード

CA cellular_automata;

void setup()
{
  size(800, 450);
  reset();
}

void draw()
{
  cellular_automata.display();
  cellular_automata.update();
}

void mousePressed()
{
  reset();
}

void reset()
{
  cellular_automata = null;
  cellular_automata = new CA(char(int(random(256))));
}
class CA
{
  int[] rule  = new int[8];
  int[] cells = new int[width];
  int hierarchy;
  int direction = 1;

  CA(char rule)
  {
    for (int i = 0; i < 8; i++)
    {
      this.rule[i] = (rule >> i) % 2;
    print(((rule >> i) % 2) + " : ");
    }
    println(int(rule));
    
    for (int i = 0; i < cells.length; i++)
      cells[i] = 0;

    cells[int(cells.length / 2)] = 1;

    hierarchy = 0;

    background(0);
  }

  void display()
  {
    loadPixels();

    for (int i = 0; i < cells.length; i++)
      if (cells[i] == 1)
        pixels[i + hierarchy * width] = color(0, 255, 0);  
      else
        pixels[i + hierarchy * width] = color(0); 

    updatePixels();
  }

  void update()
  {
    int[] nextCells = new int[width];

    for (int i = 1; i < cells.length - 1; i++)
    {
      int neighborCells = cells[i - 1] * 4 + cells[i] * 2 + cells[i + 1];
      nextCells[i] = rule[neighborCells];
    }

    nextCells[0] = cells[0];
    nextCells[cells.length - 1] = cells[cells.length - 1];

    cells = nextCells;

    hierarchy += direction;

    if (hierarchy == 0)
      direction = 1;
    else if (hierarchy == height - 1)
      direction = -1;
  }
}