Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
¿Por qué mi p5 javascript game of life no funciona?

Vi un video de tren de codificación sobre el juego de la vida e intenté hacer un programa pero no funciona. Simplemente genera cuadrados aleatorios en la pantalla como una primera imagen (la posición inicial) y luego genera otro cuadro con los cuadrados positivos (blancos) desapareciendo y eso es todo. Solo 2 marcos. Y no muestra ningún error. ¿Alguien puede explicar por qué?

 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 answers
Answer question

0

Accidentalmente está usando igualdad ( == ) en lugar de asignación ( = ) aquí

 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]; }

También debe mover su llamada frameRate a setup() .

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!