From beab48742924dc659d2b7b8ecb58e59cb553b61e Mon Sep 17 00:00:00 2001 From: Gator Date: Thu, 15 Dec 2022 16:18:57 +0100 Subject: [PATCH] overpopulation rules --- src/components/CanvasBoard.vue | 11 ++++++++++- src/components/Menu2dCA.vue | 34 +++++++++++++++++++++++++++++++--- src/modules/automata.js | 12 ++++++++++++ src/modules/preset.js | 16 +++++++++++++++- src/stores/index.js | 5 +++++ 5 files changed, 73 insertions(+), 5 deletions(-) diff --git a/src/components/CanvasBoard.vue b/src/components/CanvasBoard.vue index 3b1ff82..d624fde 100644 --- a/src/components/CanvasBoard.vue +++ b/src/components/CanvasBoard.vue @@ -21,6 +21,7 @@ create2dState, createBoard, conwayRules, + overpopulationRules, evolve2d, } from "../modules/automata.js"; import { getRandomInt, sleep } from "../modules/common.js"; @@ -34,6 +35,10 @@ workCanvas: null, workCtx: null, ctx: null, + available2dRules: { + conway: conwayRules, + overpopulation: overpopulationRules, + }, }; }, computed: { @@ -51,6 +56,7 @@ boardWidth: "boardWidth", boardHeight: "boardHeight", picture: "picture", + selected2dRules: "selected2dRules", }), ...mapWritableState(globalStore, { lastBoard: "lastBoard", @@ -134,7 +140,10 @@ if (!this.canDraw) return; const draw2dNext = async (b) => { if (!this.canDraw) return; - const newBoard = evolve2d(b, conwayRules); + const newBoard = evolve2d( + b, + this.available2dRules[this.selected2dRules.id] + ); this.drawCanvas(b, this.cellProperties); await sleep(this.refreshRate); draw2dNext(newBoard); diff --git a/src/components/Menu2dCA.vue b/src/components/Menu2dCA.vue index 7a3ca06..e8a8afc 100644 --- a/src/components/Menu2dCA.vue +++ b/src/components/Menu2dCA.vue @@ -17,14 +17,33 @@
+
+ +
diff --git a/src/modules/automata.js b/src/modules/automata.js index 8ed51a5..42e5f63 100644 --- a/src/modules/automata.js +++ b/src/modules/automata.js @@ -91,6 +91,17 @@ function conwayRules(cell, neighbors) { return cell; } +// variation on the game of life's rules, +// where the overpopulation rule is ignored +function overpopulationRules(cell, neighbors) { + // loneliness rule + if (cell === 1 && neighbors < 2) return 0; + // born when three live neighbors rule + if (cell === 0 && neighbors === 3) return 1; + // the cell remains the same if none apply + return cell; +} + // get the next evolution of a 2D CA initial state // Rules : Moore neighborhood function evolve2d(board, rulesFn) { @@ -148,6 +159,7 @@ export { createBoard, create1dStateOneCell, conwayRules, + overpopulationRules, evolve1d, evolve2d, }; diff --git a/src/modules/preset.js b/src/modules/preset.js index 0eee491..0a5b26f 100644 --- a/src/modules/preset.js +++ b/src/modules/preset.js @@ -92,4 +92,18 @@ const initialStates = [ }, ]; -export { presetRuleset, initialStates }; +const preset2dRules = [ + { + id: "conway", + name: "Conway's Game of Life", + description: "The most popular 2d automata", + }, + { + id: "overpopulation", + name: "Overpopulation variation", + description: + "Variation on Conway's Game of Life *without* the overpopulation rule", + }, +]; + +export { presetRuleset, initialStates, preset2dRules }; diff --git a/src/stores/index.js b/src/stores/index.js index 255cfdb..cd81b56 100644 --- a/src/stores/index.js +++ b/src/stores/index.js @@ -16,6 +16,11 @@ export const globalStore = defineStore("globalStore", { "000": 1, }, }, + selected2dRules: { + id: "conway", + name: "Conway's Game of Life", + description: "The most popular 2d automata", + }, cellProperties: { size: 3, liveColor: "#000000",