xxxxxxxxxx
59
class Rule90Automaton {
constructor(cols, rows, cellSize) {
this.cols = cols;
this.rows = rows;
this.cellSize = cellSize;
this.grid = [];
// Inicializa la matriz con valores aleatorios (0 o 1)
for (let i = 0; i < this.rows; i++) {
this.grid[i] = [];
for (let j = 0; j < this.cols; j++) {
this.grid[i][j] = floor(random(2));
}
}
}
display() {
// Dibuja las células
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
if (this.grid[i][j] === 1) {
fill(color('lime'));
rect(j * this.cellSize, i * this.cellSize, this.cellSize, this.cellSize);
}
}
}
}
update() {
// Calcula el siguiente estado de las células
let nextGrid = [];
for (let i = 0; i < this.rows; i++) {
nextGrid[i] = [];
for (let j = 0; j < this.cols; j++) {
let left = this.grid[i][(j - 1 + this.cols) % this.cols];
let center = this.grid[i][j];
let right = this.grid[i][(j + 1) % this.cols];
// Aplica la regla 90
nextGrid[i][j] = (left + center + right) % 2;
}
}
// Actualiza la matriz con el siguiente estado
this.grid = nextGrid;
}
}
let automaton;
function setup() {
createCanvas(800, 400);
automaton = new Rule90Automaton(80, 40, 10);
}
function draw() {
background(255);
automaton.display();
automaton.update();
}