Resetting the array in the Javascript game of life

Recently, I embarked on a coding adventure to recreate Conway's Game of Life using JavaScript. While my code logic appears sound and each function operates correctly on its own, the final outcome I desire remains elusive. My implementation involves an array named grid which holds data on cell values (alive or dead) within the game. To determine the number of live neighbors for each cell, I check all 8 surrounding cells iteratively. However, at a certain point in my program execution, the values stored in the grid seem to reset unexpectedly. This unexpected behavior has led me to wonder if the issue lies in the JavaScript environment itself.

<body>
  <style>
    * {
      padding: 0;
      margin: 0;
    }
    
    body {
      overflow: hidden;
    }
    
    canvas {
      background: #FFFFFF;
      display: block;
      margin: 0 auto;
    }
  </style>
  <canvas id="canvas" style="border:1px solid #000000;"></canvas>
</body>
<!-- The script content goes here -->

Answer №1

emptyGrid = newGrid(Game.horCells, Game.verCells);
grid = emptyGrid;
nextGrid = emptyGrid

This code snippet initializes a single grid and assigns both grid and nextGrid to the same object. Any modifications made to one will reflect on the other as well.

To have two separate grids that can be modified independently:

grid = newGrid(Game.horCells, Game.verCells);
nextGrid = newGrid(Game.horCells, Game.verCells);

Alternatively, for a more streamlined approach:

var g = () => newGrid(Game.horCells, Game.verCells);

grid = g();
nextGrid = g();

To address a query from the comments:

var g = () => newGrid(Game.horCells, Game.verCells);

Essentially serves the same purpose as:

var g = function() {
    return newGrid(Game.horCells, Game.verCells);
}

This method is referred to as a "fat-arrow" function.

Both examples achieve the same outcome of creating a function that generates a new grid, avoiding repetition of

newGrid(Game.horCells, Game.verCells);
.

The arrow function notation was chosen over the traditional function keyword for readability and conciseness.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Pass data from a child controller to its parent using $emit

My goal is to pass values from dropdown lists and input fields in the FirstController to the MainController, and values from datepickers in the SecondController to the MainController. I have been attempting to use $emit and $on but without success. It&apos ...

Explore how Vue 2.1 allows for the dynamic updating of dropdown options based on selections made in another dropdown

Currently, I have a Vue v.2 form set up with an address section that includes a State dropdown. My goal is to make the State dropdown automatically populate based on the city selected. For example, if a user chooses 'Atlanta' as the city, 'G ...

What could be the reason for this code resulting in the program ceasing to function?

for(person = 0; person <= maximum; person++) { username[person] = username1; passcode[person] = passcode1; } Once the loop in the program reaches this point, it suddenly stops running and I have to end the program. What steps can I take to reso ...

Is there a way for me to display the output within the component rather than using console log?

I created a simple app.js file in React that continuously checks if a number is prime or not and logs the result in the console every second. Now, I am looking to display this information on my homepage instead of in the console. Any suggestions on how I ...

How can you determine the distance of an SVG path?

I've been experimenting with Scrollmagic and I'm interested in implementing a similar effect to the one showcased here: To test it out, I created an svg with a squiggle path and now I need to insert the length of the path into stroke-dasharray: ...

Learn how to display a tooltip for every individual point on a Highcharts network graph within an Angular

I am currently working on developing network graphs using highcharts and highcharts-angular within my Angular application. I have successfully managed to display the graph with datalabels, but now I need to implement tooltips for each point or node on the ...

Laravel Tutorial: Passing a Specific Shop ID in a Foreach Loop to a Controller

I am trying to extract the specific shop id when the button is clicked. I have a foreach loop that displays the name of each shop and its products. What I'm aiming for is to pass the $shop->id value to the controller after clicking the x button. On ...

I'm feeling a bit lost when it comes to assigning and defining variables in AngularJS,

Currently embarking on developing my own game leveraging the latest JavaScript, AngularJS, and Node.js technologies. My approach in handling users and players is inspired by the code found here: https://github.com/amirrajan/nodejs-against-humani ...

Breaking down the results of a SQL query into an array

I'm looking to implement a paging feature for my query results where I display the first 51 records and store the rest in an array for later retrieval. While I can successfully show the initial 51 results on the page, I'm struggling to figure out ...

Using Javascript to Manage Scroll Stop Events

I am currently facing a challenge while working on an IOS (Cordova) application. I am developing it using core Javascript without relying on any framework like Ionic or Onsen UI. The issue I am encountering is that I need to trigger an event when the scrol ...

Is it possible to share a MySQL connection for cross-module usage in Node/Express by utilizing promise-mysql?

Currently, I am trying to import and utilize a database module within one of my controllers. Despite successfully establishing the initial connection, I am encountering an error when accessing any of my routes through the browser: "Cannot read property ...

"Tricky HTML table layout challenge: Margins, paddings, and borders causing

Working with HTML <table cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <img src="..."> </td> </tr> </tbody> </table> Applying CSS rules * { border: none; ...

When developing a node.js project using VMWare's shared folder, how can you manage the node_modules folder?

My current project needs to run on Linux, but I am using a Windows computer. This means I have to use a VM. Despite wanting to use WebStorm, I am avoiding JB Gateway due to its numerous bugs. My solution was to utilize the VMWare share folder function. Ho ...

How can I implement Jquery ajax calls in various JavaScript functions?

I'm encountering an issue with a particular function in my code function readComm(){ $.post("something.php", {read:"read"}, function(data){ retVal = $.trim(data).toString(); console.log(retVal); return retVal; }); } T ...

What are the limitations of using a JS file within an Angular application?

I am struggling to integrate some js methods from a file into an angular application. Unfortunately, the browser is unable to recognize the js file. I have tried following the guidelines in this SO post, but the browser still cannot locate the file in the ...

Converting a JSON object into a different format using TypeScript

Looking for advice on how to efficiently convert JSON data into a specific format without hardcoding any values like "root" or "Amount". I want to create a reusable function that can be used in various scenarios. Currently, I am working with TypeScript and ...

Creating Class Names Dynamically in Angular 2 Using ngFor Index

Encountering an issue while trying to generate a dynamic class name based on the ngFor loop index in Angular 2. Due to restrictions, I had to use a specific syntax as Angular 2 does not support ngFor and ngIf together on the same element. Given this setup ...

Reveal the class to the global scope in TypeScript

ClassExample.ts: export class ClassExample{ constructor(){} } index.html: <script src="ClassExample.js"></<script> // compiled to es5 <script> var classExample = new ClassExample(); //ClassExample is not defined < ...

Determine the display width and height of an image by setting max-width and max-height to 100%

I am currently working on a slideshow that features images of different sizes. My goal is to have these images displayed in the center both vertically and horizontally within a canvas area. Within my CSS, I've specified the width and height of each i ...

Tips for increasing the height of a popover when clicked

When a user focuses on the password input, a popover displays information. At the bottom of the popover, there is a link. How can I make the popover expand when the user clicks on this link? I have tried adding an !important class for the height value, us ...