fixes empty initial step and board dimensions

This commit is contained in:
Gator
2022-12-05 15:05:00 +01:00
parent a567b565e9
commit ed4dc5c2eb
3 changed files with 17 additions and 9 deletions

View File

@ -31,11 +31,12 @@ function evolve1d(state, rules) {
// }
// performance "choke point" in full imperative
function createBoard(state, rules, height) {
function createBoard(state, rules, max) {
var board = [];
let prevState = [];
for (let i = 0; i < height; i++) {
for (let i = 0; i < max; i++) {
let nextState = [];
// use the passed initial step during first iteration
if (i == 0) {
nextState = evolve1d(state, rules);
} else {
@ -121,7 +122,7 @@ function getDrawingValues(state, acc, cell) {
// Populates the first state with a single living cell in the center
function create1dStateOneCell(width) {
return [...Array(width)].map((cell, index) => {
if (index === width / 2 || index === width + 1 / 2) return 1;
if (index === Math.floor(width / 2)) return 1;
return 0;
});
}