Created
September 17, 2018 09:13
-
-
Save spiterman/9efac1db392f43c260b77ce287a2b0dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Organism { | |
| constructor(r, g, b) { | |
| this.r = r; | |
| this.g = g; | |
| this.b = b; | |
| this.mutationSize = 10; //Must be greater than 2 | |
| } | |
| reproduce() { | |
| // Prevents negative numbers from appearing | |
| let newR = Math.max(this.r + this.mutateTrait(), 0); | |
| let newG = Math.max(this.g + this.mutateTrait(), 0); | |
| let newB = Math.max(this.b + this.mutateTrait(), 0); | |
| return new Organism(newR, newG, newB); | |
| } | |
| mutateTrait() { | |
| let multiplier = Math.random() - 0.5; | |
| if(multiplier < 0) { | |
| return randomValue(this.mutationSize) * -1; | |
| } else { | |
| return randomValue(this.mutationSize); | |
| } | |
| } | |
| } | |
| function randomValue(range) { | |
| return Math.floor(Math.random() * range); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment