From 61846b88a5d7d43a39ecfbd56c9d9ba6e160b8ad Mon Sep 17 00:00:00 2001 From: Gator Date: Sun, 18 Dec 2022 13:50:21 +0100 Subject: [PATCH] high life rule --- src/components/CanvasBoard.vue | 2 ++ src/modules/automata.js | 16 ++++++++++++++++ src/modules/preset.js | 6 ++++++ 3 files changed, 24 insertions(+) diff --git a/src/components/CanvasBoard.vue b/src/components/CanvasBoard.vue index c2f3373..38f1d56 100644 --- a/src/components/CanvasBoard.vue +++ b/src/components/CanvasBoard.vue @@ -24,6 +24,7 @@ overpopulationRules, lonelinessRules, threebornRules, + highLifeRules, evolve2d, } from "../modules/automata.js"; import { getRandomInt, sleep } from "../modules/common.js"; @@ -42,6 +43,7 @@ overpopulation: overpopulationRules, loneliness: lonelinessRules, threeborn: threebornRules, + highlife: highLifeRules, }, }; }, diff --git a/src/modules/automata.js b/src/modules/automata.js index ac4df56..87ab8ad 100644 --- a/src/modules/automata.js +++ b/src/modules/automata.js @@ -91,6 +91,21 @@ function conwayRules(cell, neighbors) { return cell; } +// variation of the game of life where a +// cell comes to live if 6 neigbor cells are alive +function highLifeRules(cell, neighbors) { + // loneliness rule + if (cell === 1 && neighbors < 2) return 0; + // overpopulation rule + if (cell === 1 && neighbors > 3) return 0; + // born when three live neighbors rule + if (cell === 0 && neighbors === 2) return 1; + // highlife rules + if ((cell === 0 && neighbors === 3) || neighbors === 6) return 1; + // the cell remains the same if none apply + return cell; +} + // variation on the game of life's rules, // where the "three live neighbors" rule is ignored function threebornRules(cell, neighbors) { @@ -184,6 +199,7 @@ export { overpopulationRules, lonelinessRules, threebornRules, + highLifeRules, evolve1d, evolve2d, }; diff --git a/src/modules/preset.js b/src/modules/preset.js index 922aab0..731d18e 100644 --- a/src/modules/preset.js +++ b/src/modules/preset.js @@ -116,6 +116,12 @@ const preset2dRules = [ description: "Variation on Conway's Game of Life *without* the 'three live neighbors' rule", }, + { + id: "highlife", + name: "HighLife variation", + description: + "Variation on Conway's Game of Life where a cell live if the six neighbor cells are alive", + }, ]; export { presetRuleset, initialStates, preset2dRules };