I'm using the p5.js library.
I'm making a program that is meant to automatically fill closed spaces but I'm having a problem.
Sometimes the program fills half closed loops and I can't figure out why.
If anyone could help me identify the problem I will fix it myself.
Here is an example of that problem:
You have to zoom in a lot or it breaks (on a side note if you know how to scale up the pixels that would also be great)
let pixelVals;
let checkedPixels;
let filledPixels;
let iter = 0;
let drawFilled;
function setup() {
pixelVals = array(height, width, 4);
createCanvas(25, 25);
pixelDensity(1);
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
pixelVals[i][j][0] = 0;
pixelVals[i][j][1] = 0;
pixelVals[i][j][2] = 0;
pixelVals[i][j][3] = 255;
}
}
}
function draw() {
loadPixels();
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
pixelVals[i][j][1] = 0;
}
}
if(mouseIsPressed) {
if(mouseX < width && mouseX >= 0 && mouseY < height && mouseY >= 0) {
pixelVals[mouseX][mouseY][0] = 255;
}
}
checkEnclosed();
updatePixels();
}
function checkEnclosed() {
checkedPixels = array(height, width);
filledPixels = array(height, width);
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
checkedPixels[i][j] = false;
filledPixels[i][j] = false;
}
}
for(var i = 0; i < height; i++) {
for(var j = 0; j < width; j++) {
if(!checkedPixels[i][j] && pixelVals[i][j][0] != 255) {
drawFilled = true;
checkSurroundings(i, j);
if(drawFilled) {
setFilled();
} else {
setFilledFalse();
}
}
}
}
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
for(var k = 0; k < 4; k++) {
pixels[(i + j * width) * 4 + k] = pixelVals[i][j][k];
}
}
}
}
function checkSurroundings(x, y) {
pixelVals[x][y][1] = 255;
filledPixels[x][y] = true;
checkedPixels[x][y] = true;
if(x == width - 1 || x == 0 || y == height - 1 || y == 0) {
drawFilled = false;
} else {
if(x + 1 < width) {
if(pixelVals[x + 1][y][0] != 255) {
if(!checkedPixels[x + 1][y]) {
checkSurroundings(x + 1, y)
}
}
}
if(x - 1 >= 0) {
if(pixelVals[x - 1][y][0] != 255) {
if(!checkedPixels[x - 1][y]) {
checkSurroundings(x - 1, y)
}
}
}
if(y + 1 < height) {
if(pixelVals[x][y + 1][0] != 255) {
if(!checkedPixels[x][y + 1]) {
checkSurroundings(x, y + 1)
}
}
}
if(y - 1 >= 0) {
if(pixelVals[x][y - 1][0] != 255) {
if(!checkedPixels[x][y - 1]) {
checkSurroundings(x, y - 1)
}
}
}
}
}
function setFilled() {
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
if(filledPixels[i][j]) {
pixelVals[i][j][2] = 255;
}
}
}
}
function setFilledFalse() {
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
filledPixels[i][j] = false;
}
}
}
function array(length) {
var arr = new Array(length || 0), i = length;
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
while(i--) arr[length-1 - i] = array.apply(this, args);
}
return arr
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
I'm new to JavaScript so ignore the messy code :).
let pixelVals;
let checkedPixels;
let filledPixels;
let iter = 0;
let drawFilled;
function setup() {
pixelVals = array(height, width, 4);
createCanvas(25, 25);
pixelDensity(1);
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
pixelVals[i][j][0] = 0;
pixelVals[i][j][1] = 0;
pixelVals[i][j][2] = 0;
pixelVals[i][j][3] = 255;
}
}
}
function draw() {
loadPixels();
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
pixelVals[i][j][1] = 0;
}
}
if(mouseIsPressed) {
if(mouseX < width && mouseX >= 0 && mouseY < height && mouseY >= 0) {
pixelVals[mouseX][mouseY][0] = 255;
}
}
checkEnclosed();
updatePixels();
}
function checkEnclosed() {
checkedPixels = array(height, width);
filledPixels = array(height, width);
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
checkedPixels[i][j] = false;
filledPixels[i][j] = false;
}
}
for(var i = 0; i < height; i++) {
for(var j = 0; j < width; j++) {
if(!checkedPixels[i][j] && pixelVals[i][j][0] != 255) {
drawFilled = true;
checkSurroundings(i, j);
if(drawFilled) {
setFilled();
} else {
setFilledFalse();
}
}
}
}
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
for(var k = 0; k < 4; k++) {
pixels[(i + j * width) * 4 + k] = pixelVals[i][j][k];
}
}
}
}
function checkSurroundings(x, y) {
pixelVals[x][y][1] = 255;
filledPixels[x][y] = true;
checkedPixels[x][y] = true;
if(x == width - 1 || x == 0 || y == height - 1 || y == 0) {
drawFilled = false;
} else {
if(x + 1 < width) {
if(pixelVals[x + 1][y][0] != 255) {
if(!checkedPixels[x + 1][y]) {
checkSurroundings(x + 1, y)
}
}
}
if(x - 1 >= 0) {
if(pixelVals[x - 1][y][0] != 255) {
if(!checkedPixels[x - 1][y]) {
checkSurroundings(x - 1, y)
}
}
}
if(y + 1 < height) {
if(pixelVals[x][y + 1][0] != 255) {
if(!checkedPixels[x][y + 1]) {
checkSurroundings(x, y + 1)
}
}
}
if(y - 1 >= 0) {
if(pixelVals[x][y - 1][0] != 255) {
if(!checkedPixels[x][y - 1]) {
checkSurroundings(x, y - 1)
}
}
}
}
}
function setFilled() {
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
if(filledPixels[i][j]) {
pixelVals[i][j][2] = 255;
}
}
}
}
function setFilledFalse() {
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
filledPixels[i][j] = false;
}
}
}
function array(length) {
var arr = new Array(length || 0), i = length;
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
while(i--) arr[length-1 - i] = array.apply(this, args);
}
return arr
}
Much appreciated,
Zac
This was my solution by changing the checkSurroundings function:
function checkSurroundings(x, y) {
pixelVals[x][y][1] = 255;
filledPixels[x][y] = true;
checkedPixels[x][y] = true;
if(x == width - 1 || x == 0 || y == height - 1 || y == 0) {
drawFilled = false;
}
if(x + 1 < width) {
if(pixelVals[x + 1][y][0] != 255) {
if(!checkedPixels[x + 1][y]) {
checkSurroundings(x + 1, y)
}
}
}
if(x - 1 >= 0) {
if(pixelVals[x - 1][y][0] != 255) {
if(!checkedPixels[x - 1][y]) {
checkSurroundings(x - 1, y)
}
}
}
if(y + 1 < height) {
if(pixelVals[x][y + 1][0] != 255) {
if(!checkedPixels[x][y + 1]) {
checkSurroundings(x, y + 1)
}
}
}
if(y - 1 >= 0) {
if(pixelVals[x][y - 1][0] != 255) {
if(!checkedPixels[x][y - 1]) {
checkSurroundings(x, y - 1)
}
}
}
}
let pixelVals;
let checkedPixels;
let filledPixels;
let iter = 0;
let drawFilled;
function setup() {
pixelVals = array(height, width, 4);
createCanvas(25, 25);
pixelDensity(1);
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
pixelVals[i][j][0] = 0;
pixelVals[i][j][1] = 0;
pixelVals[i][j][2] = 0;
pixelVals[i][j][3] = 255;
}
}
}
function draw() {
loadPixels();
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
pixelVals[i][j][1] = 0;
}
}
if(mouseIsPressed) {
if(mouseX < width && mouseX >= 0 && mouseY < height && mouseY >= 0) {
pixelVals[mouseX][mouseY][0] = 255;
}
}
checkEnclosed();
updatePixels();
}
function checkEnclosed() {
checkedPixels = array(height, width);
filledPixels = array(height, width);
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
checkedPixels[i][j] = false;
filledPixels[i][j] = false;
}
}
for(var i = 0; i < height; i++) {
for(var j = 0; j < width; j++) {
if(!checkedPixels[i][j] && pixelVals[i][j][0] != 255) {
drawFilled = true;
checkSurroundings(i, j);
if(drawFilled) {
setFilled();
} else {
setFilledFalse();
}
}
}
}
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
for(var k = 0; k < 4; k++) {
pixels[(i + j * width) * 4 + k] = pixelVals[i][j][k];
}
}
}
}
function checkSurroundings(x, y) {
pixelVals[x][y][1] = 255;
filledPixels[x][y] = true;
checkedPixels[x][y] = true;
if(x == width - 1 || x == 0 || y == height - 1 || y == 0) {
drawFilled = false;
}
if(x + 1 < width) {
if(pixelVals[x + 1][y][0] != 255) {
if(!checkedPixels[x + 1][y]) {
checkSurroundings(x + 1, y)
}
}
}
if(x - 1 >= 0) {
if(pixelVals[x - 1][y][0] != 255) {
if(!checkedPixels[x - 1][y]) {
checkSurroundings(x - 1, y)
}
}
}
if(y + 1 < height) {
if(pixelVals[x][y + 1][0] != 255) {
if(!checkedPixels[x][y + 1]) {
checkSurroundings(x, y + 1)
}
}
}
if(y - 1 >= 0) {
if(pixelVals[x][y - 1][0] != 255) {
if(!checkedPixels[x][y - 1]) {
checkSurroundings(x, y - 1)
}
}
}
}
function setFilled() {
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
if(filledPixels[i][j]) {
pixelVals[i][j][2] = 255;
}
}
}
}
function setFilledFalse() {
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
filledPixels[i][j] = false;
}
}
}
function array(length) {
var arr = new Array(length || 0), i = length;
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
while(i--) arr[length-1 - i] = array.apply(this, args);
}
return arr
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
Related
As the title says I am trying to write luhns algorithm in Javascript and have it print out a valid card number and what type of card it is whether that be American Express, Mastercard, or Visa.
When ran all it does is continuously loop the alert box. What I want it to print is this is a valid card type and what brand of card it is in the Alert Box.
function checkCreditCard() {
var ccnum = document.getElementById("cardnum").value;
var cardArray = [cardnum]
var temp = ccnum;
var checkerArray;
if (cardType(ccnum) === "AmericanExpress") {
checkerArray = [15];
for (x = 0; x < 15; x++) {
checkerArray[x] = ccnum % 10;
ccnum = ccnum / 10;
}
} else {
checkerArray = [16];
for (x = 0; x < 16; x++) {
checkerArray[x] = ccnum % 10;
ccnum = ccnum / 10;
if (ccnum == 0) {
checkerArray[15] = -1;
checkerArray[14] = -1;
checkerArray[13] = -1;
}
}
}
var summing;
for (x = 1; x < checkerArray.length; x = x + 2) {
if (checkerArray[x] < 0) {
return;
}
checkerArray[x] = checkerArray[x] * 2;
if (checkerArray[x] >= 10) {
summing = summing + checkerArray[x] % 10 + checkerArray[x] / 10
} else {
summing = summing + checkerArray[x];
}
}
for (x = 0; x < checkerArray.length; x = x + 2) {
if (checkerArray[x] < 0) {
return;
}
summing = summing + checkerArray[x];
if (summing == 20) {
alert("This Card is Legit")
} else {
alert("This Card is Invalid")
}
}
function cardType(ccnum) {
var x = {
Visa: /^4[0-9]{12}(?:[0-9]{3})?$/,
Mastercard: /^5[1-5][0-9]{14}$/,
AmericanExpress: /^3[47][0-9]{13}$/,
}
for (var l in x) {
if (x[l].test(ccnum)) {
return l;
}
}
return null;
}
}
<input type="text" id="cardnum" onkeyup="checkCreditCard()" />
<span id="valid">Enter a Number and Press Enter</span>
Let's use let
Also let's move the alert OUTSIDE the loop
function cardType(ccnum) {
var x = {
Visa: /^4[0-9]{12}(?:[0-9]{3})?$/,
Mastercard: /^5[1-5][0-9]{14}$/,
AmericanExpress: /^3[47][0-9]{13}$/,
}
for (var l in x) {
if (x[l].test(ccnum)) {
return l;
}
}
return null;
}
document.getElementById("cardForm").addEventListener("submit", function(e) {
e.preventDefault();
var ccnum = document.getElementById("cardnum").value;
if (ccnum.length < 16) return
var cardArray = [cardnum]
var temp = ccnum;
var checkerArray;
if (cardType(ccnum) === "AmericanExpress") {
checkerArray = [15];
for (let x = 0; x < 15; x++) {
checkerArray[x] = ccnum % 10;
ccnum /= 10;
}
} else {
checkerArray = [16];
for (let x = 0; x < 16; x++) {
checkerArray[x] = ccnum % 10;
ccnum /= 10;
if (ccnum == 0) {
checkerArray[15] = -1;
checkerArray[14] = -1;
checkerArray[13] = -1;
}
}
}
var summing;
for (let x = 1; x < checkerArray.length; x = x + 2) {
checkerArray[x] *= 2;
if (checkerArray[x] >= 10) {
summing += checkerArray[x] % 10 + checkerArray[x] / 10
} else {
summing += checkerArray[x];
}
}
for (let x = 0; x < checkerArray.length; x = x + 2) {
summing += checkerArray[x];
}
if (summing == 20) {
alert("This Card is Legit")
} else {
alert("This Card is Invalid")
}
})
<form id="cardForm">
<input type="tel" id="cardnum" maxlength="16" />
<span id="valid">Enter a Number and Press Enter</span>
</form>
My code is as follows:
var map = [];
document.body.onkeydown = document.body.onkeyup = function(e){
e = e || event; // to deal with IE
map[e.key] = e.type == 'keydown';
console.log(map)
if (map["ArrowLeft"]) {move("left")}
if (map["ArrowRight"]) {move("right")}
if (map["Spacebar"] || map[" "]) {jump()};
However if i hold down the right arrow key and then press space the character jumps without changing the x pos.
My jumping code is:
async function jump() {
var jh = j;
var i;
for (i = jh * 2; i > -Math.abs(jh) * 2 - 0.5; i -= 0.5) {
rem_rect(70, py, 30, 30)
py -= i
rect(70, py, 30, 30)
await sleep(15)
}
}
My moving code is:
async function move(d) {
var i;
for (i = 0; i < p.length; i++) {
if (d === "right") {
var j;
for (j = 0; j <= 5; j++) {
rem_rect(p[i][0], p[i][1], p[i][2], p[i][3]);
p[i][0] -= ms / 5;
rect(p[i][0], p[i][1], p[i][2], p[i][3])
await sleep(0)
}
}
else if (d === "left") {
var j;
for (j = 0; j <= 5; j++) {
rem_rect(p[i][0], p[i][1], p[i][2], p[i][3]);
p[i][0] += ms / 5;
rect(p[i][0], p[i][1], p[i][2], p[i][3])
await sleep(0)
}
}
}
}
My function seem not to work and I am not sure why. It should return true or false but it doesnt. I am trying to calculate if the value is lower than 0 and if so return false, true otherwise.
function tickets(peopleInLine){
// ...
var twentyfive = 0;
var fifty = 0;
var hundred = 0;
function checkforsales() {
if ((twentyfive < 0) || (fifty < 0) || (hundred < 0)) {
return false
} else {
return true
}
}
for (let i = 0; i < peopleInLine.length; i++) {
if(peopleInLine[i] === 25) {
twentyfive = twentyfive + 25;
} else if (peopleInLine[i] === 50) {
fifty = fifty + 50;
twentyfive = twentyfive - 25;
checkforsales();
} else {
hundred = hundred + 100;
checkforsales();
}
}
}
console.log(tickets([25, 100]));
function tickets(peopleInLine){
// ...
var twentyfive = 0;
var fifty = 0;
var hundred = 0;
function checkforsales() {
if ((twentyfive < 0) || (fifty < 0) || (hundred < 0)) {
return false
} else {
return true
}
}
for (let i = 0; i < peopleInLine.length; i++) {
if(peopleInLine[i] === 25) {
twentyfive = twentyfive + 25;
} else if (peopleInLine[i] === 50) {
fifty = fifty + 50;
twentyfive = twentyfive - 25;
} else {
hundred = hundred + 100;
}
}
return checkforsales();
}
console.log(tickets([25, 100]));
You need to return checkforsales function as below
function tickets(peopleInLine){
// ...
var twentyfive = 0;
var fifty = 0;
var hundred = 0;
function checkforsales() {
if ((twentyfive < 0) || (fifty < 0) || (hundred < 0)) {
return false
} else {
return true
}
}
for (let i = 0; i < peopleInLine.length; i++) {
if(peopleInLine[i] === 25) {
twentyfive = twentyfive + 25;
} else if (peopleInLine[i] === 50) {
fifty = fifty + 50;
twentyfive = twentyfive - 25;
} else {
hundred = hundred + 100;
}
}
return checkforsales();
}
Yesterday, I had asked for help in printing out a Fractal tree. That question was answered, but now I am trying to implement one more rule into my code. And while implementing it in my code, it is printing the tree, but not at the right coordinates. Here is a link to my previous question. I would like to print both the trees side-by-side instead of on top of each other. So my question is how do I achieve that? Thanks for all the help.
Here is the new code:
var sentence = "F";
var sentence2 = "F";
var sentence3 = "X";
var rules = [];
rules[0] = {
a: "F",
b: "F[+F]F[-F]F"
}
var rulesB = [];
rulesB[0] = {
a: "F",
b: "FF-[-F+F+F]+[+F-F-F]"
}
/*
var rulesC = [];
rulesC[0] = {
a: "F",
b: "FF"
}
rulesC[1] = {
a: "X",
b: "F-[[X]+X]+F[+FX]-X"
}
*/
var x = 50; // starting x
var y = 400; // starting y
var y_stack = []; // save & restore don't handle coordinates
var y_stack2 = []
draw();
function turtle(sentence, context){
for (var i = 0; i < sentence.length; i++) {
var current = sentence.charAt(i);
if (current == "F") {
y -= 20;
context.lineTo(x, y);
context.stroke();
} else if (current == "+") {
context.translate(x, y);
context.rotate((20 * Math.PI) / 180);
context.translate(-x, -y);
} else if (current == "-") {
context.translate(x, y);
context.rotate((-20 * Math.PI) / 180);
context.translate(-x, -y);
} else if (current == "[") {
context.save();
y_stack.push(y);
} else if (current == "]") {
context.restore();
y = y_stack.pop();
context.moveTo(x, y)
}
}
}
function generate(sentence){
var nextSentence = "";
for (var i = 0; i < sentence.length; i++) {
var current = sentence.charAt(i);
var found = false;
for (var j = 0; j < rules.length; j++ ) {
if (current == rules[j].a) {
found = true;
nextSentence += rules[j].b;
break;
}
}
if (!found) {
nextSentence += current;
}
}
return nextSentence;
}
function turtle2(sentence2, context){
for (var i = 0; i < sentence2.length; i++) {
var current = sentence2.charAt(i);
if (current == "F") {
y -= 20;
context.lineTo(x, y);
context.stroke();
} else if (current == "+") {
context.translate(x, y);
context.rotate((22.5 * Math.PI) / 180);
context.translate(-x, -y);
} else if (current == "-") {
context.translate(x, y);
context.rotate((-22.5 * Math.PI) / 180);
context.translate(-x, -y);
} else if (current == "[") {
context.save();
y_stack2.push(y);
} else if (current == "]") {
context.restore();
y = y_stack2.pop();
context.moveTo(x, y)
}
}
}
function generate2(sentence2){
var nextsentence2 = "";
for (var i = 0; i < sentence2.length; i++) {
var current = sentence2.charAt(i);
var found = false;
for (var j = 0; j < rulesB.length; j++ ) {
if (current == rulesB[j].a) {
found = true;
nextsentence2 += rulesB[j].b;
break;
}
}
if (!found) {
nextsentence2 += current;
}
}
return nextsentence2;
}
function draw() {
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext('2d');
// context.moveTo(x, y);
for (i = 0; i < 2; i++) {
sentence = generate(sentence);
}
console.log(sentence);
turtle(sentence, context);
//context.moveTo(100,400);
for(i = 0; i < 2; i++) {
sentence2 = generate2(sentence2);
}
console.log(sentence2);
turtle2(sentence2, context);
}
/*
function turtle3(){
//resetMatrix();
context.translate(width/6, height);
for (var n = 0; n < sentence3.length; n++){
var current = sentence3.charAt(n);
if (current == "F"){
context.stroke("#28b232");
context.lineTo(0, -len3);
context.translate(0, -len3);
}else if (current == "X"){
context.stroke("#024d08");
context.lineTo(-len3, 0);
context.translate(0, -len3);
}else if (current == "+"){
context.rotate(angle2);
}else if (current == "-"){
context.rotate(-angle2);
}else if (current == "["){
save();
}else if (current == "]"){
restore();
}
}
}
*/
/*
function generate3(){
len2 *= .5;
var nextSentence3 = "";
for (var i = 0; i < sentence3.length; i++){
var current3 = sentence3.charAt(i);
var found3 = false;
for (var j = 0; j < rulesC.length; j++ ){
if (current3 == rulesC[j].a){
found3 = true;
nextSentence3 += rulesC[j].b;
break;
}
}
if (!found3){
nextSentence3 += current3;
}
}
sentence3 = nextSentence3;
turtle3();
}*/
I have a 2d array called 'map.curCellMap' storing 1's and 0's. I am using the function 'cells.getNeighbourCount' (below) to reference cells in this map.
Within the context of the 'cells.update' function (below) this is returning an undefined error. In firefox it reads 'TypeError: map.curCellMap[nextY] is undefined'. However, if I call 'cells.getNeighbourCount' with hardcoded values it does work.
What gives?
var cells = {
getNeighbourCount: function(x, y) {
/*
/* [y][x]
/* Start a top and move clockwise
*/
var neighbours = [[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1],[0,-1]],
count = 0;
for(i = 0; i < neighbours.length; i++) {
var curNeighbour = neighbours[i];
//Skip this iteration if neighbour is out of bounds
if((y + curNeighbour[0] < 0) || (y + curNeighbour[0] > ROWS) || (x + curNeighbour[1] < 0) || (x + curNeighbour[1] > COLS)) {
continue;
}
var nextY = y+curNeighbour[0],
nextX = x+curNeighbour[1];
if(map.curCellMap[nextY][nextX] == 1) {
count++;
}
}
return count;
},
update: function() {
for(y = 0; y < ROWS; y++) {
for(x = 0; x < COLS; x++) {
var numNeighbours = this.getNeighbourCount(x, y),
newCellState;
if(numNeighbours >= 2 && numNeighbours <= 3) {
newCellState = 1;
} else {
newCellState = 0;
}
map.nextCellMap[y][x] = newCellState;
}
}
map.curCellMap = map.nextCellMap;
}
}
var map = {
curCellMap: null,
nextCellMap: null,
//Map functions
init: function() {
this.curCellMap = this.generateRandomMap(0.05);
this.nextCellMap = this.curCellMap;
},
generateRandomMap: function(density) {
/*
/* Generates a random cell map
/* density = 0.00 -> No live cells, density = 1.00 -> All cells live
*/
var map = helpers.create2DArray(ROWS, COLS);
for(y = 0; y < ROWS; y++) {
for(x = 0; x < COLS; x++) {
var rand = helpers.getRandomNum(0, 1);
map[y][x] = (rand <= density) ? 1 : 0;
}
}
return map;
}
}
The problem was
if((y + curNeighbour[0] < 0) || (y + curNeighbour[0] > ROWS) || (x + curNeighbour[1] < 0) || (x + curNeighbour[1] > COLS)) {
Needed to read
if((y + curNeighbour[0] < 0) || (y + curNeighbour[0] > ROWS-1) || (x + curNeighbour[1] < 0) || (x + curNeighbour[1] > COLS-1)) {
OOPS!