conways-game-of-life

Why can't I make a copy of this 2d array in JS? How can I make a copy?

柔情痞子 提交于 2019-12-01 12:18:45
I'm implementing a John Conway Game of Life, but I'm having a weird problem. Here is a short version if the code giving me trouble: let lifeMap = [ [true, false, false], [false, false, false], [false, false, false] ]; let oldLifeMap = lifeMap.slice(); for (let row = 0; row < lifeMap.length; row++) { for (let val = 0; val < lifeMap[row].length; val++) { let bool = lifeMap[row][val]; let newBool = false; // here is where I would determine if cell is alive/dead lifeMap[row][val] = newBool; if (row === 0 && val === 0) console.log("at (0,0)", oldLifeMap[0][0]); } } In response to this code,

Why can't I make a copy of this 2d array in JS? How can I make a copy?

醉酒当歌 提交于 2019-12-01 10:01:55
问题 I'm implementing a John Conway Game of Life, but I'm having a weird problem. Here is a short version if the code giving me trouble: let lifeMap = [ [true, false, false], [false, false, false], [false, false, false] ]; let oldLifeMap = lifeMap.slice(); for (let row = 0; row < lifeMap.length; row++) { for (let val = 0; val < lifeMap[row].length; val++) { let bool = lifeMap[row][val]; let newBool = false; // here is where I would determine if cell is alive/dead lifeMap[row][val] = newBool; if

Program takes too much memory

走远了吗. 提交于 2019-11-29 16:30:13
I'm using WPF to develop a simulator of Conway's Game of Life. From some reason, sometimes the program takes up to 400,000K memory (When I draw a lot of cells really fast). How can I reduce the memory usage and/or reduce the lags caused by it. Edit 1: Main Window Code: http://pastebin.com/mz0z7tBu Grid class: http://pastebin.com/ZHX1WBuK cell struct: struct Cell { public int Neighbors {get; set;} public bool Alive { get; set; } } Edit 2: I'll try to explain Program Structure: Cell is a structure that contains AutoProperty neighbors ofType int, and AutoProperty IsAlive ofType bool. CellGrid is

Program takes too much memory

梦想的初衷 提交于 2019-11-28 11:20:45
问题 I'm using WPF to develop a simulator of Conway's Game of Life. From some reason, sometimes the program takes up to 400,000K memory (When I draw a lot of cells really fast). How can I reduce the memory usage and/or reduce the lags caused by it. Edit 1: Main Window Code: http://pastebin.com/mz0z7tBu Grid class: http://pastebin.com/ZHX1WBuK cell struct: struct Cell { public int Neighbors {get; set;} public bool Alive { get; set; } } Edit 2: I'll try to explain Program Structure: Cell is a

Code Golf: Conway's Game of Life

泄露秘密 提交于 2019-11-27 10:08:28
The Challenge: Write the shortest program that implements John H. Conway's Game of Life cellular automaton. [ link ] EDIT: After about a week of competition, I have selected a victor: pdehaan , for managing to beat the Matlab solution by one character with perl. For those who haven't heard of Game of Life, you take a grid (ideally infinite) of square cells. Cells can be alive (filled) or dead (empty). We determine which cells are alive in the next step of time by applying the following rules: Any live cell with fewer than two live neighbours dies, as if caused by under-population. Any live

Optimizing Conway's 'Game of Life'

一曲冷凌霜 提交于 2019-11-27 06:25:20
To experiment, I've (long ago) implemented Conway's Game of Life (and I'm aware of this related question!). My implementation worked by keeping 2 arrays of booleans, representing the 'last state', and the 'state being updated' (the 2 arrays being swapped at each iteration). While this is reasonably fast, I've often wondered about how to optimize this. One idea, for example, would be to precompute at iteration N the zones that could be modified at iteration (N+1) (so that if a cell does not belong to such a zone, it won't even be considered for modification at iteration (N+1)). I'm aware that

Code Golf: Conway's Game of Life

主宰稳场 提交于 2019-11-26 17:55:38
问题 Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. The Challenge: Write the shortest program that implements John H. Conway's Game of Life cellular automaton. [link] EDIT: After about a week of competition, I have selected a victor: pdehaan , for managing to beat the Matlab solution by one character with perl. For those who haven't heard of Game of Life, you take a grid

Issue with Game of Life

情到浓时终转凉″ 提交于 2019-11-26 15:32:46
I'm working on a Java implementation of Conway's game of life as a personal project for myself. So far it works but the rules are coming out wrong. The expected patterns aren't showing up as well as it should. Is there something wrong with my code? import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Cell extends JComponent implements MouseListener { private int row, col; private boolean isLiving; public Cell(int r, int c) { this.row = r; this.col = c; this.addMouseListener(this); } public void isAlive(int neighbors) { if (this.isLiving) { if (neighbors < 2) { this

Issue with Game of Life

大城市里の小女人 提交于 2019-11-26 04:02:42
问题 I\'m working on a Java implementation of Conway\'s game of life as a personal project for myself. So far it works but the rules are coming out wrong. The expected patterns aren\'t showing up as well as it should. Is there something wrong with my code? import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Cell extends JComponent implements MouseListener { private int row, col; private boolean isLiving; public Cell(int r, int c) { this.row = r; this.col = c; this