Move the mouse Press R to reset Press left & right arrows to switch between different themes Resizing the grid needs a reset (r) to be applied
xxxxxxxxxx
OPC.slider("BIRTH",2,1,7,1);
OPC.slider("DEATH",3,1,7,1);
OPC.slider("GridLength",100,10,300,5);
// Variables globales
var rows = 100;
var cols = 100;
var grid = new Array(cols);
var bgvalue = new Array(155,188,15);
var fillvalue = new Array(15,56,15);
var theme = 0;
// Dimensions des cellules
var cellWidth;
var cellHeight;
// Initialisation du jeu de la vie
function setup() {
createCanvas(windowWidth, windowHeight);
// Initialisation de la grille
for (var i = 0; i < cols; i++) {
grid[i] = new Array(rows);
for (var j = 0; j < rows; j++) {
grid[i][j] = 0;
}
}
// Calcul des dimensions des cellules
cellWidth = width / cols;
cellHeight = height / rows;
}
// Dessin du jeu de la vie
function draw() {
themeUpdate();
background(bgvalue[0],bgvalue[1],bgvalue[2]);
// Dessin de la grille
for (var i = 0; i < cols; i++) {
for (var j = 0; j < rows; j++) {
var x = i * cellWidth;
var y = j * cellHeight;
if (grid[i][j] == 1) {
fill(fillvalue[0],fillvalue[1],fillvalue[2]);
noStroke();
rect(x, y, cellWidth, cellHeight);
}
}
}
// Copie de la grille actuelle
var nextGrid = new Array(cols);
for (var i = 0; i < cols; i++) {
nextGrid[i] = new Array(rows);
for (var j = 0; j < rows; j++) {
nextGrid[i][j] = grid[i][j];
}
}
// Calcul de la prochaine génération
for (var i = 0; i < cols; i++) {
for (var j = 0; j < rows; j++) {
var state = grid[i][j];
var neighbors = countNeighbors(grid, i, j);
// Règles du jeu de la vie
if (state == 0 && neighbors == BIRTH) {
nextGrid[i][j] = 1;
} else if (state == 1 && (neighbors > DEATH)) {
nextGrid[i][j] = 0;
}
}
}
// Mise à jour de la grille
grid = nextGrid;
}