Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 903861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:57:22+00:00 2026-05-15T15:57:22+00:00

I’ve got some problems implementing Conway’s Game of Life in JavaScript. At the German

  • 0

I’ve got some problems implementing Conway’s Game of Life in JavaScript. At the German Wikipedia it says that this pattern here:

image could not be found

Will create an empty world after 54 generations and the evolution looks like this:

image could not be found

That means, the second generation looks like this:

image could not be found

But when using my code, the second generation looks like this and the world doesn’t get empty, either:

image could not be found

So my code is obviously wrong, but I don’t see my mistake:

var cellSize = 5,   // In px
    fieldSize = 70, // In cells
    canvas = document.getElementById("canvas"),
    ctx = canvas.getContext("2d"),
    thisGeneration = [],
    nextGeneration = [];

// Set canvas size
canvas.width = canvas.height = cellSize * fieldSize;


// Fill the generation array
for (var i = 0; i < fieldSize; i++){

    thisGeneration.push([]);

    for (var x = 0; x < fieldSize; x++){

        // thisGeneration[thisGeneration.length-1].push( (Math.random() >  0.08)? 0 : 1);

        thisGeneration[thisGeneration.length-1].push(0);

    }

}

// Draw pattern:

thisGeneration[35][35]      =   thisGeneration[35][36]  = thisGeneration[35][37] =

thisGeneration[36][35]                                  = thisGeneration[36][37] =
thisGeneration[37][35]                                  = thisGeneration[37][37] =


thisGeneration[39][35]                                  = thisGeneration[39][37] =
thisGeneration[40][35]                                  = thisGeneration[40][37] =
thisGeneration[41][35]      =   thisGeneration[41][36]  = thisGeneration[41][37] =
1;

// "slice" causes "nextGeneration" to be a copy, not a reference
nextGeneration = thisGeneration.slice(0);

setInterval(function(){

    for (var y = 0, l = thisGeneration.length, x, l2, n; y < l; y++){

        for (x = 0, l2 = thisGeneration[y].length;  x < l2; x++){

            ctx.fillStyle = thisGeneration[y][x]? "black" : "white";
            ctx.fillRect(x*cellSize, y*cellSize, cellSize, cellSize);

            // n := Number of neighbors of the cell
            n = 0;

            if (typeof (thisGeneration[y-1]) != "undefined"){

                n += (

                    (thisGeneration     [y-1]   [x-1]   ||0)+
                    (thisGeneration     [y-1]   [ x ]   ||0)+
                    (thisGeneration     [y-1]   [x+1]   ||0)

                );

            }

            n += (

                (thisGeneration     [ y ]   [x-1]   ||0)+
                (thisGeneration     [ y ]   [x+1]   ||0)

            );

            if (typeof (thisGeneration[y+1]) != "undefined"){

                n += (

                    (thisGeneration     [y+1]   [x-1]   ||0)+
                    (thisGeneration     [y+1]   [ x ]   ||0)+
                    (thisGeneration     [y+1]   [x+1]   ||0)

                );

            }

            if (n === 3 && !thisGeneration[y][x]){
            // Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

                nextGeneration[y][x] = 1;

            }

            if (n < 2 && thisGeneration[y][x]){
            // Any live cell with fewer than two live neighbours dies, as if caused by under-population.

                nextGeneration[y][x] = 0;

            }

            if ((n === 2 || n === 3) && thisGeneration[y][x]){
            // Any live cell with two or three live neighbours lives on to the next generation.

                nextGeneration[y][x] = 1;

            }

            if (n > 3 && thisGeneration[y][x]){
            // Any live cell with more than three live neighbours dies, as if by overcrowding.

                nextGeneration[y][x] = 0;

            }

        }

    }

    thisGeneration = nextGeneration.slice(0); // "slice" causes "thisGeneration" to be a copy, not a reference

}, 1000);

I’d also like to know whether I could improve the performance of the script, because as you can see I use a 2-dimensional array save the cell states.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:57:23+00:00Added an answer on May 15, 2026 at 3:57 pm

    I don’t think that thisGeneration and nextGeneration are as independent as you think they are. I suggest you replace the slice operations with a cell by cell copy and see if that resolves things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I know there's a lot of other questions out there that deal with this
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.