| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent aa946a1 commit 5014c77
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,56 @@ | |||
| 1 | + function CellularAutomata(fillShape, emptyShape) { | ||
| 2 | + var nextGrid = []; | ||
| 3 | + | ||
| 4 | + for (let i = 0; i < G.length; i++) { | ||
| 5 | + nextGrid[i] = []; | ||
| 6 | + for (let j = 0; j < G[i].length; j++) { | ||
| 7 | + var adjCount = 0; | ||
| 8 | + var twoAwayCount = 0; | ||
| 9 | + //look at the states of the neighboring cells | ||
| 10 | + for (var x = -2; x <= 2; x++) { | ||
| 11 | + for (var y = -2; y <= 2; y++) { | ||
| 12 | + if ((i + x >= 0 && i + x < G.length) && (j + y >= 0 && j + y < G[i].length)) { | ||
| 13 | + if (!(x !== 0 && y !== 0) && G[i + x][j + y] == emptyShape) { | ||
| 14 | + if (x == -2 || x == 2 || y == -2 || y == 2) { | ||
| 15 | + twoAwayCount++; | ||
| 16 | + } else { | ||
| 17 | + adjCount++; | ||
| 18 | + } | ||
| 19 | + } | ||
| 20 | + } | ||
| 21 | + } | ||
| 22 | + } | ||
| 23 | + //change the current cell's state according to these rules | ||
| 24 | + if ((adjCount >= 5)) { | ||
| 25 | + nextGrid[i][j] = fillShape; | ||
| 26 | + } else if (adjCount <= 1) { | ||
| 27 | + if (twoAwayCount < 3) { | ||
| 28 | + nextGrid[i][j] = fillShape; | ||
| 29 | + } else { | ||
| 30 | + nextGrid[i][j] = emptyShape; | ||
| 31 | + } | ||
| 32 | + } else { | ||
| 33 | + nextGrid[i][j] = emptyShape; | ||
| 34 | + } | ||
| 35 | + } | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + for (let i = 0; i < nextGrid.length; i++) { | ||
| 39 | + for (let j = 0; j < nextGrid[i].length; j++) { | ||
| 40 | + tracer._denotify(i, j, G[i][j]); | ||
| 41 | + tracer._select(i, j)._wait(); | ||
| 42 | + G[i][j] = nextGrid[i][j]; | ||
| 43 | + if (G[i][j] == fillShape) { | ||
| 44 | + tracer._notify(i, j, G[i][j]); | ||
| 45 | + } else { | ||
| 46 | + tracer._notify(i, j, G[i][j]); | ||
| 47 | + tracer._denotify(i, j, G[i][j]); | ||
| 48 | + tracer._deselect(i, j); | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + for (var iter = 0; iter < generations; iter++) { | ||
| 55 | + CellularAutomata('#', '.'); | ||
| 56 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,28 @@ | |||
| 1 | + var gridSize = 10; | ||
| 2 | + var generations = 4; | ||
| 3 | + var fillChance = 0.55; | ||
| 4 | + | ||
| 5 | + var G = []; | ||
| 6 | + var nextG = []; | ||
| 7 | + for (var i = 0; i < gridSize; i++) { | ||
| 8 | + G[i] = []; | ||
| 9 | + nextG[i] = []; | ||
| 10 | + for (var j = 0; j < gridSize; j++) { | ||
| 11 | + if (Math.random() < fillChance || i === 0 || j === 0 || i == gridSize - 1 || j == gridSize - 1) { | ||
| 12 | + G[i][j] = '#'; | ||
| 13 | + } else { | ||
| 14 | + G[i][j] = '.'; | ||
| 15 | + } | ||
| 16 | + nextG[i][j] = '#'; | ||
| 17 | + } | ||
| 18 | + } | ||
| 19 | + var tracer = new Array2DTracer (); | ||
| 20 | + tracer._setData(G); | ||
| 21 | + | ||
| 22 | + for (var gi = 0; gi < G.length; gi++) { | ||
| 23 | + for (var gj = 0; gj < G[gi].length; gj++) { | ||
| 24 | + if (G[gi][gj] == '#') { | ||
| 25 | + tracer._notify(gi, gj, G[gi][gj]); | ||
| 26 | + } | ||
| 27 | + } | ||
| 28 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,9 @@ | |||
| 1 | + { | ||
| 2 | + "Cellular Automata": "Uses a grid of cells and looks at each cell and the state (ex: on/off) of each of the surrounding neighbor cells and changes the state of the current cell according to a set of rules based on the neighbor cells' states.", | ||
| 3 | + "References": [ | ||
| 4 | + "<a href='https://en.wikipedia.org/wiki/Cellular_automaton'>Wikipedia</a>" | ||
| 5 | + ], | ||
| 6 | + "files": { | ||
| 7 | + "cellular_automata": "" | ||
| 8 | + } | ||
| 9 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments