Commit 8a328648 authored by Md Abid Sikder's avatar Md Abid Sikder
Browse files

Add Cellular Automata generating code

parent 4cde17ff
class CA
{
int[] cells;
int[] ruleset;
int w = 10;
int generation = 0;
String name;
CA(int newRuleset)
{
cells = new int[width/w];
ruleset = new int[8];
this.name = Integer.toBinaryString(newRuleset);
for (int i = 0; i < name.length(); i++)
{
String subsetRule = name.substring(i, i+1);
ruleset[i] = Integer.parseInt(subsetRule);
}
for (int i = 0; i < cells.length; i++)
{
// randomize the first generation
cells[i] = random(1)>0.5? 0:1;
}
}
void generate()
{
int[] nextgen = new int[cells.length];
for (int i = 1; i < cells.length-1; i++)
{
int left = cells[i-1];
int middle = cells[i];
int right = cells[i+1];
nextgen[i] = rules(left, middle, right);
}
cells = nextgen;
generation++;
}
void display()
{
for (int i = 0; i < cells.length; i++) {
if (cells[i] == 1) fill(0);
else fill(200);
// Set the y-location according to the generation.
rect(i*w, generation*w, w, w);
}
}
int rules(int a, int b, int c)
{
String s = "" + a + b + c;
int index = Integer.parseInt(s, 2);
return ruleset[index];
}
}
CA auto;
int yCounter = 0;
int ruleCounter = 0;
void setup()
{
size(1500, 1000);
auto = new CA(ruleCounter);
}
void draw()
{
auto.display();
auto.generate();
auto.display();
yCounter += auto.w;
// when it reaches the bottom of the window, save the full image
if (yCounter > height)
{
String unpaddedName = auto.name;
String paddedName = "00000000".substring(unpaddedName.length()) + unpaddedName;
save("elementaryCA_" + paddedName + ".png");
ruleCounter++;
auto = new CA(ruleCounter);
yCounter = 0;
println(auto.name);
}
}
import java.util.HashMap;
class CA
{
int[] cells;
HashMap<Integer, HashMap<Integer, HashMap<Integer, Integer>>> ruleset;
// width in pixels
int cellWidth = 5;
int generation = 0;
String name;
CA()
{
cells = new int[width/cellWidth];
ruleset = new HashMap<Integer, HashMap<Integer, HashMap<Integer, Integer>>>();
// make the name while building the ruleset
StringBuilder nameBuilder = new StringBuilder();
// make a random ruleset
for (int i = 0; i < 3; i++)
{
ruleset.put(i, new HashMap<Integer, HashMap<Integer, Integer>>());
// initialize the ruleset for the medium level
for (int j = 0; j < 3; j++)
{
ruleset.get(i).put(
j, new HashMap<Integer, Integer>()
);
// initialize a random rule for the bottom level
for (int k = 0; k < 3; k++)
{
ruleset.get(i).get(j).put(
k, int(random(3))
);
nameBuilder.append(ruleset.get(i).get(j).get(k));
// i is left cell, j is middle cell, and k is right cell
}
}
}
// make the name for the cellular automaton based on the ruleset
name = nameBuilder.toString();
for (int i = 0; i < cells.length; i++)
{
cells[i] = int(random(3));
}
}
void generate()
{
int[] nextgen = new int[cells.length];
for (int i = 1; i < cells.length-1; i++)
{
int left = cells[i-1];
int middle = cells[i];
int right = cells[i+1];
nextgen[i] = rules(left, middle, right);
}
cells = nextgen;
generation++;
}
int rules(int a, int b, int c)
{
return ruleset.get(a).get(b).get(c);
}
void display()
{
colorMode(RGB);
noStroke();
for (int i = 0; i < cells.length; i++) {
switch(cells[i])
{
case 0:
fill(242,65,80, 255);
break;
case 1:
fill(31,175,191, 255);
break;
case 2:
fill(89,89,89, 255);
break;
default:
println("Uh oh something went wrong");
break;
}
// Set the y-location according to the generation.
rect(i*cellWidth, generation*cellWidth, cellWidth, cellWidth);
}
}
}
CA auto;
int yCounter = 0;
void setup()
{
size(1500, 1000);
auto = new CA();
}
void draw()
{
auto.display();
auto.generate();
auto.display();
yCounter += auto.cellWidth;
// when it reaches the bottom of the window, save the full image
if (yCounter > height)
{
save("states3CA_" + auto.name + ".png");
println(auto.name);
auto = new CA();
yCounter = 0;
}
}
import java.util.HashMap;
class CA
{
int[] cells;
HashMap<Integer, HashMap<Integer, HashMap<Integer, Integer>>> ruleset;
// width in pixels
int cellWidth = 5;
int generation = 0;
String name;
CA()
{
cells = new int[width/cellWidth];
ruleset = new HashMap<Integer, HashMap<Integer, HashMap<Integer, Integer>>>();
StringBuilder nameBuilder = new StringBuilder();
// make a random ruleset
for (int i = 0; i < 4; i++)
{
ruleset.put(i, new HashMap<Integer, HashMap<Integer, Integer>>());
for (int j = 0; j < 4; j++)
{
ruleset.get(i).put(
j, new HashMap<Integer, Integer>()
);
for (int k = 0; k < 4; k++)
{
ruleset.get(i).get(j).put(
k, int(random(4))
);
nameBuilder.append(ruleset.get(i).get(j).get(k));
// i is left cell, j is middle cell, and k is right cell
}
}
}
// make the name for the cellular automaton based on the ruleset
name = nameBuilder.toString();
for (int i = 0; i < cells.length; i++)
{
cells[i] = int(random(4));
}
}
void generate()
{
int[] nextgen = new int[cells.length];
for (int i = 1; i < cells.length-1; i++)
{
int left = cells[i-1];
int middle = cells[i];
int right = cells[i+1];
nextgen[i] = rules(left, middle, right);
}
cells = nextgen;
generation++;
}
int rules(int a, int b, int c)
{
return ruleset.get(a).get(b).get(c);
}
void display()
{
colorMode(RGB);
noStroke();
for (int i = 0; i < cells.length; i++) {
switch(cells[i])
{
case 0:
fill(#2C3E50);
break;
case 1:
fill(#E74C3C);
break;
case 2:
fill(#ECF0F1);
break;
case 3:
fill(#3498DB);
break;
default:
println("Uh oh something went wrong");
break;
}
// Set the y-location according to the generation.
rect(i*cellWidth, generation*cellWidth, cellWidth, cellWidth);
}
}
}
CA auto;
int yCounter = 0;
void setup()
{
size(1500, 1000);
auto = new CA();
}
void draw()
{
auto.display();
auto.generate();
auto.display();
yCounter += auto.cellWidth;
if (yCounter > height)
{
save("states4CA_" + auto.name + ".png");
auto = new CA();
yCounter = 0;
}
}
import java.util.HashMap;
class CA
{
int[] cells;
HashMap<Integer, HashMap<Integer, HashMap<Integer, Integer>>> ruleset;
// width in pixels
int cellWidth = 5;
int generation = 0;
int n;
String name;
CA(int n)
{
cells = new int[width/cellWidth];
ruleset = new HashMap<Integer, HashMap<Integer, HashMap<Integer, Integer>>>();
// make the name while building the ruleset
StringBuilder nameBuilder = new StringBuilder();
// make a random ruleset
// initialize the ruleset for the top level
for (int i = 0; i < n; i++)
{
ruleset.put(i, new HashMap<Integer, HashMap<Integer, Integer>>());
// initialize the ruleset for the medium level
for (int j = 0; j < n; j++)
{
ruleset.get(i).put(
j, new HashMap<Integer, Integer>()
);
// initialize a random rule for the bottom level
for (int k = 0; k < n; k++)
{
ruleset.get(i).get(j).put(
k, int(random(n))
);
nameBuilder.append(ruleset.get(i).get(j).get(k));
}
}
}
// make the name for the cellular automaton based on the ruleset
name = nameBuilder.toString();
for (int i = 0; i < cells.length; i++)
{
cells[i] = int(random(n));
}
}
void generate()
{
int[] nextgen = new int[cells.length];
for (int i = 1; i < cells.length-1; i++)
{
int left = cells[i-1];
int me = cells[i];
int right = cells[i+1];
nextgen[i] = rules(left, me, right);
}
cells = nextgen;
// Increment the generation counter.
generation++;
}
int rules(int a, int b, int c)
{
return ruleset.get(a).get(b).get(c);
}
void display()
{
colorMode(HSB);
noStroke();
for (int i = 0; i < cells.length; i++) {
// change color scheme here
fill(360/nth * cells[i], 255, 255, 255);
// Set the y-location according to the generation.
rect(i*cellWidth, generation*cellWidth, cellWidth, cellWidth);
}
}
}
CA auto;
int yCounter = 0;
// enter in the number of possible cell states you'd like
int nth = 5;
void setup()
{
size(1500, 1000);
auto = new CA(nth);
}
void draw()
{
auto.display();
auto.generate();
auto.display();
yCounter += auto.cellWidth;
if (yCounter > height)
{
save("states" + nth + "CA_" + auto.name + ".png");
auto = new CA(nth);
yCounter = 0;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment