From 088b851cd6ab8561b4370abd254ecc30c86e6dac Mon Sep 17 00:00:00 2001 From: searchingforapuppy Date: Wed, 28 Apr 2021 20:49:06 +0900 Subject: [PATCH] Implement Exercise7.2 --- .../CA.js | 69 +++++++++++++++++++ .../index.html | 13 ++++ .../sketch.js | 13 ++++ 3 files changed, 95 insertions(+) create mode 100644 chp07_CA/Exercise_7_02_WolframCA_randomized_initial_states/CA.js create mode 100644 chp07_CA/Exercise_7_02_WolframCA_randomized_initial_states/index.html create mode 100644 chp07_CA/Exercise_7_02_WolframCA_randomized_initial_states/sketch.js diff --git a/chp07_CA/Exercise_7_02_WolframCA_randomized_initial_states/CA.js b/chp07_CA/Exercise_7_02_WolframCA_randomized_initial_states/CA.js new file mode 100644 index 00000000..b72a4ca7 --- /dev/null +++ b/chp07_CA/Exercise_7_02_WolframCA_randomized_initial_states/CA.js @@ -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; + } +} diff --git a/chp07_CA/Exercise_7_02_WolframCA_randomized_initial_states/index.html b/chp07_CA/Exercise_7_02_WolframCA_randomized_initial_states/index.html new file mode 100644 index 00000000..62bd4706 --- /dev/null +++ b/chp07_CA/Exercise_7_02_WolframCA_randomized_initial_states/index.html @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/chp07_CA/Exercise_7_02_WolframCA_randomized_initial_states/sketch.js b/chp07_CA/Exercise_7_02_WolframCA_randomized_initial_states/sketch.js new file mode 100644 index 00000000..73e7c368 --- /dev/null +++ b/chp07_CA/Exercise_7_02_WolframCA_randomized_initial_states/sketch.js @@ -0,0 +1,13 @@ +let ca; + +function setup() { + frameRate(20); + createCanvas(400, 400); + ca = new CA(); +} + +function draw() { + ca.generate(); + ca.rules(); + ca.display(); +}