xxxxxxxxxx
60
let cells;
let cellSize = 10;
let colors = []; // Colors for different states
let song;
function preload() {
// Load an audio file
song = loadSound('sampleaudio.mp3');
}
function setup() {
createCanvas(800, 200);
cells = Array(floor(width / cellSize)).fill(0);
// Define colors for different states
colors[0] = color(255);
colors[1] = color(0, 0, 255);
}
function draw() {
background(255);
// Draw cells based on their state
for (let i = 0; i < cells.length; i++) {
let state = cells[i];
let col = colors[state];
fill(col);
noStroke();
rect(i * cellSize, 0, cellSize, height);
}
// Update cellular automaton rules when the mouse is pressed
if (mouseIsPressed) {
updateAutomaton();
if (!song.isPlaying()) {
song.play();
}
} else {
// Pause audio when the mouse is not pressed
if (song.isPlaying()) {
song.pause();
}
}
}
function updateAutomaton() {
let nextGen = [cells];
for (let i = 1; i < cells.length - 1; i++) {
// Apply custom rules for pattern evolution
if (cells[i] === 1 && random() < 0.1) {
// Allow living cells to die with a certain probability
nextGen[i] = 0;
} else if (cells[i] === 0 && random() < 0.05) {
// Create new living cells with a certain probability
nextGen[i] = 1;
}
}
cells = nextGen;
}