Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Implement Exercise7.2 #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions chp07_CA/Exercise_7_02_WolframCA_randomized_initial_states/CA.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class CA {
constructor() {
// the width of each cell
this.w = 10;

// all cells start with state 0, except the center cell has state 1
this.cells = new Array(width / this.w).fill(0);
this.cells[this.cells.length / 2] = 1;

// keep track of generations
this.generation = 0;

// rule 90 in Wolfram's CA
this.ruleSet = [0, 1, 0, 1, 1, 0, 1, 0];
}

generate() {
// an array to store the state for the next generation
let nextGen = new Array(this.cells.length).fill(0);

// update generation
for (let i = 1; i < this.cells.length - 1; i++) {
// look at the states from the current array
let left = this.cells[i - 1];
let middle = this.cells[i];
let right = this.cells[i + 1];

// create a new generation from a rule
nextGen[i] = this.rules(left, middle, right);

// the new generation becomes the current generation
}
this.cells = nextGen;
this.generation++;
}

// look up a new state from the ruleset
rules(a, b, c) {
// join three bits into a string
let s = "" + a + b + c;

// translate binary into a index in the ruleset
let index = parseInt(s, 2);

// return a new state from the rule in the ruleset
return this.ruleSet[index];
}

display() {
// call restart() when the last generation reaches the edge of the canvas
if (this.generation * this.w >= height) {
this.restart();
}
for (let i = 0; i < this.cells.length; i++) {
// 0 -> white, 1 -> black
if (this.cells[i] === 1) fill(0);
else fill(255);
// display each generation
rect(i * this.w, this.generation * this.w, this.w, this.w);
}
}
// randomize the states of cells and reset generation counter
restart() {
this.cells = new Array(width / this.w)
.fill(0)
.map((elem) => Math.round(Math.random()));
this.generation = 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js"></script>
<meta charset="utf-8" />
</head>

<body>
<script src="CA.js"></script>
<script src="sketch.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let ca;

function setup() {
frameRate(20);
createCanvas(400, 400);
ca = new CA();
}

function draw() {
ca.generate();
ca.rules();
ca.display();
}