Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
Why is my p5 javascript game of life not working?

I watched a coding train video about the game of life and i tried making a program but it doesn't work. It just generates random squares on the screen as a first image (the starting position) and then just generates another frame with the positive squares (white) disappearing and that's it. Only 2 frames. And it doesn't show an error. Can someone explain why?

function make2D(x, y) {
  let arr = new Array(x);
  for (let i = 0; i < x; i++) {
    arr[i] = new Array(y);
  }
  return arr;
}

let grid;
let col;
let row;
let resolution = 20;

function setup() {
  createCanvas(500, 500);
  col = width / resolution;
  row = height / resolution;
  grid = make2D(col, row);
  for (let i = 0; i < col; i++) {
    for (let j = 0; j < row; j++) {
      grid[i][j] = floor(random(2));
    }
  }
}

function draw() {
  frameRate(1);
  background(0);
  //drawing
  for (let i = 0; i < col; i++) {
    for (let j = 0; j < row; j++) {
      let x = i * resolution;
      let y = j * resolution;
      if (grid[i][j]) {
        fill(255);
      } else {
        fill(0);
      }
      rect(x, y, resolution, resolution);
    }
  }
  //processing
  let next = make2D(col, row);
  for (let i = 0; i < col; i++) {
    for (let j = 0; j < row; j++) {
      let nb = calculatePosition(grid, i, j);
      //fill(255,0,0);
      //text(grid[i][j],i*resolution+6,j*resolution+15);
      if (grid[i][j] == 0 && nb == 3) {
        next[i][j] == 1;
      } else if (grid[i][j] == 1 && (nb > 3 || nb < 2)) {
        next[i][j] == 0;
      } else {
        next[i][j] = grid[i][j];
      }
    }
  }
  grid = next;
}

function calculatePosition(grid, x, y) {
  let sum = 0;
  for (let i = -1; i < 2; i++) {
    for (let j = -1; j < 2; j++) {
      let cols = (x + i + col) % col;
      let rows = (y + j + row) % row;
      sum += grid[cols][rows];
    }
  }
  sum -= grid[x][y];
  console.log(sum);
  return sum;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.js"></script>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You accidentally are using equality (==) instead of assignment (=) here

if (grid[i][j] == 0 && nb == 3) {
  next[i][j] == 1;
} else if (grid[i][j] == 1 && (nb > 3 || nb < 2)) {
  next[i][j] == 0;
} else {
  next[i][j] = grid[i][j];
}

You should also move your frameRate call to setup().

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda