now it actually evolves according to the rules

This commit is contained in:
2021-12-30 14:42:10 +01:00
parent 995fca7355
commit 6166475c5c
2 changed files with 16 additions and 15 deletions

21
main.js
View File

@ -5,10 +5,10 @@ const rules = {
"110": "1",
"101": "0",
"100": "1",
"011": "0",
"011": "1",
"010": "0",
"001": "0",
"000": "1"
"001": "1",
"000": "0"
}
function getRandomInt(min, max) {
@ -24,9 +24,8 @@ function sleep(ms) {
function evolve(state, acc) {
const [x, y, z, ...xs] = state;
if (!xs.length) {
return acc + x + acc[0] + acc[1]
return acc[0] + acc + acc[acc.length - 1]
}
else {
const rule = x + y + z;
@ -40,10 +39,16 @@ function draw(state, context, acc) {
Object.keys(state).forEach(
function (key) {
context.moveTo(key * cell_dim, acc * cell_dim)
context.fillRect(key * cell_dim, acc * cell_dim, cell_dim, cell_dim)
if(state[key] == "1") {
context.fillStyle="black";
context.moveTo(key * cell_dim, acc * cell_dim)
context.fillRect(key * cell_dim, acc * cell_dim, cell_dim, cell_dim)
}else {
if (acc % 2) {
context.fillStyle="white";
}else {
context.fillStyle="white";
}
}
})
}
@ -58,6 +63,8 @@ window.addEventListener("load", async function() {
_=> getRandomInt(0, 2).toString()
).join("")
console.log("initial state length : ", initial_state.length)
var new_state = evolve(initial_state, "")
var acc = 0