Automaton printing random characters from set
in
Programming Questions
•
3 months ago
I am trying to rework the cellular automaton code so that instead of filling each cell with 0 or 255, filling it instead with a randomly generated symbol from an array. I have gotten thus far (see below,) but where I have put the line text(symbols[int(random(symbols.length))]); is giving me the error code of "The method text(char,float,float) in the type PApplet is not applicable for the arguments (char)". Any help or suggestions would be great.
CA ca;
PFont f;
char[] symbols = {
'▓', '▒', '░'
};
void setup() {
size(640, 360, P2D);
f = createFont("FreeSans", 20, true);
textFont(f, 1);
frameRate(30);
background(0);
int[] ruleset = {0,1,0,1,1,0,1,0};
ca = new CA(ruleset);
}
void draw() {
ca.render();
ca.generate();
if (ca.finished()) {
background(0);
ca.randomize();
ca.restart();
}
}
void mousePressed() {
background(0);
ca.randomize();
ca.restart();
}
class CA {
int[] cells;
int generation;
int scl;
int[] rules;
CA(int[] r) {
rules = r;
scl = 1;
cells = new int[width/scl];
restart();
}
CA() {
scl = 1;
cells = new int[width/scl];
randomize();
restart();
}
// Set the rules of the CA
void setRules(int[] r) {
rules = r;
}
void randomize() {
for (int i = 0; i < 8; i++) {
rules[i] = int(random(2));
}
}
void restart() {
for (int i = 0; i < cells.length; i++) {
cells[i] = 0;
}
cells[cells.length/2] = 1;
generation = 0;
}
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] = executeRules(left,me,right);
}
for (int i = 1; i < cells.length-1; i++) {
cells[i] = nextgen[i];
}
generation++;
}
void render() {
for (int i = 0; i < cells.length; i++) {
if (cells[i] == 1) {
text(symbols[int(random(symbols.length))]);
} else {
fill(0);
}
noStroke();
rect(i*scl,generation*scl, scl,scl);
}
}
int executeRules (int a, int b, int c) {
if (a == 1 && b == 1 && c == 1) { return rules[0]; }
if (a == 1 && b == 1 && c == 0) { return rules[1]; }
if (a == 1 && b == 0 && c == 1) { return rules[2]; }
if (a == 1 && b == 0 && c == 0) { return rules[3]; }
if (a == 0 && b == 1 && c == 1) { return rules[4]; }
if (a == 0 && b == 1 && c == 0) { return rules[5]; }
if (a == 0 && b == 0 && c == 1) { return rules[6]; }
if (a == 0 && b == 0 && c == 0) { return rules[7]; }
return 0;
}
boolean finished() {
if (generation > height/scl) {
return true;
} else {
return false;
}
}
}
1